Android文件密码学

Ak.*_*... 2 android cryptography

如何在SD卡中的应用程序文件上进行加密和解密?所以我可以保护SD卡上的文件,没有其他人能够在那些文件解密后访问该应用程序之外?

有没有人可以给我任何好的示例源来实现Android应用程序的加密?

Sha*_*dne 5

我编写了这个程序,它将使用AES加密文件并解密同一个文件.这肯定会对你有所帮助.

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();
Run Code Online (Sandbox Code Playgroud)