我嘗試加密和解密大型音頻二進制文件。使用CipherInputStream和CipherOutputStream。我知道,關於這樣的主題存在很多問題。但我不明白我的代碼有什麼問題。請明確描述出什麼錯誤。謝謝。Android加密大文件
public void encrypt() {
doCrypto(Cipher.ENCRYPT_MODE, KEY);
}
public void decrypt() {
doCrypto(Cipher.DECRYPT_MODE, KEY);
}
private void doCrypto(int cipherMode, String key) {
try {
Key secretKey = new SecretKeySpec(key.getBytes("UTF-8"), "AES");
Cipher cipher = Cipher.getInstance("AES");
cipher.init(cipherMode, secretKey);
FileInputStream inputStream = new FileInputStream(this);
FileOutputStream fileOutputStream = new FileOutputStream(this);
int read;
CipherInputStream cis = new CipherInputStream(inputStream, cipher);
CipherOutputStream cos = new CipherOutputStream(fileOutputStream, cipher);
while ((read = cis.read()) != -1) {
cos.write(read);
cos.flush();
}
cos.close();
cis.close();
inputStream.close();
fileOutputStream.close();
} catch (NoSuchPaddingException | NoSuchAlgorithmException
| InvalidKeyException | IOException ex) {
throw new RuntimeException("Error encrypting/decrypting file", ex);
}
}
例外:
Caused by: java.lang.RuntimeException: Error encrypting/decrypting file at .hortext.HortextFile.doCrypto(HortextFile.java:81)
at .tools.hortext.HortextFile.decrypt(HortextFile.java:52)
at .tools.hortext.FilesStorage.getStringFromStorage(FilesStorage.java:104)
at com.msg.mobilinga.ui.ListenTextActivity.onCreate(ListenTextActivity.java:83)
at android.app.Activity.performCreate(Activity.java:5990)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1106)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2278)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2387)
at android.app.ActivityThread.access$800(ActivityThread.java:151)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1303)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:135)
at android.app.ActivityThread.main(ActivityThread.java:5254)
at java.lang.reflect.Method.invoke(Native Method)
at java.lang.reflect.Method.invoke(Method.java:372)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:903)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:698)
Caused by: java.io.IOException: Error while finalizing cipher
at javax.crypto.CipherInputStream.fillBuffer(CipherInputStream.java:104)
at javax.crypto.CipherInputStream.read(CipherInputStream.java:130)
at com.msg.mobilinga.tools.hortext.HortextFile.doCrypto(HortextFile.java:69)
at com.msg.mobilinga.tools.hortext.HortextFile.decrypt(HortextFile.java:52)
at com.msg.mobilinga.tools.hortext.FilesStorage.getStringFromStorage(FilesStorage.java:104)
at com.msg.mobilinga.ui.ListenTextActivity.onCreate(ListenTextActivity.java:83)
at android.app.Activity.performCreate(Activity.java:5990)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1106)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2278)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2387)
at android.app.ActivityThread.access$800(ActivityThread.java:151)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1303)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:135)
at android.app.ActivityThread.main(ActivityThread.java:5254)
at java.lang.reflect.Method.invoke(Native Method)
at java.lang.reflect.Method.invoke(Method.java:372)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:903)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:698)
Caused by: javax.crypto.IllegalBlockSizeException: last block incomplete in decryption
at com.android.org.bouncycastle.jcajce.provider.symmetric.util.BaseBlockCipher.engineDoFinal(BaseBlockCipher.java:894)
at javax.crypto.Cipher.doFinal(Cipher.java:1314)
at javax.crypto.CipherInputStream.fillBuffer(CipherInputStream.java:102)
at javax.crypto.CipherInputStream.read(CipherInputStream.java:130)
at com.msg.mobilinga.tools.hortext.HortextFile.doCrypto(HortextFile.java:69)
at com.msg.mobilinga.tools.hortext.HortextFile.decrypt(HortextFile.java:52)
at com.msg.mobilinga.tools.hortext.FilesStorage.getStringFromStorage(FilesStorage.java:104)
at com.msg.mobilinga.ui.ListenTextActivity.onCreate(ListenTextActivity.java:83)
at android.app.Activity.performCreate(Activity.java:5990)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1106)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2278)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2387)
at android.app.ActivityThread.access$800(ActivityThread.java:151)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1303)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:135)
at android.app.ActivityThread.main(ActivityThread.java:5254)
at java.lang.reflect.Method.invoke(Native Method)
at java.lang.reflect.Method.invoke(Method.java:372)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:903)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:698)
@ArtjomB。我的例外是編輯器不可見的。我被編輯後。請拿出你的減號。謝謝。 – smail2133
除了逐字節讀寫外,使用ECB模式和使用文本編碼密鑰,您的代碼看起來很好。 Scratch that ...爲什麼你同時使用'CipherInputStream' *和*'CipherOutputStream'? –
@ArtjomB。嗯,你能告訴我我該怎麼做?謝謝。 – smail2133