[英]Android encryption/Decryption issue
我正在android中對文件進行加密解密,為此我使用下面的代碼
private void encryptFile()
{
try
{
File f = new File(Environment.getExternalStorageDirectory() + "/images.jpg");
FileInputStream in = new FileInputStream(f);
byte[] buffer = new byte[100];
int num = in.read(buffer, 0, 100);
Encryption mEncryption = new Encryption("test");
File tempFile = new File(Environment.getExternalStorageDirectory() + "/temp.jpg");
FileOutputStream os = new FileOutputStream(tempFile);
os.write(mEncryption.encrypt(buffer), 0, 100);
while(in.read(buffer) != -1)
{
os.write(buffer);
}
in.close();
os.close();
}
catch (Exception e)
{
e.printStackTrace();
}
}
private void decryptFile()
{
try
{
File f = new File(Environment.getExternalStorageDirectory() + "/temp.jpg");
FileInputStream in = new FileInputStream(f);
byte[] buffer = new byte[100];
in.read(buffer, 0, 100);
Encryption mEncryption = new Encryption("test");
File tempFile = new File(Environment.getExternalStorageDirectory() + "/images.jpg");
FileOutputStream os = new FileOutputStream(tempFile);
os.write(mEncryption.decrypt(buffer), 0, 100);
while(in.read(buffer) != -1)
{
os.write(buffer);
}
in.close();
os.close();
}
catch (Exception e)
{
e.printStackTrace();
}
}
但是,當我解密文件時,它給我IllegalBlockSizeException: last block incomplete in decryption
任何想法為什么會發生?
編輯:我正在使用此加密類
你正在使用的加密類已經發布在我的博客上,就像你說的那樣( http://blog.kotowicz.net/2010/09/story-of-android-cryptography-and.html ),但作為你如何應該的例子' T實施加密! 它有一些特定的填充和密鑰擴展問題,所有這些都在博客文章中提到過。
該類源自此類源自Android Remote Notifier項目。 如果您確實需要,請至少使用更正的版本http://code.google.com/p/android-notifier/source/browse/trunk/AndroidNotifier/src/org/damazio/notifier/util/Encryption.java -我博客文章中的版本存在一些嚴重問題。
正如Nic Strong提到的那樣,你遇到填充 - 塊密碼與塊大小對齊,你應該考慮到這一點。
此代碼存在許多潛在問題。 什么是Encryption
類? 它不是標准Android SDK的一部分。 你不打算加密整個文件嗎? 此代碼僅加密前100個字節。 而這正是主要的錯誤所在。 然后代碼假定輸入文件的前100個字節將包含加密數據,這是一個無效的假設。 將加密數據的開始標准化為加密算法的塊大小的長度。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.