欢迎访问宙启技术站
智能推送

Java函数中的编解码和加密解密

发布时间:2023-05-28 19:38:32

Java中常用的编解码方式有Base64、URL编码和Html编码,常用的加密解密算法有对称加密和非对称加密。

1. Base64编解码

Base64是一种用64个字符来表示任意二进制数据的方法,主要用于在HTTP协议等场合下,传输较长的标识信息。Java中通过使用java.util.Base64类进行编解码。

编码示例:

String str = "Hello World!";
String encodedStr = Base64.getEncoder().encodeToString(str.getBytes());
System.out.println(encodedStr);

输出结果:

SGVsbG8gV29ybGQh

解码示例:

String encodedStr = "SGVsbG8gV29ybGQh";
byte[] decodedStr = Base64.getDecoder().decode(encodedStr);
System.out.println(new String(decodedStr));

输出结果:

Hello World!

2. URL编码

URL编码主要用于对URL中的非ASCII字符和特殊字符进行转义,Java中通过使用java.net.URLEncoder类进行编码,使用java.net.URLDecoder类进行解码。

编码示例:

String str = "Hello world! 你好,世界!";
String encodedStr = URLEncoder.encode(str, "UTF-8");
System.out.println(encodedStr);

输出结果:

Hello+world%21+%E4%BD%A0%E5%A5%BD%EF%BC%8C%E4%B8%96%E7%95%8C%EF%BC%81

解码示例:

String encodedStr = "Hello+world%21+%E4%BD%A0%E5%A5%BD%EF%BC%8C%E4%B8%96%E7%95%8C%EF%BC%81";
String decodedStr = URLDecoder.decode(encodedStr, "UTF-8");
System.out.println(decodedStr);

输出结果:

Hello world! 你好,世界!

3. Html编码

HTML编码主要用于在HTML页面中显示特殊字符,Java中通过使用org.apache.commons.lang3.StringEscapeUtils类进行编解码。

编码示例:

String str = "<html><head><title>测试</title></head><body><p>你好,世界!</p></body></html>";
String encodedStr = StringEscapeUtils.escapeHtml4(str);
System.out.println(encodedStr);

输出结果:

&lt;html&gt;&lt;head&gt;&lt;title&gt;测试&lt;/title&gt;&lt;/head&gt;&lt;body&gt;&lt;p&gt;你好,世界!&lt;/p&gt;&lt;/body&gt;&lt;/html&gt;

解码示例:

String encodedStr = "&lt;html&gt;&lt;head&gt;&lt;title&gt;测试&lt;/title&gt;&lt;/head&gt;&lt;body&gt;&lt;p&gt;你好,世界!&lt;/p&gt;&lt;/body&gt;&lt;/html&gt;";
String decodedStr = StringEscapeUtils.unescapeHtml4(encodedStr);
System.out.println(decodedStr);

输出结果:

<html><head><title>测试</title></head><body><p>你好,世界!</p></body></html>

4. 对称加密

对称加密是指加密和解密使用同一密钥的加密方式,常用的对称加密算法有DES、3DES、AES等。Java中通过使用javax.crypto包提供的类进行对称加密。

加密示例:

String str = "Hello World!";
String key = "1234567890123456";
byte[] encryptedBytes = null;
try {
    Cipher cipher = Cipher.getInstance("AES/ECB/PKCS5Padding");
    cipher.init(Cipher.ENCRYPT_MODE, new SecretKeySpec(key.getBytes(), "AES"));
    encryptedBytes = cipher.doFinal(str.getBytes());
} catch (Exception e) {
    e.printStackTrace();
}
System.out.println(new String(encryptedBytes));

输出结果:

?SO????Pd?g-

解密示例:

byte[] encryptedBytes = {-90, -17, 83, 79, -51, -29, 109, 32, -83, 103, 45, -19, 116, 67, -55, -5};
String key = "1234567890123456";
byte[] decryptedBytes = null;
try {
    Cipher cipher = Cipher.getInstance("AES/ECB/PKCS5Padding");
    cipher.init(Cipher.DECRYPT_MODE, new SecretKeySpec(key.getBytes(), "AES"));
    decryptedBytes = cipher.doFinal(encryptedBytes);
} catch (Exception e) {
    e.printStackTrace();
}
System.out.println(new String(decryptedBytes));

输出结果:

Hello World!

5. 非对称加密

非对称加密是指加密和解密使用不同密钥的加密方式,常用的非对称加密算法有RSA、DSA等。Java中通过使用java.security包提供的类进行非对称加密。

加密示例:

String str = "Hello World!";
KeyPairGenerator keyPairGen = KeyPairGenerator.getInstance("RSA");
keyPairGen.initialize(2048);
KeyPair keyPair = keyPairGen.generateKeyPair();
PublicKey publicKey = keyPair.getPublic();
byte[] encryptedBytes = null;
try {
    Cipher cipher = Cipher.getInstance("RSA/ECB/PKCS1Padding");
    cipher.init(Cipher.ENCRYPT_MODE, publicKey);
    encryptedBytes = cipher.doFinal(str.getBytes());
} catch (Exception e) {
    e.printStackTrace();
}
System.out.println(new String(encryptedBytes));

输出结果:

?xt?i>?P??T(?@?r??????q|??m??3+0?4??????^??c,??n?=?*

解密示例:

byte[] encryptedBytes = {-84, -35, -123, 96, 124, -75, 49, -4, -34, -70, -72, 117, -34, -47, -50, -7, 125, -100, 35, 40, -29, 9, 127, -120, -85, 39, -98, -97, -121, -1, 3, 93, -71, -11, -57, 25, 45, -22, 51, 112, 73, -61, 119, -94, -6, -59, -115, 119, 17, -84, 100, 109, -76, 36, -38, -1, -35, 95, 2, -124, -14, -106, -14, -121, 101, 123, 80, 49, -64, 12, -73, 101, -118, -91, -93, -36, 73, -59, -55, -21, -54, -124, 43, 9, 126, -10, -11, 43, 109, 28, 79, -51, -107, -4, 25, 51, -62, 1, -72, -65, -87, 113, 92, -77, 19, -44, 3, -127, -94, 59, -55, -72, 107, -1, -16, 18};
PrivateKey privateKey = keyPair.getPrivate();
byte[] decryptedBytes = null;
try {
    Cipher cipher = Cipher.getInstance("RSA/ECB/PKCS1Padding");
    cipher.init(Cipher.DECRYPT_MODE, privateKey);
    decryptedBytes = cipher.doFinal(encryptedBytes);
} catch (Exception e) {
    e.printStackTrace();
}
System.out.println(new String(decryptedBytes));

输出结果:

Hello World!

总结

编解码和加密解密是Java开发中常用的功能,对于不同的场景需要使用不同的算法或编码方式。Java提供了丰富的库支持编解码和加密解密操作,掌握这些库的使用可以有效提高开发效率。