2
我試圖將BouncyCastle特定的實現轉換爲通用的實現,但由於我仍在努力處理基礎知識,所以我很難做到。使用密鑰和iv的Java AES塊解密
這是以前的BC代碼的工作原理:
public int decrypt(SecurityToken token, byte[] dataToDecrypt, int inputOffset,
int inputLength, byte[] output, int outputOffset) {
// Make new RijndaelEngine
RijndaelEngine engine = new RijndaelEngine(128);
// Make CBC blockcipher
BufferedBlockCipher bbc = new BufferedBlockCipher(
new CBCBlockCipher(engine));
// find right decryption key and right initialization vector
KeyParameter secret = new KeyParameter(
token.getRemoteEncryptingKey());
byte[] iv = token.getRemoteInitializationVector();
// initialize cipher for decryption purposes
bbc.init(false, new ParametersWithIV(secret, iv));
decryptedBytes = bbc.processBytes(dataToDecrypt, inputOffset,
inputLength, output, outputOffset);
decryptedBytes += bbc.doFinal(output, outputOffset+decryptedBytes);
return decryptedBytes;
}
,這是我謙虛的嘗試至今:
SecretKeySpec spec = new SecretKeySpec(
token.getRemoteEncryptingKey(),
"AES");
cipher = Cipher.getInstance("AES/CBC/PKCS5Padding");
cipher.init(Cipher.DECRYPT_MODE, spec, new IvParameterSpec(token.getRemoteInitializationVector()));
decryptedBytes = cipher.update(dataToDecrypt, inputOffset,
inputLength, output, outputOffset);
decryptedBytes += cipher.doFinal(output, outputOffset+decryptedBytes);
return decryptedBytes;
這給
javax.crypto.BadPaddingException: Given final block not properly padded
,在這裏輸入功能:
decrypt: dataToDecrypt.length=1088 inputOffset=0 inputLength=1088 output.length=16384 outputOffset=1180
decrypt: token.getRemoteEncryptingKey()=lBjgFjfR3IilCyT5AqRnXQ==
decrypt: token.getRemoteInitializationVector()=0JFEdkuW6pMo0cwfKdZa3w==
我錯過了什麼?
E:輸入數據