在Java中实现加解密函数
发布时间:2023-08-21 09:04:05
在Java中,可以使用多种算法来实现加解密函数,例如对称加密算法、非对称加密算法和哈希算法等。
1. 对称加密算法:
对称加密算法使用相同的密钥进行加密和解密,常用的对称加密算法有DES、AES、RC4等。
例如,使用AES算法实现加解密函数的代码如下:
import javax.crypto.Cipher;
import javax.crypto.spec.SecretKeySpec;
import java.nio.charset.StandardCharsets;
import java.util.Base64;
public class AESUtil {
private static final String AES_ALGORITHM = "AES/ECB/PKCS5Padding";
public static String encrypt(String plainText, String secretKey) {
try {
Cipher cipher = Cipher.getInstance(AES_ALGORITHM);
SecretKeySpec keySpec = new SecretKeySpec(secretKey.getBytes(StandardCharsets.UTF_8), "AES");
cipher.init(Cipher.ENCRYPT_MODE, keySpec);
byte[] encryptedBytes = cipher.doFinal(plainText.getBytes(StandardCharsets.UTF_8));
return Base64.getEncoder().encodeToString(encryptedBytes);
} catch (Exception e) {
e.printStackTrace();
}
return null;
}
public static String decrypt(String encryptedText, String secretKey) {
try {
Cipher cipher = Cipher.getInstance(AES_ALGORITHM);
SecretKeySpec keySpec = new SecretKeySpec(secretKey.getBytes(StandardCharsets.UTF_8), "AES");
cipher.init(Cipher.DECRYPT_MODE, keySpec);
byte[] encryptedBytes = Base64.getDecoder().decode(encryptedText);
byte[] decryptedBytes = cipher.doFinal(encryptedBytes);
return new String(decryptedBytes, StandardCharsets.UTF_8);
} catch (Exception e) {
e.printStackTrace();
}
return null;
}
}
2. 非对称加密算法:
非对称加密算法使用公钥进行加密,私钥进行解密。常用的非对称加密算法有RSA、DSA等。
例如,使用RSA算法实现加解密函数的代码如下:
import javax.crypto.Cipher;
import java.nio.charset.StandardCharsets;
import java.security.KeyPair;
import java.security.KeyPairGenerator;
import java.security.PrivateKey;
import java.security.PublicKey;
public class RSAUtil {
private static final String RSA_ALGORITHM = "RSA";
public static KeyPair generateKeyPair(int keySize) {
try {
KeyPairGenerator keyPairGenerator = KeyPairGenerator.getInstance(RSA_ALGORITHM);
keyPairGenerator.initialize(keySize);
return keyPairGenerator.generateKeyPair();
} catch (Exception e) {
e.printStackTrace();
}
return null;
}
public static byte[] encrypt(byte[] plainBytes, PublicKey publicKey) {
try {
Cipher cipher = Cipher.getInstance(RSA_ALGORITHM);
cipher.init(Cipher.ENCRYPT_MODE, publicKey);
return cipher.doFinal(plainBytes);
} catch (Exception e) {
e.printStackTrace();
}
return null;
}
public static byte[] decrypt(byte[] encryptedBytes, PrivateKey privateKey) {
try {
Cipher cipher = Cipher.getInstance(RSA_ALGORITHM);
cipher.init(Cipher.DECRYPT_MODE, privateKey);
return cipher.doFinal(encryptedBytes);
} catch (Exception e) {
e.printStackTrace();
}
return null;
}
}
3. 哈希算法:
哈希算法将任意长度的数据转换成固定长度的哈希值,常用的哈希算法有MD5、SHA-1、SHA-256等。
例如,使用MD5算法实现哈希函数的代码如下:
import java.nio.charset.StandardCharsets;
import java.security.MessageDigest;
public class MD5Util {
private static final String MD5_ALGORITHM = "MD5";
public static String hash(String input) {
try {
MessageDigest md = MessageDigest.getInstance(MD5_ALGORITHM);
byte[] hashBytes = md.digest(input.getBytes(StandardCharsets.UTF_8));
StringBuilder sb = new StringBuilder();
for (byte b : hashBytes) {
sb.append(Integer.toHexString((b >> 4) & 0xF));
sb.append(Integer.toHexString(b & 0xF));
}
return sb.toString();
} catch (Exception e) {
e.printStackTrace();
}
return null;
}
}
需要注意的是,以上代码仅为示例,实际使用时可能还需要考虑数据的编码、填充方式、密钥的生成和管理等因素。
