Java中的加密函数使用说明
Java中的加密函数主要用于保护数据的安全性,包括对数据的加密和解密操作。下面将对Java中常用的加密函数进行详细的使用说明。
常用的Java加密函数主要有以下几种:对称加密、非对称加密和哈希算法。
1. 对称加密:
对称加密使用同一个密钥进行加密和解密,常用的对称加密算法有DES、AES和RC4等。
- DES加密函数使用示例:
import javax.crypto.Cipher;
import javax.crypto.KeyGenerator;
import javax.crypto.SecretKey;
import javax.crypto.spec.SecretKeySpec;
import java.util.Base64;
public class DesEncryptUtils {
public static String encrypt(String data, String key) throws Exception {
SecretKey secretKey = generateKey(key);
Cipher cipher = Cipher.getInstance("DES");
cipher.init(Cipher.ENCRYPT_MODE, secretKey);
byte[] encryptedBytes = cipher.doFinal(data.getBytes());
return Base64.getEncoder().encodeToString(encryptedBytes);
}
public static String decrypt(String encryptedData, String key) throws Exception {
SecretKey secretKey = generateKey(key);
Cipher cipher = Cipher.getInstance("DES");
cipher.init(Cipher.DECRYPT_MODE, secretKey);
byte[] decryptedBytes = cipher.doFinal(Base64.getDecoder().decode(encryptedData));
return new String(decryptedBytes);
}
private static SecretKey generateKey(String key) throws Exception {
KeyGenerator keyGenerator = KeyGenerator.getInstance("DES");
keyGenerator.init(56);
SecretKey secretKey = keyGenerator.generateKey();
byte[] encodedKey = Base64.getDecoder().decode(Base64.getEncoder().encodeToString(secretKey.getEncoded()));
return new SecretKeySpec(encodedKey, "DES");
}
}
使用示例:
public class Main {
public static void main(String[] args) throws Exception {
String originalData = "Hello, world!";
String key = "aaaabbbbccccdddd";
String encryptedData = DesEncryptUtils.encrypt(originalData, key);
System.out.println("Encrypted data: " + encryptedData);
String decryptedData = DesEncryptUtils.decrypt(encryptedData, key);
System.out.println("Decrypted data: " + decryptedData);
}
}
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;
import java.util.Base64;
public class RsaEncryptUtils {
public static String encrypt(String data, PublicKey publicKey) throws Exception {
Cipher cipher = Cipher.getInstance("RSA");
cipher.init(Cipher.ENCRYPT_MODE, publicKey);
byte[] encryptedBytes = cipher.doFinal(data.getBytes());
return Base64.getEncoder().encodeToString(encryptedBytes);
}
public static String decrypt(String encryptedData, PrivateKey privateKey) throws Exception {
Cipher cipher = Cipher.getInstance("RSA");
cipher.init(Cipher.DECRYPT_MODE, privateKey);
byte[] decryptedBytes = cipher.doFinal(Base64.getDecoder().decode(encryptedData));
return new String(decryptedBytes, StandardCharsets.UTF_8);
}
public static KeyPair generateKeyPair() throws Exception {
KeyPairGenerator keyPairGenerator = KeyPairGenerator.getInstance("RSA");
keyPairGenerator.initialize(2048);
return keyPairGenerator.generateKeyPair();
}
}
使用示例:
public class Main {
public static void main(String[] args) throws Exception {
String originalData = "Hello, world!";
KeyPair keyPair = RsaEncryptUtils.generateKeyPair();
PublicKey publicKey = keyPair.getPublic();
PrivateKey privateKey = keyPair.getPrivate();
String encryptedData = RsaEncryptUtils.encrypt(originalData, publicKey);
System.out.println("Encrypted data: " + encryptedData);
String decryptedData = RsaEncryptUtils.decrypt(encryptedData, privateKey);
System.out.println("Decrypted data: " + decryptedData);
}
}
3. 哈希算法:
哈希算法用于生成数据的摘要,常用的哈希算法有MD5、SHA-1和SHA-256等。
- MD5哈希函数使用示例:
import java.math.BigInteger;
import java.security.MessageDigest;
public class Md5HashUtils {
public static String hash(String data) throws Exception {
MessageDigest md = MessageDigest.getInstance("MD5");
byte[] messageDigest = md.digest(data.getBytes());
BigInteger no = new BigInteger(1, messageDigest);
StringBuilder hashText = new StringBuilder(no.toString(16));
while (hashText.length() < 32) {
hashText.insert(0, "0");
}
return hashText.toString();
}
}
使用示例:
public class Main {
public static void main(String[] args) throws Exception {
String originalData = "Hello, world!";
String hashText = Md5HashUtils.hash(originalData);
System.out.println("Hash text: " + hashText);
}
}
以上是Java中常用的加密函数的使用说明,根据需求选择合适的加密算法来保护数据的安全性。
