在Android上使用AES加密文件

在Android上使用AES加密文件,第1张

在Android上使用AES加密文件

也许我缺少了一些东西,但就我而言,它可以正常工作。您能否仅通过更改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兼容模式下运行。

希望这可以帮助你。



欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/zaji/5488056.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-12-12
下一篇 2022-12-12

发表评论

登录后才能评论

评论列表(0条)

保存