加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

sqlite3加密

发布时间:2020-12-12 19:33:32 所属栏目:百科 来源:网络整理
导读:在加密和解密的时候我们都依赖于一个key,这个key的生成会决定着加密效果。key可以保存在内存中或者以二进制的形式写入到文件,每次当程序启动时,从文件读取到内存中。key的生成,我们可以自定义一些规则,比如当前用户的密码+特定字符串,当前deviceId+特定

在加密和解密的时候我们都依赖于一个key,这个key的生成会决定着加密效果。key可以保存在内存中或者以二进制的形式写入到文件,每次当程序启动时,从文件读取到内存中。key的生成,我们可以自定义一些规则,比如当前用户的密码+特定字符串,当前deviceId+特定字符串等等。这个就自定义了。

public static byte[] encrypt(byte[] key,byte[]data) {
        SecretKeySpec sKeySpec = new SecretKeySpec(key,"AES");
        Cipher cipher;
        byte[] encryptedData = null;
        try {
            cipher = Cipher.getInstance("AES");
            cipher.init(Cipher.ENCRYPT_MODE,sKeySpec);
            encryptedData = cipher.doFinal(data);
        } catch (Exception e) {
            Log.i(TAG,145)">"encrypt exception" + e.getMessage());
        }
        return encryptedData;
    }

    byte[] decrypt(byte [] decryptedData .DECRYPT_MODE,sKeySpec);
            decryptedData "decrypt exception" return decryptedData;
    }

    byte[] generateKey(byte[] randomNumberSeed) {
        SecretKey sKey null;
        KeyGenerator keyGen;
        try {
            keyGen = KeyGenerator"AES");
            SecureRandom random = SecureRandom"SHA1PRNG");
            random.setSeed(randomNumberSeed);
            sKey = keyGen.generateKey();
        } catch (NoSuchAlgorithmException e) {
            Log"generateKey exception" return sKey.getEncoded();
    }

    //调用的demo
    void testAES() {
        String randomNumberSeed = "aaaa";
        String data "Hello World";
        byte [] key = generateKey(randomNumberSeed.getBytes());
        byte [] encryptData = encrypt(key,data.getBytes());
        String str = Base64.encodeToString(encryptData,Base64.DEFAULT);
        Log"encrypt data:" + str);
        byte [] decodeData .decode(str,93)">+ new String(decrypt(key,decodeData)));
    }
引用:https://github.com/fred-ye/summary/issues/47

参考:

安卓Sqlite加密---第三方开源-sqlCipher

http://www.bafenbaosoft.com/post/11.html

http://icodingnotes.com/2015/03/09/Android%E7%AC%94%E8%AE%B0%E4%B9%8B%E5%AE%89%E5%85%A8%E7%AF%87%EF%BC%88%E9%9B%B6%EF%BC%89%E2%80%94%E2%80%94%E4%BD%BF%E7%94%A8SQLCipher%E5%8A%A0%E5%AF%86SQLite%E6%95%B0%E6%8D%AE%E5%BA%93/

SQLite全面学习(一)
http://www.codeceo.com/article/sqlite-learning-01.html
android-lite-orm https://github.com/litesuits/android-lite-orm

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读