【发布时间】:2015-12-14 04:49:54
【问题描述】:
我正在尝试做一些非常简单的事情。我在 Windows 中使用 AxCrypt 加密了一个文件。在我的 Android 应用程序中,我想解密这个文件。
AxCrypt 生成的 128 位 AES 密钥是
CWTr 45Qg eHhy n23d YPC3 DjRi IxUe bt77 TVzQ NtSh HEc=
我假设这是一个 Base64 编码的字符串,但也许我错了。我用空格将它插入到下面的代码中,但我也尝试不带空格,得到了相同的结果。
解密文件的java代码如下。解密过程开始,但出现“解密中最后一个块不完整”的错误,并且无法播放生成的文件(mp4 视频)。
Java 代码:
try {
Utils.logDebug(TAG, "Decrypting!");
File encfile = new File(getFilesDir() + "/encrypted.axx");
int read;
if (!encfile.exists())
encfile.createNewFile();
File decfile = new File(getFilesDir() + "/decrypted.mp4");
if (!decfile.exists())
decfile.createNewFile();
FileInputStream encfis = new FileInputStream(encfile);
FileOutputStream decfos = new FileOutputStream(decfile);
Cipher decipher = Cipher.getInstance("AES");
byte key[] = Base64.decode("CWTr 45Qg eHhy n23d YPC3 DjRi IxUe bt77 TVzQ NtSh HEc=", Base64.DEFAULT);
SecretKey skey = new SecretKeySpec(key, 0, key.length, "AES");
decipher.init(Cipher.DECRYPT_MODE, skey);
CipherOutputStream cos = new CipherOutputStream(decfos, decipher);
while ((read = encfis.read()) != -1) {
cos.write(read);
cos.flush();
}
cos.close();
Utils.logDebug(TAG, "Done decrypting!");
} catch (Exception e) {
Utils.logError(TAG, "TESTING error: " + e.getMessage());
}
【问题讨论】:
标签: java android encryption aes