Java中加密解密函数详解
发布时间:2023-06-21 10:15:34
Java中加密与解密是非常重要的基础性工具之一,主要用于保护敏感数据,防止数据泄漏或劫持等问题。在常见的加密方法中,对称加密和非对称加密是最常用的两种方法。接下来,我将详细介绍Java中常用的加密和解密函数。
一、对称加密
对称加密法是一种加密方法,其加密过程和解密过程均使用相同的密钥。常见的对称加密算法有DES、AES和IDEA等。
1. DES加密解密方法
DES加密算法使用一个固定的密码,将原文加密成密文,并且通过密钥解密。下面是DES加密的Java代码实现:
import javax.crypto.Cipher;
import javax.crypto.spec.SecretKeySpec;
public class DESEncrypt {
private static final String Algorithm = "DES";
private static byte[] keyBytes = new byte[] { 0x11, 0x22, 0x4F, 0x58,
(byte) 0x88, 0x10, 0x40, 0x38 };
/**
* 加密函数
* @param content
* @return
* @throws Exception
*/
public static byte[] encrypt(byte[] content) throws Exception {
SecretKeySpec key = new SecretKeySpec(keyBytes, Algorithm);
Cipher cipher = Cipher.getInstance(Algorithm);
cipher.init(Cipher.ENCRYPT_MODE, key);
byte[] result = cipher.doFinal(content);
return result;
}
/**
* 解密函数
* @param content
* @return
* @throws Exception
*/
public static byte[] decrypt(byte[] content) throws Exception {
SecretKeySpec key = new SecretKeySpec(keyBytes, Algorithm);
Cipher cipher = Cipher.getInstance(Algorithm);
cipher.init(Cipher.DECRYPT_MODE, key);
byte[] result = cipher.doFinal(content);
return result;
}
}
2. AES加密解密方法
AES是一种可替代DES的高级加密标准,通常用于加密大块数据。AES具有较高的安全性和效率,因此在许多应用程序中都在使用。以下是AES加密和解密的Java代码实现:
import javax.crypto.Cipher;
import javax.crypto.spec.SecretKeySpec;
import java.security.MessageDigest;
public class AESEncrypt {
private static final String Algorithm = "AES/ECB/PKCS5Padding";
private static byte[] keyBytes;
/**
* AES加密函数
* @param content
* @param password
* @return
* @throws Exception
*/
public static byte[] encrypt(String content, String password) throws Exception {
byte[] contentBytes = content.getBytes("utf-8");
MessageDigest md5 = MessageDigest.getInstance("md5");
byte[] key = md5.digest(password.getBytes("utf-8"));
keyBytes = new byte[16];
System.arraycopy(key, 0, keyBytes, 0, 16);
SecretKeySpec keySpec = new SecretKeySpec(keyBytes, "AES");
Cipher cipher = Cipher.getInstance(Algorithm);
cipher.init(Cipher.ENCRYPT_MODE, keySpec);
byte[] result = cipher.doFinal(contentBytes);
return result;
}
/**
* AES解密函数
* @param content
* @param password
* @return
* @throws Exception
*/
public static String decrypt(byte[] content, String password) throws Exception {
MessageDigest md5 = MessageDigest.getInstance("md5");
byte[] key = md5.digest(password.getBytes("utf-8"));
keyBytes = new byte[16];
System.arraycopy(key, 0, keyBytes, 0, 16);
SecretKeySpec keySpec = new SecretKeySpec(keyBytes, "AES");
Cipher cipher = Cipher.getInstance(Algorithm);
cipher.init(Cipher.DECRYPT_MODE, keySpec);
byte[] result = cipher.doFinal(content);
String decryptedValue = new String(result, "utf-8");
return decryptedValue;
}
}
二、非对称加密
非对称加密是指加密和解密使用不同的密钥,公钥用于加密,私钥用于解密。在常见的非对称加密方法中,RSA是最常用的一种。
1. RSA加密解密方法
RSA加密算法是一种非对称密钥加密方法。在RSA加密过程中,用于加密的公钥是可公开的,而只有私钥持有人才能解密。以下是RSA加密和解密的Java代码实现:
import javax.crypto.Cipher;
import java.security.KeyPair;
import java.security.KeyPairGenerator;
public class RSAEncrypt {
/**
* 生成RSA公钥和私钥
* @return
* @throws Exception
*/
public static KeyPair generateRSAKeyPair() throws Exception {
KeyPairGenerator kpGen = KeyPairGenerator.getInstance("RSA");
kpGen.initialize(1024);
KeyPair kp = kpGen.generateKeyPair();
return kp;
}
/**
* RSA加密函数
* @param data 需要加密的数据
* @param publicKey RSA公钥
* @return
* @throws Exception
*/
public static byte[] encrypt(byte[] data, byte[] publicKey) throws Exception {
Cipher cipher = Cipher.getInstance("RSA");
cipher.init(Cipher.ENCRYPT_MODE, RSAUtil.getPublicKey(publicKey));
byte[] cipherData = cipher.doFinal(data);
return cipherData;
}
/**
* RSA解密函数
* @param data 需要解密的数据
* @param privateKey RSA私钥
* @return
* @throws Exception
*/
public static byte[] decrypt(byte[] data, byte[] privateKey) throws Exception {
Cipher cipher = Cipher.getInstance("RSA");
cipher.init(Cipher.DECRYPT_MODE, RSAUtil.getPrivateKey(privateKey));
byte[] plainData = cipher.doFinal(data);
return plainData;
}
}
以上就是Java中常用的加密和解密函数的详细介绍。在实际应用中,我们需要根据具体需要选择不同的加密方法,以达到更好的保护数据和确保数据的安全性。
