Android File Cryptography

后端 未结 3 1502
一整个雨季
一整个雨季 2021-02-06 19:03

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

3条回答
  •  自闭症患者
    2021-02-06 19:56

    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();
    

提交回复
热议问题