[update] AES类兼容ECB(虽然不再建议利用)

This commit is contained in:
its
2024-04-26 11:41:08 +08:00
parent 1415200b1a
commit cf8231017d
2 changed files with 9 additions and 2 deletions

View File

@@ -93,7 +93,10 @@ public class AES {
byte[] plaintext = new byte[plaintextLength];
System.arraycopy(dataBytes, 0, plaintext, 0, dataBytes.length);
SecretKeySpec keyspec = new SecretKeySpec(key.getBytes(charset), AES_ALGORITHM);
IvParameterSpec ivspec = new IvParameterSpec(iv.getBytes(charset));
IvParameterSpec ivspec = null;
if(!algorithm.name.contains("ECB")){
ivspec = new IvParameterSpec(iv.getBytes(charset));
}
cipher.init(Cipher.ENCRYPT_MODE, keyspec, ivspec);
byte[] encrypted = cipher.doFinal(plaintext);
return Base64.getEncoder().encodeToString(encrypted);
@@ -115,7 +118,10 @@ public class AES {
byte[] encrypted = Base64.getDecoder().decode(data);
Cipher cipher = Cipher.getInstance(algorithm.name);
SecretKeySpec keyspec = new SecretKeySpec(key.getBytes(charset), AES_ALGORITHM);
IvParameterSpec ivspec = new IvParameterSpec(iv.getBytes(charset));
IvParameterSpec ivspec = null;
if(!algorithm.name.contains("ECB")){
ivspec = new IvParameterSpec(iv.getBytes(charset));
}
cipher.init(Cipher.DECRYPT_MODE, keyspec, ivspec);
byte[] original = cipher.doFinal(encrypted);
return new String(original, charset).trim();