栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 面试经验 > 面试问答

在Android上使用AES加密文件

面试问答 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

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

希望这可以帮助你。



转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/495935.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号