android加密存储文件

       

随着APP 数量的不断扩大,手机个人信息也渐渐地成为公开的秘密。加密APP中的信息是也渐渐地成为必不可少的了。虽然有时加密很简单,但对于普通的用户(非技术发烧友)来说,已经很难破解了。JAVA提供了加解密支持--md5,DES,AES,PBE,RSA等很多加密算法。其中肯定会有适合的算法进行加解密。

以DES加密为例写的验证程序

demo下载:http://download.csdn.net/detail/maokunlove/5134498

DESEncrypt 是对数据加解密的工具类

public class DESEncrypt {
    Key mKey;

    public DESEncrypt() {
        getKey("abcd");//"abcd"是加密的KEY,也可以为其它的任意字符(包括数字或汉字),如果知道此KEY,并且知道是DES加密,那对方就可以解密了。最好不能公开
    }

    /**
     * According to the parameters generated KEY
     * @param strKey
     */
    public void getKey(String strKey) {
        try {
            KeyGenerator generator = KeyGenerator.getInstance("DES");
            generator.init(new SecureRandom(strKey.getBytes()));
            mKey = generator.generateKey();
            generator = null;
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    /**
     * Encrypt the string
     * @param strMing
     * @return
     */
    public String getEncString(String strMing) {
        String strMi = "";
        try {
            return byte2hex(getEncCode(strMing.getBytes()));
        } catch (Exception e) {
            e.printStackTrace();
        }
        return strMi;
    }

    /**
     * decrypt the string
     * @param strMi
     * @return
     */
    public String getDesString(String strMi) {
        String strMing = "";
        try {
            return new String(getDesCode(hex2byte(strMi.getBytes())));
        } catch (Exception e) {
            e.printStackTrace();
        }
        return strMing;
    }

    /**
     * Encrypt the byte number string
     * @param byteS
     * @return
     */
    private byte[] getEncCode(byte[] byteS) {
        byte[] byteFina = null;
        Cipher cipher;
        try {
            cipher = Cipher.getInstance("DES");
            cipher.init(Cipher.ENCRYPT_MODE, mKey);
            byteFina = cipher.doFinal(byteS);
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            cipher = null;
        }
        return byteFina;
    }

    /**
     * Decrypt the byte number string
     * @param byteD
     * @return
     */
    private byte[] getDesCode(byte[] byteD) {
        Cipher cipher;
        byte[] byteFina = null;
        try {
            cipher = Cipher.getInstance("DES");
            cipher.init(Cipher.DECRYPT_MODE, mKey);
            byteFina = cipher.doFinal(byteD);
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            cipher = null;
        }
        return byteFina;
    }

    /**
     * Convert one byte number to hexadecimal string
     * @param b
     * @return
     */
    public static String byte2hex(byte[] b) {
        String hs = "";
        String stmp = "";
        for (int n = 0; n < b.length; n++) {
            stmp = (java.lang.Integer.toHexString(b[n] & 0XFF));
            if (stmp.length() == 1)
                hs = hs + "0" + stmp;
            else
                hs = hs + stmp;
        }
        return hs.toUpperCase();
    }

    /**
     * Convert the hexadecimal string to one byte number
     *
     * @param b
     * @return
     */
    public static byte[] hex2byte(byte[] b) {
        if ((b.length % 2) != 0)
            throw new IllegalArgumentException();
        byte[] b2 = new byte[b.length / 2];
        for (int n = 0; n < b.length; n += 2) {
            String item = new String(b, n, 2);
            b2[n / 2] = (byte) Integer.parseInt(item, 16);
        }
        return b2;
    }
}

MainActivity验证是否加解密

public class MainActivity extends Activity implements OnClickListener {

    private static final String TAG = "MainActivity";
    static final int READ_BLOCK_SIZE = 100;
    
    EditText mInput;
    TextView mFileText;
    Button mSave,mGetSave;
    String fileName = "file.dat";//文件名最好是那种特殊的后缀名的,即使导出来也没有工具打开(对于一般用户来说),也可以进行加密
    
    DESEncrypt mDESEncrypt;
    
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        mInput = (EditText) findViewById(R.id.input_view);
        mFileText = (TextView) findViewById(R.id.file_text);
        mSave = (Button) findViewById(R.id.save);
        mSave.setOnClickListener(this);
        mGetSave = (Button) findViewById(R.id.get_save);
        mGetSave.setOnClickListener(this);
        
        mDESEncrypt = new DESEncrypt();
    }
    @Override
    public void onClick(View view) {
        switch(view.getId()){
        case R.id.save:
            saveTofile(fileName,mInput.getText().toString());
            break;
        case R.id.get_save:
            getFileText();
            break;
        }
    }
    
    private void saveTofile(String fileName,String text) {
        try {
            FileOutputStream fOut;
            fOut = openFileOutput(fileName, MODE_WORLD_READABLE);

            OutputStreamWriter osw = new OutputStreamWriter(fOut);

            osw.write(mDESEncrypt.getEncString(text));
            osw.flush();
            osw.close();

            Toast.makeText(getBaseContext(), "File saved successfully!",Toast.LENGTH_SHORT).show();

            mInput.setText("");
        } catch (Exception ioe) {
            ioe.printStackTrace();
        }
    }
    
    private void getFileText() {
        try {
            FileInputStream fIn = openFileInput(fileName);
            InputStreamReader isr = new InputStreamReader(fIn);

            char[] inputBuffer = new char[READ_BLOCK_SIZE];
            String s = "";

            int charRead;
            while ((charRead = isr.read(inputBuffer)) > 0) {
                String readString = String.copyValueOf(inputBuffer, 0, charRead);
                s += readString;

                inputBuffer = new char[READ_BLOCK_SIZE];
            }

        } catch (Exception ioe) {
            ioe.printStackTrace();
        }
    }

范例下载:http://download.csdn.net/detail/maokunlove/5134498



  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值