2015-07-13 121 views
0

我想使用此代碼在SQLite數據庫來存儲密碼:如何在Android中的SQLite數據庫中存儲AES密碼?

public static String encrypt(String Data) throws Exception { 
    Key key = generateKey(); 
    Cipher c = Cipher.getInstance(ALGO); 
    c.init(Cipher.ENCRYPT_MODE, key); 
    byte[] encVal = c.doFinal(Data.getBytes()); 
    String encryptedValue = new BASE64Encoder().encode(encVal); 
    return encryptedValue; 
} 

public static String decrypt(String encryptedData) throws Exception { 
    Key key = generateKey(); 
    Cipher c = Cipher.getInstance(ALGO); 
    c.init(Cipher.DECRYPT_MODE, key); 
    byte[] decordedValue = new BASE64Decoder().decodeBuffer(encryptedData); 
    byte[] decValue = c.doFinal(decordedValue); 
    String decryptedValue = new String(decValue); 
    return decryptedValue; 
} 

private static Key generateKey() throws Exception { 
    Key key = new SecretKeySpec(keyValue, ALGO); 
    return key; 
} 

這個字節數組作爲關鍵

private static final byte[] keyValue = 
     new byte[] { 'T', 'h', 'e', 'B', 'e', 's', 't', 
      'S', 'e', 'c', 'r','e', 't', 'K', 'e', 'y' }; 

但問題是在哪裏存儲關鍵,因爲使用關鍵的,任何人都可以提前對數據進行解密,並導致安全問題..

請幫我....謝謝..

回答

1

使用用戶生成的密鑰,如引腳屏幕。當應用程序開始向用戶請求密鑰時,您不需要存儲它,然後將其保存在內存中,直到應用程序關閉。

0

無論你在哪裏PLAC事e你的祕密鑰匙。如果我可以反編譯並運行你的代碼,那麼我只需要在創建密鑰後將斷點放在一行就可以了。沒有任何工作方法可以將加密數據放入代碼中,並使其僅供您的代碼使用。

相關問題