温馨提示:这篇文章已超过239天没有更新,请注意相关的内容是否还可用!
1、AES加密是一种对称加密算法,也就是加密和解密使用相同的密钥。它可以对数据进行快速加密和解密,适用于对大量数据进行加密的场景。AES加密算法可以使用不同的密钥长度,包括128位、192位和256位。
下面是一个使用Java实现AES加密的示例代码:
import javax.crypto.Cipher;
import javax.crypto.spec.SecretKeySpec;
import java.util.Base64;
public class AESUtils {
private static final String ALGORITHM = "AES";
private static final String TRANSFORMATION = "AES/ECB/PKCS5Padding";
private static final String KEY = "ThisIsASecretKey";
public static String encrypt(String input) throws Exception {
SecretKeySpec keySpec = new SecretKeySpec(KEY.getBytes(), ALGORITHM);
Cipher cipher = Cipher.getInstance(TRANSFORMATION);
cipher.init(Cipher.ENCRYPT_MODE, keySpec);
byte[] encryptedBytes = cipher.doFinal(input.getBytes());
return Base64.getEncoder().encodeToString(encryptedBytes);
}
public static String decrypt(String encryptedInput) throws Exception {
SecretKeySpec keySpec = new SecretKeySpec(KEY.getBytes(), ALGORITHM);
Cipher cipher = Cipher.getInstance(TRANSFORMATION);
cipher.init(Cipher.DECRYPT_MODE, keySpec);
byte[] decryptedBytes = cipher.doFinal(Base64.getDecoder().decode(encryptedInput));
return new String(decryptedBytes);
}
public static void main(String[] args) throws Exception {
String input = "Hello, World!";
String encryptedInput = encrypt(input);
System.out.println("Encrypted: " + encryptedInput);
String decryptedInput = decrypt(encryptedInput);
System.out.println("Decrypted: " + decryptedInput);
}
}
在上面的代码中,我们使用`javax.crypto`包提供的`Cipher`类来进行AES加密和解密操作。我们需要指定算法和加密模式,这里使用的是ECB模式和PKCS5Padding填充方式。然后,我们创建一个`SecretKeySpec`对象,使用密钥字符串和算法名称来初始化。接下来,我们使用`Cipher`对象的`init`方法来指定操作模式和密钥。我们调用`doFinal`方法来执行加密或解密操作,将结果转换为Base64编码的字符串返回。
2、RSA加密是一种非对称加密算法,它使用一对密钥,包括公钥和私钥。公钥用于加密数据,私钥用于解密数据。RSA加密算法适用于对小量数据进行加密,如对称加密算法的密钥加密。
下面是一个使用Java实现RSA加密的示例代码:
import java.security.KeyPair;
import java.security.KeyPairGenerator;
import java.security.PrivateKey;
import java.security.PublicKey;
import javax.crypto.Cipher;
public class RSAUtils {
private static final String ALGORITHM = "RSA";
public static KeyPair generateKeyPair() throws Exception {
KeyPairGenerator keyPairGenerator = KeyPairGenerator.getInstance(ALGORITHM);
keyPairGenerator.initialize(2048);
return keyPairGenerator.generateKeyPair();
}
public static byte[] encrypt(String input, PublicKey publicKey) throws Exception {
Cipher cipher = Cipher.getInstance(ALGORITHM);
cipher.init(Cipher.ENCRYPT_MODE, publicKey);
return cipher.doFinal(input.getBytes());
}
public static String decrypt(byte[] encryptedInput, PrivateKey privateKey) throws Exception {
Cipher cipher = Cipher.getInstance(ALGORITHM);
cipher.init(Cipher.DECRYPT_MODE, privateKey);
byte[] decryptedBytes = cipher.doFinal(encryptedInput);
return new String(decryptedBytes);
}
public static void main(String[] args) throws Exception {
String input = "Hello, World!";
KeyPair keyPair = generateKeyPair();
PublicKey publicKey = keyPair.getPublic();
PrivateKey privateKey = keyPair.getPrivate();
byte[] encryptedInput = encrypt(input, publicKey);
System.out.println("Encrypted: " + new String(encryptedInput));
String decryptedInput = decrypt(encryptedInput, privateKey);
System.out.println("Decrypted: " + decryptedInput);
}
}
在上面的代码中,我们使用`java.security`包提供的`KeyPairGenerator`类来生成RSA密钥对。我们创建一个`KeyPairGenerator`对象,并指定算法为RSA,密钥长度为2048位。然后,我们调用`generateKeyPair`方法生成密钥对。
接下来,我们使用`javax.crypto`包提供的`Cipher`类来进行RSA加密和解密操作。我们创建一个`Cipher`对象,指定算法为RSA。对于加密操作,我们使用公钥来初始化`Cipher`对象;对于解密操作,我们使用私钥来初始化`Cipher`对象。然后,我们调用`doFinal`方法来执行加密或解密操作,将结果返回。
我们调用`encrypt`方法使用公钥对数据进行加密,并将加密结果转换为字符串输出。然后,我们调用`decrypt`方法使用私钥对加密数据进行解密,并将解密结果输出。