【问题标题】:Encrypting files with AES on Android在 Android 上使用 AES 加密文件
【发布时间】:2015-03-22 19:04:46
【问题描述】:

所以我正在为自己做一个个人项目,我正在尝试加密我手机上的文件。这些文件可以是任何东西,即文档、照片等。现在我正试图让它正常工作。每当我运行加密时,它似乎都能正常工作并加密文件。当我运行解密时,有时它可以工作,有时它不能。当它失败时,我通常会收到“完成密码时出错,填充块损坏”错误。我也没有使用不同的测试文件,所以它不像某些文件工作而其他文件不工作。这与我每次尝试的两个文件相同。

public static void encryptfile(String path,String Pass) throws IOException, NoSuchAlgorithmException, NoSuchPaddingException, InvalidKeyException {
    FileInputStream fis = new FileInputStream(path);
    FileOutputStream fos = new FileOutputStream(path.concat(".crypt"));
    byte[] key = (salt + Pass).getBytes("UTF-8");
    MessageDigest sha = MessageDigest.getInstance("SHA-1");
    key = sha.digest(key);
    key = Arrays.copyOf(key,16);
    SecretKeySpec sks = new SecretKeySpec(key, "AES");
    Cipher cipher = Cipher.getInstance("AES");
    cipher.init(Cipher.ENCRYPT_MODE, sks);
    CipherOutputStream cos = new CipherOutputStream(fos, cipher);
    int b;
    byte[] d = new byte[8];
    while((b = fis.read(d)) != -1) {
        cos.write(d, 0, b);
    }
    cos.flush();
    cos.close();
    fis.close();
}

public static void decrypt(String path,String Pass) throws IOException, NoSuchAlgorithmException, NoSuchPaddingException, InvalidKeyException {
    FileInputStream fis = new FileInputStream(path);
    FileOutputStream fos = new FileOutputStream(path.replace(".crypt",""));
    byte[] key = (salt + Pass).getBytes("UTF-8");
    MessageDigest sha = MessageDigest.getInstance("SHA-1");
    key = sha.digest(key);
    key = Arrays.copyOf(key,16);
    SecretKeySpec sks = new SecretKeySpec(key, "AES");
    Cipher cipher = Cipher.getInstance("AES");
    cipher.init(Cipher.DECRYPT_MODE, sks);
    CipherInputStream cis = new CipherInputStream(fis, cipher);
    int b;
    byte[] d = new byte[8];
    while((b = cis.read(d)) != -1) {
        fos.write(d, 0, b);
    }
    fos.flush();
    fos.close();
    cis.close();
}

目前,Salt 和 Password 是静态的,不会出于测试目的而更改。大约一半的时间仍然会出错。

有没有人知道为什么会发生这种情况?我一直在四处寻找,我发现了一些可以尝试的方法,但都没有奏效。我查看了以下一些问题的解决方案:

Android decryption: Error while finalizing cipher

last block incomplete with CipherInputStream/CipherOutputStream, even with padding AES/CBC/PKCS5Padding

Encryption error on Android 4.2

Decrypting error : "no iv set when one expected"

How to handle "last block incomplete in decryption"

Encryption and decryption of image file

Tips on encryption/decryption of images in java using AES

非常感谢任何帮助!我想我只是缺少一些简单的东西......

更新!

当它是盐时,人们是对的。当我移除盐时,问题就解决了......做了更多的挖掘,结果是盐+通行证是问题所在,但是因为盐是一个字节[],而通行证是一个字符串。我把salt改成String,然后用salt.concat(Pass),问题就解决了!

【问题讨论】:

  • 我个人很想看看你试图实现什么。它还可以帮助其他人,因为他们可以看到哪些选项已被尝试(即使它们实施不正确)。
  • @Bono 我添加了我认为对我有帮助的链接,但最终对我没有多大帮助。我已经尝试过一些事情,比如让它没有填充,使用 CBC 而不是 EBC(无论如何我都想做)。在某一时刻,我什至让它转换了 Base64:P
  • 似乎盐最有可能是问题所在,因为盐由随机字节组成,但它是作为字符串给出的。你是如何制作盐的?
  • @MaartenBodewes 我希望是这样,但 Salt 目前已修复用于测试:/
  • 此错误可能发生在错误的密钥、乱码的密文或 - 仅对于非常短的密文 - 因为错误的 IV 值。如果您输入了错误的密码,这将是检测它的唯一方法(如果不存在身份验证标签)。

