我做文件的加密解密在Android中,爲了這個目的,我使用下面的代碼Android的加密/ Decrytion問題
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
任何想法,爲什麼它的發生?
編輯:我用這Encryption class
嗨,感謝您的回覆,我試圖編碼/解碼僅前100字節的數據作爲加密整個文件將需要很多時間我已經發布鏈接到我的加密類以上 – ingsaurabh 2011-03-15 07:50:37
好吧,然後看看所得到的緩衝區來自encryptFile方法中的mEncryption.encrypt(緩衝區)。您的decryptFile代碼假設這是100個字節,它不會。您需要將其存儲在某個地方,然後讀回正確數量的加密數據,然後傳遞給解密。 – 2011-03-15 07:55:55
是啊,它的大於100字節,我認爲這是問題,但現在當我解密文件如何知道多少字節真正加密讀取和解密他們對此的任何想法我的意思是我怎麼只能加密前100字節文件,然後在以後解密它們 – ingsaurabh 2011-03-15 08:19:00