简体   繁体   English

Android SQlite密码加密?

[英]Android SQlite password encryption?

So I had an idea for an Android App (just for learning) since im just starting out. 所以我对Android应用程序(仅供学习)有了一个想法,因为我刚刚开始。 it would basically be an app that lets your "store/vault" your passwords you need to remember. 它基本上是一个应用程序,让你“存储/保管”你需要记住的密码。 But it would encrypt/decrypt them through SQlite (which would be the storage median). 但它会通过SQlite(这将是存储中位数)对它们进行加密/解密。 What types of encryption can Android/SQlite3 do? Android / SQlite3可以进行哪些类型的加密?

I use apache commons Base64 for encoding the encrypted password. 我使用apache commons Base64来编码加密密码。 You end up storing the password in the db as a Blob. 您最终将密码作为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;
   }

You probably want to use Android's javax.crypto package and then store the encrypted data in sqlite3 rows. 您可能想要使用Android的javax.crypto包,然后将加密数据存储在sqlite3行中。

This provides symmetric encryption, allowing your user to enter a password which would unlock content in the database that was encrypted with that password. 这提供了对称加密,允许您的用户输入密码,该密码可以解锁使用该密码加密的数据库中的内容。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM