簡體   English   中英

如何為Android Kitkat和更高版本加密和解密文件?

[英]How to encrypt and decrypt file for Android Kitkat and higher?

我正在搜索和測試解決方案一段時間,到目前為止沒有成功。 總是有一些問題。 以下代碼在Android Kitkat和更高版本上是“有效的”(意味着在運行時不顯示任何錯誤),但解密的文件不可讀。 為什么?

final static byte[] iv = new byte[16];//ADDED
final static int buffer = 102400;
final static String encryptionType = "AES/CFB8/NoPadding";//CHANGED TO DIFFERENT TYPE

static void encrypt(String password, File fileInput, File fileOutput) throws Exception {

    IvParameterSpec ivParams = new IvParameterSpec(iv);//ADDED

    FileInputStream fis = new FileInputStream(fileInput);
    FileOutputStream fos = new FileOutputStream(fileOutput);

    SecretKeySpec sks = new SecretKeySpec(password.getBytes("UTF-8"), encryptionType);

    Cipher cipher = Cipher.getInstance(encryptionType);
    //cipher.init(Cipher.ENCRYPT_MODE, sks);REPLACED
    cipher.init(Cipher.ENCRYPT_MODE, sks, ivParams);
    CipherOutputStream cos = new CipherOutputStream(fos, cipher);

    int b;
    byte[] d = new byte[buffer];
    while ((b = fis.read(d)) != -1) {
        cos.write(d, 0, b);
    }

    cos.flush();
    cos.close();
    fis.close();
}

static void decrypt(String password, File fileInput, File fileOutput) throws Exception {

    IvParameterSpec ivParams = new IvParameterSpec(iv);//ADDED

    FileInputStream fis = new FileInputStream(fileInput);
    FileOutputStream fos = new FileOutputStream(fileOutput);

    SecretKeySpec sks = new SecretKeySpec(password.getBytes("UTF-8"), encryptionType);

    Cipher cipher = Cipher.getInstance(encryptionType);
    //cipher.init(Cipher.ENCRYPT_MODE, sks);REPLACED
    cipher.init(Cipher.DECRYPT_MODE, sks, ivParams);
    CipherInputStream cis = new CipherInputStream(fis, cipher);

    int b;
    byte[] d = new byte[buffer];
    while ((b = cis.read(d)) != -1) {
        fos.write(d, 0, b);
    }

    fos.flush();
    fos.close();
    cis.close();
}

編輯:我將類型更改為“ AES / CFB8 / NoPadding”后,似乎沒問題,進程中沒有錯誤,但是解密后的文件不可讀。

解密方法中的問題是由以下行引起的:

cipher.init(Cipher.ENCRYPT_MODE, sks);

該模式需要Cipher.DECRYPT_MODE ,因此該行應為

cipher.init(Cipher.DECRYPT_MODE, sks);

其他問題包括使用過長的DESede算法,缺少任何IV生成和處理,缺少基於密碼的良好密鑰派生算法以及密文上沒有任何MAC。 正確使用具有適當的隨機數生成和處理的AES GCM模式,以及使用PBKDF2(可在Android和Oracle Java上使用)將代表重大改進。

您沒有提供IV,因此會自動為您生成一個。 您必須找到一種方法將此IV傳送給收件人。 通常,IV / Nonce在密文之前,並被接收者剝離以解密數據。 CipherInputStream / CipherOutputStream不會為您執行此操作,因此您必須自己執行此操作。

我終於通過使用較短的密碼解決了這個問題。 我不確定為什么,但是在Android 7和8上,長密碼沒有問題,但是在Android 4.4上使用相同的密碼會導致瘋狂的錯誤和制動加密。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM