×

注意!页面内容来自https://blog.csdn.net/weixin_32260467/article/details/117318689,本站不储存任何内容,为了更好的阅读体验进行在线解析,若有广告出现,请及时反馈。若您觉得侵犯了您的利益,请通知我们进行删除,然后访问 原网页

android 文件加密解决方法,如何在Android中加密和解密文件?

该博客介绍了如何使用AES算法对文件进行加密和解密,并将加密后的文件保存到SD卡上。提供了生成密钥、加密文件和解密文件的Java方法。在Android环境下,文件读写和AES加密解密过程清晰易懂。

我有类似的问题,加密/解密我提出了这个解决方案:

public static byte[] generateKey(String password) throws Exception

{

byte[] keyStart = password.getBytes("UTF-8");

KeyGenerator kgen = KeyGenerator.getInstance("AES");

SecureRandom sr = SecureRandom.getInstance("SHA1PRNG""Crypto");

sr.setSeed(keyStart);

kgen.init(128sr);

SecretKey skey = kgen.generateKey();

return skey.getEncoded();

}

public static byte[] encodeFile(byte[] keybyte[] fileData) throws Exception

{

SecretKeySpec skeySpec = new SecretKeySpec(key"AES");

Cipher cipher = Cipher.getInstance("AES");

cipher.init(Cipher.ENCRYPT_MODEskeySpec);

byte[] encrypted = cipher.doFinal(fileData);

return encrypted;

}

public static byte[] decodeFile(byte[] keybyte[] fileData) throws Exception

{

SecretKeySpec skeySpec = new SecretKeySpec(key"AES");

Cipher cipher = Cipher.getInstance("AES");

cipher.init(Cipher.DECRYPT_MODEskeySpec);

byte[] decrypted = cipher.doFinal(fileData);

return decrypted;

}

要将加密文件保存到sd,请执行以下操作:

File file = new File(Environment.getExternalStorageDirectory() + File.separator + "your_folder_on_sd""file_name");

BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream(file));

byte[] yourKey = generateKey("password");

byte[] filesBytes = encodeFile(yourKeyyourByteArrayContainigDataToEncrypt);

bos.write(fileBytes);

bos.flush();

bos.close();

解码文件使用:

byte[] yourKey = generateKey("password");

byte[] decodedData = decodeFile(yourKeybytesOfYourFile);

对于将文件读入字节数组,有不同的方法。例如:http://examples.javacodegeeks.com/core-java/io/fileinputstream/read-file-in-byte-array-with-fileinputstream/

确定要放弃本次机会?
福利倒计时
: :

立减 ¥

普通VIP年卡可用
立即使用
参与评论 您还未登录,请先 登录 后发表或查看评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
点击重新获取
扫码支付
< type="text/css">
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值