在Java中如何加密解密字符串?
发布时间:2023-06-01 08:13:39
Java提供了多种方法实现字符串加密解密。常见的加密算法包括对称加密算法和非对称加密算法。对称加密算法采用相同的密钥进行加密和解密,加密与解密速度快,但安全性不高。而非对称加密算法采用一对公私钥进行加密及解密,其中一个公钥用于加密数据,另一个私钥用于解密数据,安全性高但速度较慢。
下面介绍几种常用的加密解密字符串的方式:
1. 使用MD5加密算法
MD5算法是一种常用的哈希散列函数,它将任意长度的信息压缩成128位(16字节)的字节串。Java中可以通过MessageDigest类的getInstance方法来获得MD5实例。下面给出加密字符串的代码:
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
public class MD5Util {
public static String encrypt(String sourceStr) {
String result = "";
try {
MessageDigest md = MessageDigest.getInstance("MD5");
byte[] bytes = md.digest(sourceStr.getBytes());
result = byteArrayToHexString(bytes);
} catch (NoSuchAlgorithmException e) {
e.printStackTrace();
}
return result;
}
private static String byteArrayToHexString(byte[] bytes) {
StringBuilder sb = new StringBuilder();
for (byte b : bytes) {
String hex = Integer.toHexString(b & 0xff);
if (hex.length() == 1) {
sb.append("0");
}
sb.append(hex);
}
return sb.toString();
}
}
2. 使用AES加密算法
AES是一种高级加密标准,是一种对称加密算法。Java中可以采用javax.crypto包提供的AES加密算法,使用时需要提供密钥长度、填充模式和加密模式。下面给出AES加密算法的代码:
import javax.crypto.Cipher;
import javax.crypto.spec.SecretKeySpec;
import java.util.Base64;
public class AESUtil {
private static final String AES = "AES";
public static String encrypt(String input, String key) throws Exception {
byte[] inputBytes = input.getBytes();
SecretKeySpec keySpec = new SecretKeySpec(key.getBytes(), AES);
Cipher cipher = Cipher.getInstance(AES);
cipher.init(Cipher.ENCRYPT_MODE, keySpec);
byte[] encryptedBytes = cipher.doFinal(inputBytes);
return Base64.getEncoder().encodeToString(encryptedBytes);
}
public static String decrypt(String encrypted, String key) throws Exception {
byte[] encryptedBytes = Base64.getDecoder().decode(encrypted);
SecretKeySpec keySpec = new SecretKeySpec(key.getBytes(), AES);
Cipher cipher = Cipher.getInstance(AES);
cipher.init(Cipher.DECRYPT_MODE, keySpec);
byte[] decryptedBytes = cipher.doFinal(encryptedBytes);
return new String(decryptedBytes);
}
}
3. 使用RSA加密算法
RSA是一种非对称加密算法,可以用于密钥交换和数字签名。Java中可以使用java.security包提供的RSA实现,其中公钥和私钥分别使用KeyPairGenerator和KeyPair的generateKeyPair()方法生成。下面给出RSA加密算法的代码:
import javax.crypto.Cipher;
import java.security.KeyPair;
import java.security.KeyPairGenerator;
import java.security.PrivateKey;
import java.security.PublicKey;
import java.util.Base64;
public class RSAUtil {
private static final String RSA = "RSA";
public static KeyPair generateKeyPair() throws Exception {
KeyPairGenerator generator = KeyPairGenerator.getInstance(RSA);
generator.initialize(2048);
return generator.generateKeyPair();
}
public static String encrypt(String input, PublicKey publicKey) throws Exception {
byte[] inputBytes = input.getBytes();
Cipher cipher = Cipher.getInstance(RSA);
cipher.init(Cipher.ENCRYPT_MODE, publicKey);
byte[] encryptedBytes = cipher.doFinal(inputBytes);
return Base64.getEncoder().encodeToString(encryptedBytes);
}
public static String decrypt(String encrypted, PrivateKey privateKey) throws Exception {
byte[] encryptedBytes = Base64.getDecoder().decode(encrypted);
Cipher cipher = Cipher.getInstance(RSA);
cipher.init(Cipher.DECRYPT_MODE, privateKey);
byte[] decryptedBytes = cipher.doFinal(encryptedBytes);
return new String(decryptedBytes);
}
}
在实际应用中,需要根据具体的加密需求选择合适的加密算法,同时注意加密过程中出现的异常和安全性问题。
