在安卓设备上使用 AES 加密文件

2022-09-04 22:07:21

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

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 是静态的,不会出于测试目的而更改。仍然有大约一半的时间出错。

有没有人对为什么会发生这种情况有任何想法?我一直在四处寻找,我发现了一些可以尝试的东西,但没有一个有效。我查看了以下一些问题以寻求解决方案:

安卓解密:最终确定密码时出错

最后一个块不完整,使用 CipherInputStream/CipherOutputStream,即使使用填充 AES/CBC/PKCS5Padding 也是如此

安卓4.2上的加密错误

解密错误:“在预期时没有iv设置”

如何处理“解密中的最后一个块不完整”

镜像文件的加密和解密

有关使用AES在Java中加密/解密图像的提示

任何帮助都非常感谢!我想我只是错过了一些简单的东西...

更新!

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


答案 1

也许我错过了一些东西,但在我这边,它没有问题。你能尝试以下类简单地改变文件ToBeCrypted,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兼容模式下运行。

希望这对您有所帮助。

再见皮耶罗


答案 2

推荐