首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Android文件加密

Android文件加密
EN

Stack Overflow用户
提问于 2012-02-06 14:50:13
回答 2查看 9K关注 0票数 2

如何对SD卡中的应用程序文件进行加密和解密?这样我就可以保护SD卡上的文件,其他人就不能在不解密这些文件的情况下访问该应用程序之外的内容。

有没有人可以给我一个很好的示例源码来实现android应用的加密?

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2012-02-06 14:58:24

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

代码语言:javascript
复制
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();
票数 5
EN

Stack Overflow用户

发布于 2012-02-07 12:48:43

我找到了android应用程序的加密解决方案,通过它你可以保护应用程序内部和应用程序外的应用程序数据- sdcard文件管理器不能访问应用程序的机密信息。

为此,您可以使用两种可能的方法来保护应用程序文件。

  1. 更改具有其他扩展名的文件扩展名格式,以便真正的文件将无法通过用户
  2. 打开您能够使用应用程序文件内容的加密。

密码学

在这里,我将发布一个示例源代码,其中的应用程序文件将使用AES算法加密,它将不能被文件管理器中的用户访问,但一旦用户进入应用程序,它将以真实的形式解密,并且工作正常。

代码语言:javascript
复制
package com.filepermition.android;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;

import javax.crypto.Cipher;
import javax.crypto.CipherInputStream;
import javax.crypto.CipherOutputStream;
import javax.crypto.KeyGenerator;
import javax.crypto.SecretKey;

import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;

public class AndroidFilePermitionActivity extends Activity 
{
    Button btn_button;

    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        btn_button = (Button)findViewById(R.id.btn_button);

        btn_button.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {

                try{
                    FileInputStream fis = new FileInputStream(
                        new File("/mnt/sdcard/testfile/file.wav"));
                    File outfile = new File("/mnt/sdcard/testfile/encTest1234.wav");

                    int read;
                    if(!outfile.exists())
                        outfile.createNewFile();

                    File decfile = new File("/mnt/sdcard/testfile/dec123.wav");
                    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();

                }catch (Exception e) {
                    // TODO: handle exceptione
                    e.printStackTrace();
                }
            }
        });
    }
}
票数 3
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/9156545

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档