且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

在 Android 上使用 AES 加密文件

更新时间:2023-09-28 12:40:16

也许我遗漏了一些东西,但在我这边它可以正常工作.您可以尝试简单地更改 fileToBeCrypted、fileToBeDecrypted 和 fileDecryptedOutput 变量的以下类吗?

maybe I'm missing something but on my side it works without a problem. Can you try the following class simply changing the fileToBeCrypted, fileToBeDecrypted and fileDecryptedOutput variables?

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 兼容模式下运行.

I can iterate many times without an error! I'm using Oracle JDK 1.8 but running in 1.7 compatibility mode.

希望对你有所帮助.

再见皮耶罗