使用Java函数实现加密解密
加密和解密是信息安全领域中非常重要的两个环节,任何一种加密算法都需要提供一个加密函数和一个解密函数。Java 是市场上 的编程语言之一,并且自带了很多加密解密的函数,本文将介绍 Java 中常见的加密解密函数,包括 Base64、MD5、SHA-1、DES、AES 等。
1. Base64 加密解密
Base64 是用来将二进制数据编码成 ASCII 字符的一种方法,常用于传输邮件、网页等文本协议消息。在 Java 中,可以使用 sun.misc.BASE64Encoder 和 sun.misc.BASE64Decoder 来实现 Base64 加密和解密。
加密代码如下:
import sun.misc.BASE64Encoder;
public class Base64Demo {
public static void main(String[] args) throws Exception {
String str = "Hello World!";
BASE64Encoder encoder = new BASE64Encoder();
String encodedStr = encoder.encode(str.getBytes());
System.out.println("Encoded String: " + encodedStr);
}
}
解密代码如下:
import sun.misc.BASE64Decoder;
public class Base64Demo {
public static void main(String[] args) throws Exception {
String str = "SGVsbG8gV29ybGQh";
BASE64Decoder decoder = new BASE64Decoder();
byte[] decodedBytes = decoder.decodeBuffer(str);
System.out.println("Decoded String: " + new String(decodedBytes));
}
}
2. MD5 加密
MD5 是一种常见的摘要算法,作用是将任意长度的消息转换成固定长度的摘要(通常为128位),主要用于验证数据的完整性和一致性。在 Java 中,可以使用 java.security.MessageDigest 类来实现 MD5 加密,调用其 digest() 函数即可。
import java.security.MessageDigest;
public class MD5Demo {
public static void main(String[] args) throws Exception {
String password = "123456";
MessageDigest digest = MessageDigest.getInstance("MD5");
byte[] encodedBytes = digest.digest(password.getBytes());
StringBuffer sb = new StringBuffer();
for (int i = 0; i < encodedBytes.length; i++) {
String hex = Integer.toHexString(0xff & encodedBytes[i]);
if (hex.length() == 1) {
sb.append('0');
}
sb.append(hex);
}
System.out.println("MD5: " + sb.toString());
}
}
3. SHA-1 加密
SHA-1 比 MD5 更安全,生成的摘要长度为160位。在 Java 中,可以使用 java.security.MessageDigest 类来实现 SHA-1加密,调用其 digest() 函数即可。
import java.security.MessageDigest;
public class SHA1Demo {
public static void main(String[] args) throws Exception {
String password = "123456";
MessageDigest digest = MessageDigest.getInstance("SHA-1");
byte[] encodedBytes = digest.digest(password.getBytes());
StringBuffer sb = new StringBuffer();
for (int i = 0; i < encodedBytes.length; i++) {
String hex = Integer.toHexString(0xff & encodedBytes[i]);
if (hex.length() == 1) {
sb.append('0');
}
sb.append(hex);
}
System.out.println("SHA-1: " + sb.toString());
}
}
4. DES 加密解密
DES 是一种对称加密算法,加密和解密使用的是同一组密钥,需要确保密钥的安全性。在 Java 中,可以使用 javax.crypto 包中的 Cipher 类来实现 DES 加密和解密,需要指定加密模式和填充模式。
加密代码如下:
import javax.crypto.Cipher;
import javax.crypto.spec.SecretKeySpec;
public class DESDemo {
public static void main(String[] args) throws Exception {
String key = "12345678";
String plainText = "Hello World!";
SecretKeySpec secretKeySpec = new SecretKeySpec(key.getBytes(), "DES");
Cipher cipher = Cipher.getInstance("DES/ECB/PKCS5Padding");
cipher.init(Cipher.ENCRYPT_MODE, secretKeySpec);
byte[] encryptedBytes = cipher.doFinal(plainText.getBytes());
System.out.println("Encrypted String: " + new String(encryptedBytes));
}
}
解密代码如下:
import javax.crypto.Cipher;
import javax.crypto.spec.SecretKeySpec;
public class DESDemo {
public static void main(String[] args) throws Exception {
String key = "12345678";
String encryptedText = "sXGEjK0igzg=";
SecretKeySpec secretKeySpec = new SecretKeySpec(key.getBytes(), "DES");
Cipher cipher = Cipher.getInstance("DES/ECB/PKCS5Padding");
cipher.init(Cipher.DECRYPT_MODE, secretKeySpec);
byte[] decryptedBytes = cipher.doFinal(Base64.getDecoder().decode(encryptedText));
System.out.println("Decrypted String: " + new String(decryptedBytes));
}
}
5. AES 加密解密
AES 是一种高级加密标准,比 DES 更安全,密钥长度可以为128位、192位或256位。在 Java 中,也可以使用 javax.crypto 包中的 Cipher 类来实现 AES 加密和解密,需要指定加密模式、填充模式和密钥长度。
加密代码如下:
import javax.crypto.Cipher;
import javax.crypto.spec.SecretKeySpec;
import java.util.Base64;
public class AESDemo {
public static void main(String[] args) throws Exception {
String key = "1234567812345678";
String plainText = "Hello World!";
SecretKeySpec secretKeySpec = new SecretKeySpec(key.getBytes(), "AES");
Cipher cipher = Cipher.getInstance("AES/ECB/PKCS5Padding");
cipher.init(Cipher.ENCRYPT_MODE, secretKeySpec);
byte[] encryptedBytes = cipher.doFinal(plainText.getBytes());
System.out.println("Encrypted String: " + Base64.getEncoder().encodeToString(encryptedBytes));
}
}
解密代码如下:
import javax.crypto.Cipher;
import javax.crypto.spec.SecretKeySpec;
import java.util.Base64;
public class AESDemo {
public static void main(String[] args) throws Exception {
String key = "1234567812345678";
String encryptedText = "PITzGKUDhbqVwFOinWWkOw==";
SecretKeySpec secretKeySpec = new SecretKeySpec(key.getBytes(), "AES");
Cipher cipher = Cipher.getInstance("AES/ECB/PKCS5Padding");
cipher.init(Cipher.DECRYPT_MODE, secretKeySpec);
byte[] decryptedBytes = cipher.doFinal(Base64.getDecoder().decode(encryptedText));
System.out.println("Decrypted String: " + new String(decryptedBytes));
}
}
这些加密解密函数都很方便易用,但是需要注意的是,密钥的安全性是非常重要的,密钥的泄露可能会导致信息的严重损失。在实际应用中,需要采取合适的密钥管理策略,保护密钥的安全性,增强加密算法的安全性。
