这里分享AES的加密代码,包含两种填充方式PKCS7Padding和PKCS5Padding。代码中加密后的数据再加密成Base64形式,这一部分也可以换成16进制的形式。
Java本身是不支持PKCS7Padding加密、解密的,因此需要添加外部依赖。
<!-- https://mvnrepository.com/artifact/org.bouncycastle/bcprov-jdk15on -->
<dependency>
<groupId>org.bouncycastle</groupId>
<artifactId>bcprov-jdk15on</artifactId>
<version>1.60</version>
</dependency>
import org.bouncycastle.jce.provider.BouncyCastleProvider;
import org.bouncycastle.util.encoders.Base64;
import javax.crypto.Cipher;
import javax.crypto.spec.SecretKeySpec;
import java.security.Key;
import java.security.Security;
public class AESTest {
//添加AES/ECB/PKCS7Padding的加密与解密方式
static {
if (Security.getProvider("BC") == null) {
Security.addProvider(new BouncyCastleProvider());
} else {
Security.removeProvider("BC");
Security.addProvider(new BouncyCastleProvider());
}
}
//AES/ECB/PKCS5Padding加密
private static String encode(String data, String privateKey) {
try {
Key key = generateKey(privateKey);
Cipher cipher = Cipher.getInstance("AES/ECB/PKCS5Padding");
cipher.init(Cipher.ENCRYPT_MODE, key);
byte[] bytes = cipher.doFinal(data.getBytes());
return new String(Base64.encode(bytes));
} catch (Exception e) {
e.printStackTrace();
}
return null;
}
//AES/ECB/PKCS5Padding解密
private static String decode(String data, String privateKey) {
try {
Key key = generateKey(privateKey);
Cipher cipher = Cipher.getInstance("AES/ECB/PKCS5Padding");
cipher.init(Cipher.DECRYPT_MODE, key);
byte[] bytes = cipher.doFinal(Base64.decode(data.getBytes()));
return new String(bytes);
} catch (Exception e) {
e.printStackTrace();
}
return null;
}
//AES/ECB/PKCS7Padding加密
private static String encodeWithPKCS7Padding(String data, String privateKey) {
try {
Key key = generateKey(privateKey);
Cipher cipher = Cipher.getInstance("AES/ECB/PKCS7Padding", "BC");
cipher.init(Cipher.ENCRYPT_MODE, key);
byte[] bytes = cipher.doFinal(data.getBytes());
return new String(Base64.encode(bytes));
} catch (Exception e) {
e.printStackTrace();
}
return null;
}
//AES/ECB/PKCS7Padding解密
private static String decodeWithPKCS7Padding(String data, String privateKey) {
try {
Key key = generateKey(privateKey);
Cipher cipher = Cipher.getInstance("AES/ECB/PKCS7Padding", "BC");
cipher.init(Cipher.DECRYPT_MODE, key);
byte[] bytes = cipher.doFinal(Base64.decode(data.getBytes()));
return new String(bytes);
} catch (Exception e) {
e.printStackTrace();
}
return null;
}
private static Key generateKey(String privateKey) {
return new SecretKeySpec(privateKey.getBytes(), "AES");
}
public static void main(String[] args) {
//密钥需要16位
String privateKey = "12349876abcdzyxw";
String data = "This is a demo.";
String encode = encode(data, privateKey);
String decode = decode(encode, privateKey);
System.out.println("加密前数据:" + data + "\n加密后数据:" + encode + "\n解密数据:" + decode);
System.out.println("----------------------------------------------------------------------");
String encodeWithPKCS7Padding = encodeWithPKCS7Padding(data, privateKey);
String decodeWithPKCS7Padding = decodeWithPKCS7Padding(encodeWithPKCS7Padding, privateKey);
System.out.println("加密前数据:" + data + "\n加密后数据:" + encodeWithPKCS7Padding + "\n解密数据:" + decodeWithPKCS7Padding);
}
}
参考链接:https://blog.csdn.net/lijun169/article/details/82736103