[英]Removing characters from decrypted Base64 string in Java
關於這個問題; Java(Android)解密帶有IV的味精
我的消息可以很好地解密,但帶有不需要的IV字節數據。
我嘗試刪除所附的IV,但是並沒有刪除所有字符,並且某些字符始終保留在后面。 我不確定如何計算編碼的IV的長度以刪除不需要的字符。
public String decrypt(String cipherText, byte[] encryptionKey) throws Exception {
SecretKeySpec key = new SecretKeySpec(encryptionKey, "AES");
cipher.init(Cipher.DECRYPT_MODE, key, iV);
String decrypt = new String(cipher.doFinal( Base64.decode(cipherText, Base64.DEFAULT)));
byte[] decryptData = new byte[decrypt.getBytes().length - iV.getIV().length];
System.arraycopy(decrypt.getBytes(), iV.getIV().length, decryptData, 0, decrypt.getBytes().length - iV.getIV().length);
Log.d("decrypt = ", decrypt);
decrypt = new String(decryptData, "UTF-8");
return decrypt;
}
您需要在解密之前而不是之后刪除IV,因為它是解密的參數。 由於IV是密文的前綴,因此無需將其保存在其他位置(無需iV
參考)。
byte[] ciphertextBytes = Base64.decode(cipherText, Base64.DEFAULT);
IvParameterSpec iv = new IvParameterSpec(ciphertextBytes, 0, 16);
ciphertextBytes = Arrays.copyOfRange(ciphertextBytes, 16, ciphertextBytes.length);
SecretKeySpec key = new SecretKeySpec(encryptionKey, "AES");
cipher.init(Cipher.DECRYPT_MODE, key, iv);
String decrypt = new String(cipher.doFinal(ciphertextBytes), "UTF-8");
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.