标签: java android encryption


【解决方案1】:

也许我遗漏了一些东西,但就我而言,它可以正常工作。 您可以尝试以下类,只需更改 fileToBeCrypted、fileToBeDecrypted 和 fileDecryptedOutput 变量吗?

package test;

import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.security.InvalidKeyException;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.util.Arrays;

import javax.crypto.Cipher;
import javax.crypto.CipherInputStream;
import javax.crypto.CipherOutputStream;
import javax.crypto.NoSuchPaddingException;
import javax.crypto.spec.SecretKeySpec;

public class TestCrypt{

    private static final String salt = "t784";
    private static final String cryptPassword = "873147cbn9x5'2 79'79314";
    private static final String fileToBeCrypted = "c:\\Temp\\sampleFile.conf";
    private static final String fileToBeDecrypted = "c:\\Temp\\sampleFile.conf.crypt";
    private static final String fileDecryptedOutput = "c:\\Temp\\sampleFile.conf.decrypted";

    public static void main(String[] args) throws Exception
    {
        for (int i=0; i<100; i++)
        {
            encryptfile(fileToBeCrypted, cryptPassword);
            decrypt(fileToBeDecrypted, cryptPassword, fileDecryptedOutput);
            System.out.println(i);
        }
    }

    public static void encryptfile(String path,String password) throws IOException, NoSuchAlgorithmException, NoSuchPaddingException, InvalidKeyException {
        FileInputStream fis = new FileInputStream(path);
        FileOutputStream fos = new FileOutputStream(path.concat(".crypt"));
        byte[] key = (salt + password).getBytes("UTF-8");
        MessageDigest sha = MessageDigest.getInstance("SHA-1");
        key = sha.digest(key);
        key = Arrays.copyOf(key,16);
        SecretKeySpec sks = new SecretKeySpec(key, "AES");
        Cipher cipher = Cipher.getInstance("AES");
        cipher.init(Cipher.ENCRYPT_MODE, sks);
        CipherOutputStream cos = new CipherOutputStream(fos, cipher);
        int b;
        byte[] d = new byte[8];
        while((b = fis.read(d)) != -1) {
            cos.write(d, 0, b);
        }
        cos.flush();
        cos.close();
        fis.close();
    }

    public static void decrypt(String path,String password, String outPath) throws IOException, NoSuchAlgorithmException, NoSuchPaddingException, InvalidKeyException {
        FileInputStream fis = new FileInputStream(path);
        FileOutputStream fos = new FileOutputStream(outPath);
        byte[] key = (salt + password).getBytes("UTF-8");
        MessageDigest sha = MessageDigest.getInstance("SHA-1");
        key = sha.digest(key);
        key = Arrays.copyOf(key,16);
        SecretKeySpec sks = new SecretKeySpec(key, "AES");
        Cipher cipher = Cipher.getInstance("AES");
        cipher.init(Cipher.DECRYPT_MODE, sks);
        CipherInputStream cis = new CipherInputStream(fis, cipher);
        int b;
        byte[] d = new byte[8];
        while((b = cis.read(d)) != -1) {
            fos.write(d, 0, b);
        }
        fos.flush();
        fos.close();
        cis.close();
    }

}

我可以多次迭代而不会出错! 我使用的是 Oracle JDK 1.8,但在 1.7 兼容模式下运行。

希望对你有所帮助。

再见 皮耶罗

【讨论】:

  • 原来是盐....唯一的问题是我不知道为什么。我正在做更多的挖掘:)
  • 同意Linux,salt不能包含特殊字符。
  • 不适用于视频文件。加解密后,视频无法播放。请提出一些解决方案。
  • 您好,没有特殊的配置或更改来处理视频等二进制文件:您将它们存储在哪里?
  • 我们可以使用任何字符作为盐吗?
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-11-13
  • 2011-12-20
  • 2015-08-14
  • 2016-09-22
相关资源
最近更新 更多