Option 1 : use SQLcipher .
Option 2 : The safest method ever is no chance to hack. This is not perfect, but better than nothing.
1) Insert data using this function:
public static String getEncryptedString(String message) { String cipherText = null; try { Cipher cipher = Cipher.getInstance("AES/ECB/PKCS5Padding"); cipher.init(Cipher.ENCRYPT_MODE, new SecretKeySpec(("YOUR-SECURE-PASSWORD-KEY").getBytes(), "AES")); byte[] bytes = cipher.doFinal(message.getBytes()); cipherText = Base64.encodeToString(bytes, Base64.DEFAULT); } catch(Exception ex) { cipherText = "Error in encryption"; Log.e(TAG , ex.getMessage()); ex.printStackTrace(); } return cipherText; }
2) Get data from the database and pass functions to this parameter:
//This function returns output string public static String getDecryptedString(String encoded) { String decryptString = null; try { byte[] bytes = Base64.decode(encoded, Base64.DEFAULT); Cipher cipher = Cipher.getInstance("AES/ECB/PKCS5Padding"); cipher.init(Cipher.DECRYPT_MODE, new SecretKeySpec(("YOUR-SECURE-PASSWORD-KEY").getBytes() , "AES")); decryptString = new String(cipher.doFinal(bytes), "UTF-8"); } catch(Exception ex) { decryptString = "Error in decryption"; ex.printStackTrace(); } return decryptString; }
3) The advantages of these methods: - It is impossible to decrypt without the correct key. - AES Encryption is a very secure encryption method.
4) Save the AES key in a c ++ file.
source share