我正在尝试使用 AES/ECB/PKCS7Padding 创建加密和解密函数。
private static byte[] INITIALIZATION_VECTOR = new byte[] { 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00 };
public static String encrypt(String token) {
Cipher cipher = null;
SecretKey key = null;
String tokenAsHex = null;
byte[] encryptedToken = null;
byte[] sksKey = "6iOmT2V6mnd0".getBytes(); // SecretKeySpec key.
try {
key = new SecretKeySpec(sksKey, "AES");
AlgorithmParameterSpec paramSpec = new IvParameterSpec(INITIALIZATION_VECTOR);
cipher = Cipher.getInstance("AES/CBC/PKCS5Padding");
cipher.init(Cipher.ENCRYPT_MODE, key, paramSpec);
encryptedToken = cipher.doFinal(token.getBytes("UTF-8"));
} catch (Exception e) {
e.printStackTrace();
}
return Base64.encodeBase64String(encryptedToken);
}
public static String decrypt(String token) {
Cipher cipher = null;
SecretKey key = null;
byte[] decryptedToken = null;
byte[] sksKey = "6iOmT2V6mnd0".getBytes(); // SecretKeySpec key.
try {
key = new SecretKeySpec(sksKey, "AES");
AlgorithmParameterSpec paramSpec = new IvParameterSpec(INITIALIZATION_VECTOR);
cipher = Cipher.getInstance("AES/CBC/PKCS5Padding");
cipher.init(Cipher.DECRYPT_MODE, key, paramSpec);
decryptedToken = cipher.doFinal(Base64.decodeBase64(token));
} catch(Exception e){
e.printStackTrace();
}
if (decryptedToken == null) {
System.out.println("Unable to decrypt the following token: " + token);
}
return new String(decryptedToken);
}
我已经编辑了我的程序。
现在 dycryption 似乎有效,但它只适用于 PKCS5Padding ,当我尝试使用 PKCS7Padding 时,它说它找不到提供者,怎么可能?