正在使用预先存在的 AES 密钥解密 Java 中的文件
Decrypting file in Java with pre-existing AES key
我正在尝试做一些非常简单的事情。我使用 AxCrypt 在 Windows 中加密了一个文件。在我的 Android 应用程序中,我想解密这个文件。
AxCrypt生成的128位AES密钥为
CWTr 45Qg eHhy n23d YPC3 DjRi IxUe bt77 TVzQ NtSh HEc=
我假设这是一个 Base64 编码的字符串,但也许我错了。我用空格将它插入到下面的代码中,但我也尝试不使用空格,但我得到了相同的结果。
解密文件的java代码如下。解密过程开始,但出现错误 "last block incomplete in decryption",无法播放生成的文件(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());
}
AxCrypt 在 CBC 模式下加密,以及压缩、MAC 和许多其他细节。要对此进行解密,您需要在此处查看 http://www.axantum.com/AxCrypt/faq.html 及其发布的源代码。
我正在尝试做一些非常简单的事情。我使用 AxCrypt 在 Windows 中加密了一个文件。在我的 Android 应用程序中,我想解密这个文件。
AxCrypt生成的128位AES密钥为
CWTr 45Qg eHhy n23d YPC3 DjRi IxUe bt77 TVzQ NtSh HEc=
我假设这是一个 Base64 编码的字符串,但也许我错了。我用空格将它插入到下面的代码中,但我也尝试不使用空格,但我得到了相同的结果。
解密文件的java代码如下。解密过程开始,但出现错误 "last block incomplete in decryption",无法播放生成的文件(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());
}
AxCrypt 在 CBC 模式下加密,以及压缩、MAC 和许多其他细节。要对此进行解密,您需要在此处查看 http://www.axantum.com/AxCrypt/faq.html 及其发布的源代码。