SQlite password encryption for Android?

So, I got an idea for an Android application (for training only), since I was just getting started. it will be basically an application that allows your “vault / vault” your passwords that you need to remember. But it will encrypt / decrypt them through SQlite (which will be the storage medium). What types of encryption can Android / SQlite3 have?

+4
source share
2 answers

You probably want to use the javax.crypto package, and then save the encrypted data in sqlite3 lines.

This provides symmetric encryption, allowing your user to enter a password that will unlock content in a database that has been encrypted with this password.

+3
source

I am using apache commons Base64 to encode an encrypted password. You end up storing the password in db as Blob.

import javax.crypto.Cipher; import javax.crypto.SecretKey; import javax.crypto.SecretKeyFactory; import javax.crypto.spec.DESKeySpec; import org.apache.commons.net.util.Base64; private static SecretKey key; try { byte[] secretBytes = "secret key".getBytes("UTF8"); DESKeySpec keySpec = new DESKeySpec(secretBytes); SecretKeyFactory keyFactory = SecretKeyFactory.getInstance("DES"); key = keyFactory.generateSecret(keySpec); } catch (Exception e) { Log.e(Flashum.LOG_TAG, "DatabaseHelper " + e.toString()); } public byte[] encryptPassword(String userpw) { try { byte[] cleartext = userpw.getBytes("UTF8"); Cipher cipher = Cipher.getInstance("DES"); cipher.init(Cipher.ENCRYPT_MODE, key); byte[] clearBytes = cipher.doFinal(cleartext); byte[] encryptedPwd = Base64.encodeBase64(clearBytes); return encryptedPwd; } catch (Exception e) { Log.e(Flashum.LOG_TAG, "DatabaseHelper " + e.toString()); } return null; } public String decryptPassword(byte[] userpw) { String pw = ""; try { byte[] encrypedPwdBytes = Base64.decodeBase64(userpw); Cipher cipher = Cipher.getInstance("DES"); cipher.init(Cipher.DECRYPT_MODE, key); byte[] plainTextPwdBytes = cipher.doFinal(encrypedPwdBytes); pw = new String(plainTextPwdBytes, "UTF8"); } catch (Exception e) { Log.e(Flashum.LOG_TAG, "DatabaseHelper " + e.toString()); } return pw; } 
+4
source

Source: https://habr.com/ru/post/1341313/


All Articles