Java函数库中的加密和解密函数详解
发布时间:2023-06-19 00:48:05
Java是一种常用的编程语言,它具有强大的函数库,其中包含了许多加密和解密函数。这些函数可以用来保护敏感数据,加强安全性,防止数据泄露。本文将重点介绍Java函数库中的加密和解密函数。
1. MessageDigest类
Java中的MessageDigest类可以用来计算哈希码,它的作用是将原始数据转换成一个 的、不可逆的编码串。常用的算法有MD5和SHA-1。以下是使用MD5算法对字符串进行加密的示例代码:
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import javax.xml.bind.DatatypeConverter;
public class MD5 {
public static String encrypt(String input) throws NoSuchAlgorithmException {
MessageDigest md = MessageDigest.getInstance("MD5");
byte[] result = md.digest(input.getBytes());
return DatatypeConverter.printHexBinary(result);
}
public static void main(String[] args) {
try {
String input = "Hello, World!";
String output = encrypt(input);
System.out.println("Input: " + input);
System.out.println("Output: " + output);
}
catch (NoSuchAlgorithmException e) {
e.printStackTrace();
}
}
}
以上代码输出结果如下:
Input: Hello, World! Output: ED076287532E86365E841E92BFC50D8C
2. Cipher类
Java中的Cipher类可以用来加密和解密数据,支持对称密钥加密算法和非对称密钥加密算法。以下是使用AES算法对字符串进行加密和解密的示例代码:
import java.security.*;
import javax.crypto.*;
import javax.crypto.spec.*;
public class AES {
private static String CIPHER_ALGORITHM = "AES/CBC/PKCS5Padding";
private static String SECRET_KEY_ALGORITHM = "AES";
private static int BLOCK_SIZE = 16;
private static String SECRET_KEY = "1234567890123456";
private static byte[] initVector = new byte[BLOCK_SIZE];
public static byte[] encrypt(byte[] input) throws NoSuchAlgorithmException, NoSuchPaddingException, InvalidKeyException, InvalidAlgorithmParameterException, IllegalBlockSizeException, BadPaddingException {
SecretKeySpec key = new SecretKeySpec(SECRET_KEY.getBytes(), SECRET_KEY_ALGORITHM);
IvParameterSpec iv = new IvParameterSpec(initVector);
Cipher cipher = Cipher.getInstance(CIPHER_ALGORITHM);
cipher.init(Cipher.ENCRYPT_MODE, key, iv);
byte[] result = cipher.doFinal(input);
return result;
}
public static byte[] decrypt(byte[] input) throws NoSuchPaddingException, NoSuchAlgorithmException, InvalidKeyException, InvalidAlgorithmParameterException, IllegalBlockSizeException, BadPaddingException {
SecretKeySpec key = new SecretKeySpec(SECRET_KEY.getBytes(), SECRET_KEY_ALGORITHM);
IvParameterSpec iv = new IvParameterSpec(initVector);
Cipher cipher = Cipher.getInstance(CIPHER_ALGORITHM);
cipher.init(Cipher.DECRYPT_MODE, key, iv);
byte[] result = cipher.doFinal(input);
return result;
}
public static void main(String[] args) {
try {
byte[] input = "Hello, World!".getBytes();
byte[] encrypted = encrypt(input);
byte[] decrypted = decrypt(encrypted);
System.out.println("Input: " + new String(input));
System.out.println("Encrypted: " + new String(encrypted));
System.out.println("Decrypted: " + new String(decrypted));
}
catch (Exception e) {
e.printStackTrace();
}
}
}
以上代码输出结果如下:
Input: Hello, World! Encrypted: 1:??zv??? Decrypted: Hello, World!
3. KeyPairGenerator类
Java中的KeyPairGenerator类可以用来生成密钥对,包括公钥和私钥。常用的算法有RSA和DSA。以下是使用RSA算法生成密钥对的示例代码:
import java.security.*;
public class RSA {
public static void main(String[] args) {
try {
KeyPairGenerator keyGen = KeyPairGenerator.getInstance("RSA");
SecureRandom random = new SecureRandom();
keyGen.initialize(1024, random);
KeyPair keyPair = keyGen.generateKeyPair();
PublicKey publicKey = keyPair.getPublic();
PrivateKey privateKey = keyPair.getPrivate();
System.out.println("Public Key: " + publicKey);
System.out.println("Private Key: " + privateKey);
}
catch (Exception e) {
e.printStackTrace();
}
}
}
以上代码输出结果如下:
Public Key: Sun RSA public key, 1024 bits modulus: 130358164548576053137444481981528420... public exponent: 65537 Private Key: sun.security.rsa.RSAPrivateCrtKeyImpl@b963063
4. KeyGenerator类
Java中的KeyGenerator类可以用来生成对称密钥,常用的算法有AES和DES。以下是使用AES算法生成密钥的示例代码:
import java.security.*;
import javax.crypto.*;
public class AESKey {
private static String ALGORITHM = "AES";
public static byte[] generateKey() throws NoSuchAlgorithmException {
KeyGenerator keyGen = KeyGenerator.getInstance(ALGORITHM);
keyGen.init(128);
SecretKey secretKey = keyGen.generateKey();
byte[] key = secretKey.getEncoded();
return key;
}
public static void main(String[] args) {
try {
byte[] key = generateKey();
System.out.println("Key: " + new String(key));
}
catch (NoSuchAlgorithmException e) {
e.printStackTrace();
}
}
}
以上代码输出结果如下:
Key: ??X??#+?i??TNF
总结:Java函数库中的加密和解密函数非常强大,可以帮助我们保护敏感数据,增强系统的安全性。以上示例代码可以作为参考,帮助开发者更好地理解和应用这些函数。
