Android文件加密 [英] Android File Cryptography

查看:140
本文介绍了Android文件加密的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何使加密和解密的应用程序的文件的SD卡?这样我可以担保在SD卡中的文件,并没有其他人将能够访问该应用程序之外的与出解密这些文件?

How to make encryption and decryption on an application's files in sd-card? so that i may secure the files on the sdcard and no other person will be able to access outside of that application with out decryption on those files??

有没有人谁可以给我什么好的示例源来实现加密的Andr​​oid应用程序?

Is there any person who can give me any good example source to implement the encryption on android application?

推荐答案

我写了这个程序,将使用AES加密文件和解密相同的文件。这一定会帮助你。

I have written this program that will encrypt a file using AES and decrypt the same file. This will surely help you.

FileInputStream fis = new FileInputStream(new File("D:/Shashank/Test123.txt"));
        File outfile = new File("D:/Shashank/encTest1234.txt");
        int read;
        if(!outfile.exists())
            outfile.createNewFile();
        File decfile = new File("D:/Shashank/dec123.txt");
        if(!decfile.exists())
            decfile.createNewFile();
        FileOutputStream fos = new FileOutputStream(outfile);
        FileInputStream encfis = new FileInputStream(outfile);
        FileOutputStream decfos = new FileOutputStream(decfile);
        Cipher encipher = Cipher.getInstance("AES");
        Cipher decipher = Cipher.getInstance("AES");
        KeyGenerator kgen = KeyGenerator.getInstance("AES");
        SecretKey skey = kgen.generateKey();
        encipher.init(Cipher.ENCRYPT_MODE, skey);
        CipherInputStream cis = new CipherInputStream(fis, encipher);
        decipher.init(Cipher.DECRYPT_MODE, skey);
        CipherOutputStream cos = new CipherOutputStream(decfos,decipher);
        while((read = cis.read())!=-1)
                {
                    fos.write((char)read);
                    fos.flush();
                }   
        fos.close();
        while((read=encfis.read())!=-1)
        {
            cos.write(read);
            cos.flush();
        }
    cos.close();

这篇关于Android文件加密的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