Java中如何使用函数实现简单的加密和解密功能?
发布时间:2023-06-25 09:46:47
在Java中,使用函数实现简单的加密和解密功能可以采用常见的加密算法,例如:对称加密算法和非对称加密算法。
对称加密算法是指加密和解密使用同一个密钥的加密算法,这种加密算法的实现很简单,可以使用Java中提供的一些库来实现。
1.对称加密
(1)使用Java的AES加密算法
在Java中,可以使用AES加密算法来实现对称加密,具体方法如下:
import java.security.SecureRandom;
import javax.crypto.Cipher;
import javax.crypto.KeyGenerator;
import javax.crypto.SecretKey;
import javax.crypto.spec.SecretKeySpec;
public class AESEncryption {
public static byte[] encrypt(String data, String key) throws Exception {
KeyGenerator keyGenerator = KeyGenerator.getInstance("AES");
SecureRandom secureRandom = SecureRandom.getInstance("SHA1PRNG");
secureRandom.setSeed(key.getBytes());
keyGenerator.init(128, secureRandom);
SecretKey secretKey = keyGenerator.generateKey();
byte[] keyBytes = secretKey.getEncoded();
SecretKeySpec secretKeySpec = new SecretKeySpec(keyBytes, "AES");
Cipher cipher = Cipher.getInstance("AES/ECB/PKCS5Padding");
cipher.init(Cipher.ENCRYPT_MODE, secretKeySpec);
return cipher.doFinal(data.getBytes("UTF-8"));
}
public static String decrypt(byte[] data, String key) throws Exception {
KeyGenerator keyGenerator = KeyGenerator.getInstance("AES");
SecureRandom secureRandom = SecureRandom.getInstance("SHA1PRNG");
secureRandom.setSeed(key.getBytes());
keyGenerator.init(128, secureRandom);
SecretKey secretKey = keyGenerator.generateKey();
byte[] keyBytes = secretKey.getEncoded();
SecretKeySpec secretKeySpec = new SecretKeySpec(keyBytes, "AES");
Cipher cipher = Cipher.getInstance("AES/ECB/PKCS5Padding");
cipher.init(Cipher.DECRYPT_MODE, secretKeySpec);
return new String(cipher.doFinal(data), "UTF-8");
}
public static void main(String[] args) throws Exception {
String key = "1234567890123456";
String data = "Hello World!";
byte[] encryptedData = encrypt(data, key);
String decryptedData = decrypt(encryptedData, key);
System.out.println("Encrypted data: " + encryptedData);
System.out.println("Decrypted data: " + decryptedData);
}
}
(2)使用Java的DES加密算法
除了使用AES加密算法外,还可以使用DES加密算法来实现对称加密,实现方法和上述类似。
import java.security.Key;
import javax.crypto.Cipher;
import javax.crypto.KeyGenerator;
public class DESEncryption {
private static final String ALGORITHM = "DES";
private static final int KEY_SIZE = 56;
private static final int MODE = Cipher.ENCRYPT_MODE;
public static byte[] encrypt(byte[] data, byte[] key) throws Exception {
Key desKey = generateKey(key);
Cipher cipher = Cipher.getInstance(ALGORITHM);
cipher.init(MODE, desKey);
return cipher.doFinal(data);
}
public static byte[] decrypt(byte[] data, byte[] key) throws Exception {
Key desKey = generateKey(key);
Cipher cipher = Cipher.getInstance(ALGORITHM);
cipher.init(Cipher.DECRYPT_MODE, desKey);
return cipher.doFinal(data);
}
private static Key generateKey(byte[] key) throws Exception {
KeyGenerator keyGenerator = KeyGenerator.getInstance(ALGORITHM);
keyGenerator.init(KEY_SIZE);
return keyGenerator.generateKey();
}
public static void main(String[] args) throws Exception {
String key = "12345678";
String data = "Hello World!";
byte[] encryptedData = encrypt(data.getBytes(), key.getBytes());
byte[] decryptedData = decrypt(encryptedData, key.getBytes());
System.out.println("Encrypted data: " + new String(encryptedData));
System.out.println("Decrypted data: " + new String(decryptedData));
}
}
2.非对称加密
非对称加密算法是指加密和解密使用不同密钥的加密算法,常用的非对称加密算法有RSA算法、DSA算法和ECC算法等。
(1)使用Java的RSA算法
在Java中,可以使用RSA算法来实现非对称加密,具体实现方法如下:
import java.security.Key;
import java.security.KeyPair;
import java.security.KeyPairGenerator;
import java.security.PrivateKey;
import java.security.PublicKey;
import java.security.Signature;
import javax.crypto.Cipher;
public class RSAEncryption {
private static final String ALGORITHM = "RSA";
private static final int KEY_SIZE = 1024;
private static final int MODE = Cipher.ENCRYPT_MODE;
public static byte[] encrypt(byte[] data, PublicKey publicKey) throws Exception {
Cipher cipher = Cipher.getInstance(ALGORITHM);
cipher.init(MODE, publicKey);
return cipher.doFinal(data);
}
public static byte[] decrypt(byte[] data, PrivateKey privateKey) throws Exception {
Cipher cipher = Cipher.getInstance(ALGORITHM);
cipher.init(Cipher.DECRYPT_MODE, privateKey);
return cipher.doFinal(data);
}
public static byte[] sign(byte[] data, PrivateKey privateKey) throws Exception {
Signature signature = Signature.getInstance("MD5withRSA");
signature.initSign(privateKey);
signature.update(data);
return signature.sign();
}
public static boolean verify(byte[] data, PublicKey publicKey, byte[] sign) throws Exception {
Signature signature = Signature.getInstance("MD5withRSA");
signature.initVerify(publicKey);
signature.update(data);
return signature.verify(sign);
}
public static KeyPair generateKeyPair() throws Exception {
KeyPairGenerator keyPairGenerator = KeyPairGenerator.getInstance(ALGORITHM);
keyPairGenerator.initialize(KEY_SIZE);
return keyPairGenerator.generateKeyPair();
}
public static void main(String[] args) throws Exception {
String data = "Hello World!";
KeyPair keyPair = generateKeyPair();
PublicKey publicKey = keyPair.getPublic();
PrivateKey privateKey = keyPair.getPrivate();
byte[] encryptedData = encrypt(data.getBytes(), publicKey);
byte[] decryptedData = decrypt(encryptedData, privateKey);
System.out.println("Public Key: " + publicKey);
System.out.println("Private Key: " + privateKey);
System.out.println("Encrypted data: " + new String(encryptedData));
System.out.println("Decrypted data: " + new String(decryptedData));
byte[] sign = sign(data.getBytes(), privateKey);
boolean verify = verify(data.getBytes(), publicKey, sign);
System.out.println("Signature: " + new String(sign));
System.out.println("Verify: " + verify);
}
}
(2)使用Java的ECC算法
在Java中,可以使用ECC算法来实现非对称加密,具体实现方法如下:
import java.security.KeyPair;
import java.security.KeyPairGenerator;
import java.security.PrivateKey;
import java.security.PublicKey;
import javax.crypto.Cipher;
public class ECCEncryption {
private static final String ALGORITHM = "EC";
private static final int KEY_SIZE = 256;
private static final int MODE = Cipher.ENCRYPT_MODE;
public static byte[] encrypt(byte[] data, PublicKey publicKey) throws Exception {
Cipher cipher = Cipher.getInstance(ALGORITHM);
cipher.init(MODE, publicKey);
return cipher.doFinal(data);
}
public static byte[] decrypt(byte[] data, PrivateKey privateKey) throws Exception {
Cipher cipher = Cipher.getInstance(ALGORITHM);
cipher.init(Cipher.DECRYPT_MODE, privateKey);
return cipher.doFinal(data);
}
public static KeyPair generateKeyPair() throws Exception {
KeyPairGenerator keyPairGenerator = KeyPairGenerator.getInstance(ALGORITHM);
keyPairGenerator.initialize(KEY_SIZE);
return keyPairGenerator.generateKeyPair();
}
public static void main(String[] args) throws Exception {
String data = "Hello World!";
KeyPair keyPair = generateKeyPair();
PublicKey publicKey = keyPair.getPublic();
PrivateKey privateKey = keyPair.getPrivate();
byte[] encryptedData = encrypt(data.getBytes(), publicKey);
byte[] decryptedData = decrypt(encryptedData, privateKey);
System.out.println("Public Key: " + publicKey);
System.out.println("Private Key: " + privateKey);
System.out.println("Encrypted data: " + new String(encryptedData));
System.out.println("Decrypted data: " + new String(decryptedData));
}
}
总结
在Java中,可以使用对称加密算法和非对称加密算法来实现简单的加密和解密功能,常见的对称加密算法有AES和DES,常见的非对称加密算法有RSA和ECC。具体算法的选择需要根据应用场景和数据需求进行权衡和选择。
