How to Secure Android Shared Preferences?

前端 未结 9 2166
我在风中等你
我在风中等你 2020-11-28 22:11

The common location where SharedPreferences are stored in Android apps is:

/data/data//shared_prefs/
         


        
9条回答
  •  野性不改
    2020-11-28 22:46

    public class NodeCrypto {
    
            private String iv = "fedcba9876543210";//Dummy iv (CHANGE IT!)
            private IvParameterSpec ivspec;
            private SecretKeySpec keyspec;
            private Cipher cipher;
    
            private String SecretKey = "0123456789abcdef";//Dummy secretKey (CHANGE IT!)
    
            public void doKey(String key)
            {
                    ivspec = new IvParameterSpec(iv.getBytes());
    
                    key = padRight(key,16);
    
                    Log.d("hi",key);
    
                    keyspec = new SecretKeySpec(key.getBytes(), "AES");
    
                    try {
                            cipher = Cipher.getInstance("AES/CBC/NoPadding");
                    } catch (NoSuchAlgorithmException e) {
                            // TODO Auto-generated catch block
                            e.printStackTrace();
                    } catch (NoSuchPaddingException e) {
                            // TODO Auto-generated catch block
                            e.printStackTrace();
                    }
            }
    
            public byte[] encrypt(String text,String key) throws Exception
            {
                    if(text == null || text.length() == 0)
                            throw new Exception("Empty string");
    
                    doKey(key);
    
                    byte[] encrypted = null;
    
                    try {
                            cipher.init(Cipher.ENCRYPT_MODE, keyspec, ivspec);
    
                            encrypted = cipher.doFinal(padString(text).getBytes());
                    } catch (Exception e)
                    {                       
                            throw new Exception("[encrypt] " + e.getMessage());
                    }
    
                    return encrypted;
            }
    
            public byte[] decrypt(String code,String key) throws Exception
            {
                    if(code == null || code.length() == 0)
                            throw new Exception("Empty string");
    
                    byte[] decrypted = null;
    
                    doKey(key);
    
                    try {
                            cipher.init(Cipher.DECRYPT_MODE, keyspec, ivspec);
    
                            decrypted = cipher.doFinal(hexToBytes(code));
                    } catch (Exception e)
                    {
                            throw new Exception("[decrypt] " + e.getMessage());
                    }
                    return decrypted;
            }
    
    
    
            public static String bytesToHex(byte[] data)
            {
                    if (data==null)
                    {
                            return null;
                    }
    
                    int len = data.length;
                    String str = "";
                    for (int i=0; i

提交回复
热议问题