Android SQlite password encryption?

前端 未结 2 1608
长发绾君心
长发绾君心 2020-12-15 01:48

So I had an idea for an Android App (just for learning) since im just starting out. it would basically be an app that lets your \"store/vault\" your passwords you need to re

相关标签:
2条回答
  • 2020-12-15 02:19

    I use apache commons Base64 for encoding the encrypted password. You end up storing the password in the db as a 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;
       }
    
    0 讨论(0)
  • 2020-12-15 02:26

    You probably want to use Android's javax.crypto package and then store the encrypted data in sqlite3 rows.

    This provides symmetric encryption, allowing your user to enter a password which would unlock content in the database that was encrypted with that password.

    0 讨论(0)
提交回复
热议问题