Java中的加密与解密函数及用法
Java中提供了多种加密和解密算法,主要包括对称加密算法、不对称加密算法和安全散列函数。在本篇文章中,我将会介绍常用的Java加密和解密函数及其用法。
一、对称加密算法
对称加密算法通常使用相同的密钥来进行加解密操作,包括DES、3DES、AES等。这些算法可以使用Java中的javax.crypto包中的类来进行加解密。常用的加密和解密函数包括:
1.1 DES加密和解密
DES加密和解密需要指定密钥和加密模式,其中加密模式包括ECB、CBC、CFB和OFB等。例如,“DES/CBC/PKCS5Padding”表示使用DES算法、CBC加密模式和PKCS5Padding填充方式。
加密函数:
public static byte[] desEncrypt(byte[] key, byte[] plaintext, String algorithm, String mode) throws Exception {
SecretKeySpec keySpec = new SecretKeySpec(key, algorithm);
Cipher cipher = Cipher.getInstance(algorithm + "/" + mode + "/PKCS5Padding");
byte[] iv = new byte[cipher.getBlockSize()];
IvParameterSpec ivSpec = new IvParameterSpec(iv);
cipher.init(Cipher.ENCRYPT_MODE, keySpec, ivSpec);
return cipher.doFinal(plaintext);
}
解密函数:
public static byte[] desDecrypt(byte[] key, byte[] ciphertext, String algorithm, String mode) throws Exception {
SecretKeySpec keySpec = new SecretKeySpec(key, algorithm);
Cipher cipher = Cipher.getInstance(algorithm + "/" + mode + "/PKCS5Padding");
byte[] iv = new byte[cipher.getBlockSize()];
IvParameterSpec ivSpec = new IvParameterSpec(iv);
cipher.init(Cipher.DECRYPT_MODE, keySpec, ivSpec);
return cipher.doFinal(ciphertext);
}
1.2 3DES加密和解密
3DES加密和解密可以使用DESede算法实现,也需要指定密钥和加密模式。
加密函数:
public static byte[] tripleDesEncrypt(byte[] key, byte[] plaintext, String algorithm, String mode) throws Exception {
SecretKeySpec keySpec = new SecretKeySpec(key, algorithm);
Cipher cipher = Cipher.getInstance(algorithm + "/" + mode + "/PKCS5Padding");
byte[] iv = new byte[cipher.getBlockSize()];
IvParameterSpec ivSpec = new IvParameterSpec(iv);
cipher.init(Cipher.ENCRYPT_MODE, keySpec, ivSpec);
return cipher.doFinal(plaintext);
}
解密函数:
public static byte[] tripleDesDecrypt(byte[] key, byte[] ciphertext, String algorithm, String mode) throws Exception {
SecretKeySpec keySpec = new SecretKeySpec(key, algorithm);
Cipher cipher = Cipher.getInstance(algorithm + "/" + mode + "/PKCS5Padding");
byte[] iv = new byte[cipher.getBlockSize()];
IvParameterSpec ivSpec = new IvParameterSpec(iv);
cipher.init(Cipher.DECRYPT_MODE, keySpec, ivSpec);
return cipher.doFinal(ciphertext);
}
1.3 AES加密和解密
AES加密和解密需要指定密钥和加密模式,其中加密模式包括ECB、CBC、CFB和OFB等。例如,“AES/CBC/PKCS5Padding”表示使用AES算法、CBC加密模式和PKCS5Padding填充方式。
加密函数:
public static byte[] aesEncrypt(byte[] key, byte[] plaintext, String algorithm, String mode) throws Exception {
SecretKeySpec keySpec = new SecretKeySpec(key, algorithm);
Cipher cipher = Cipher.getInstance(algorithm + "/" + mode + "/PKCS5Padding");
byte[] iv = new byte[cipher.getBlockSize()];
IvParameterSpec ivSpec = new IvParameterSpec(iv);
cipher.init(Cipher.ENCRYPT_MODE, keySpec, ivSpec);
return cipher.doFinal(plaintext);
}
解密函数:
public static byte[] aesDecrypt(byte[] key, byte[] ciphertext, String algorithm, String mode) throws Exception {
SecretKeySpec keySpec = new SecretKeySpec(key, algorithm);
Cipher cipher = Cipher.getInstance(algorithm + "/" + mode + "/PKCS5Padding");
byte[] iv = new byte[cipher.getBlockSize()];
IvParameterSpec ivSpec = new IvParameterSpec(iv);
cipher.init(Cipher.DECRYPT_MODE, keySpec, ivSpec);
return cipher.doFinal(ciphertext);
}
二、不对称加密算法
不对称加密算法中包括公钥加密和私钥解密,或私钥加密和公钥解密两种方式,如RSA算法。Java中可以使用java.security包中的类来进行加解密操作。常用的加密和解密函数包括:
2.1 RSA加密和解密
RSA加密和解密需要生成公钥和私钥,然后使用公钥加密、私钥解密或者私钥加密、公钥解密来进行加解密操作。
生成密钥对:
public static KeyPair generateKeyPair() throws Exception {
KeyPairGenerator keyPairGenerator = KeyPairGenerator.getInstance("RSA");
SecureRandom secureRandom = new SecureRandom();
keyPairGenerator.initialize(2048, secureRandom);
return keyPairGenerator.generateKeyPair();
}
公钥加密:
public static byte[] rsaEncrypt(byte[] publicKey, byte[] plaintext) throws Exception {
X509EncodedKeySpec x509KeySpec = new X509EncodedKeySpec(publicKey);
KeyFactory keyFactory = KeyFactory.getInstance("RSA");
PublicKey key = keyFactory.generatePublic(x509KeySpec);
Cipher cipher = Cipher.getInstance("RSA/ECB/PKCS1Padding");
cipher.init(Cipher.ENCRYPT_MODE, key);
return cipher.doFinal(plaintext);
}
私钥解密:
public static byte[] rsaDecrypt(byte[] privateKey, byte[] ciphertext) throws Exception {
PKCS8EncodedKeySpec pkcs8KeySpec = new PKCS8EncodedKeySpec(privateKey);
KeyFactory keyFactory = KeyFactory.getInstance("RSA");
PrivateKey key = keyFactory.generatePrivate(pkcs8KeySpec);
Cipher cipher = Cipher.getInstance("RSA/ECB/PKCS1Padding");
cipher.init(Cipher.DECRYPT_MODE, key);
return cipher.doFinal(ciphertext);
}
私钥加密:
public static byte[] rsaEncrypt(byte[] privateKey, byte[] plaintext) throws Exception {
PKCS8EncodedKeySpec pkcs8KeySpec = new PKCS8EncodedKeySpec(privateKey);
KeyFactory keyFactory = KeyFactory.getInstance("RSA");
PrivateKey key = keyFactory.generatePrivate(pkcs8KeySpec);
Cipher cipher = Cipher.getInstance("RSA/ECB/PKCS1Padding");
cipher.init(Cipher.ENCRYPT_MODE, key);
return cipher.doFinal(plaintext);
}
公钥解密:
public static byte[] rsaDecrypt(byte[] publicKey, byte[] ciphertext) throws Exception {
X509EncodedKeySpec x509KeySpec = new X509EncodedKeySpec(publicKey);
KeyFactory keyFactory = KeyFactory.getInstance("RSA");
PublicKey key = keyFactory.generatePublic(x509KeySpec);
Cipher cipher = Cipher.getInstance("RSA/ECB/PKCS1Padding");
cipher.init(Cipher.DECRYPT_MODE, key);
return cipher.doFinal(ciphertext);
}
三、安全散列函数
安全散列函数包括MD5、SHA-1、SHA-256、SHA-384和SHA-512等,主要用于数据的哈希。
3.1 MD5和SHA-1哈希
Java中可以使用java.security包中的类来进行MD5和SHA-1哈希算法的运算。
MD5哈希函数:
public static byte[] md5Digest(byte[] input) throws Exception {
MessageDigest digest = MessageDigest.getInstance("MD5");
return digest.digest(input);
}
SHA-1哈希函数:
public static byte[] sha1Digest(byte[] input) throws Exception {
MessageDigest digest = MessageDigest.getInstance("SHA-1");
return digest.digest(input);
}
3.2 SHA-256、SHA-384和SHA-512哈希
Java中可以使用java.security包中的类来进行SHA-256、SHA-384和SHA-512哈希算法的运算。
SHA-256哈希函数:
public static byte[] sha256Digest(byte[] input) throws Exception {
MessageDigest digest = MessageDigest.getInstance("SHA-256");
return digest.digest(input);
}
SHA-384哈希函数:
`java
public static byte[] sha384Digest(byte[] input) throws Exception {
MessageDigest digest = MessageDigest.getInstance("SHA-384");
return digest.digest(input);
