Security with SharedPreferences

后端 未结 3 1350
一个人的身影
一个人的身影 2020-12-13 21:22

I am developing an application in which I have to store very sensitive data and it should not come in contact with the user. I got to know from this source that if a device

3条回答
  •  忘掉有多难
    2020-12-13 21:59

    You can use SharedPreference, if you encrypt your data.

    Here is an example to encryt/decrypt data :

    protected String encrypt( String value ) {
    
        try {
            final byte[] bytes = value!=null ? value.getBytes(UTF8) : new byte[0];
            SecretKeyFactory keyFactory = SecretKeyFactory.getInstance("PBEWithMD5AndDES");
            SecretKey key = keyFactory.generateSecret(new PBEKeySpec(SEKRIT));
            Cipher pbeCipher = Cipher.getInstance("PBEWithMD5AndDES");
            pbeCipher.init(Cipher.ENCRYPT_MODE, key, new PBEParameterSpec(Settings.Secure.getString(context.getContentResolver(),Settings.System.ANDROID_ID).getBytes(UTF8), 20));
            return new String(Base64.encode(pbeCipher.doFinal(bytes), Base64.NO_WRAP),UTF8);
    
        } catch( Exception e ) {
            throw new RuntimeException(e);
        }
    
    }
    
    protected String decrypt(String value){
        try {
            final byte[] bytes = value!=null ? Base64.decode(value,Base64.DEFAULT) : new byte[0];
            SecretKeyFactory keyFactory = SecretKeyFactory.getInstance("PBEWithMD5AndDES");
            SecretKey key = keyFactory.generateSecret(new PBEKeySpec(SEKRIT));
            Cipher pbeCipher = Cipher.getInstance("PBEWithMD5AndDES");
            pbeCipher.init(Cipher.DECRYPT_MODE, key, new PBEParameterSpec(Settings.Secure.getString(context.getContentResolver(),Settings.System.ANDROID_ID).getBytes(UTF8), 20));
            return new String(pbeCipher.doFinal(bytes),UTF8);
    
        } catch( Exception e) {
            throw new RuntimeException(e);
        }
    }
    

    Source : What is the most appropriate way to store user settings in Android application

提交回复
热议问题