在加密和解密的时候我们都依赖于一个key,这个key的生成会决定着加密效果。key可以保存在内存中或者以二进制的形式写入到文件,每次当程序启动时,从文件读取到内存中。key的生成,我们可以自定义一些规则,比如当前用户的密码+特定字符串,当前deviceId+特定字符串等等。这个就自定义了。
public static byte[] encrypt(byte[] key,byte[]data) { @H_301_19@SecretKeySpec sKeySpec = new @H_301_19@SecretKeySpec(key,"AES"); @H_301_19@Cipher cipher; byte[] encryptedData = null; try { cipher = @H_301_19@Cipher.getInstance("AES"); cipher.init(@H_301_19@Cipher.ENCRYPT_MODE,sKeySpec); encryptedData = cipher.doFinal(data); } catch (@H_301_19@Exception e) { @H_301_19@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) { @H_301_19@SecretKey sKey null; @H_301_19@KeyGenerator keyGen; try { keyGen = @H_301_19@KeyGenerator"AES"); @H_301_19@SecureRandom random = @H_301_19@SecureRandom"SHA1PRNG"); random.setSeed(randomNumberSeed); sKey = keyGen.generateKey(); } catch (@H_301_19@NoSuchAlgorithmException e) { @H_301_19@Log"generateKey exception" return sKey.getEncoded(); } //调用的demo void testAES() { @H_301_19@String randomNumberSeed = "aaaa"; @H_301_19@String data "Hello World"; byte [] key = generateKey(randomNumberSeed.getBytes()); byte [] encryptData = encrypt(key,data.getBytes()); @H_301_19@String str = @H_301_19@Base64.encodeToString(encryptData,@H_301_19@Base64.DEFAULT); @H_301_19@Log"encrypt data:" + str); byte [] decodeData .decode(str,93)">+ new @H_301_19@String(decrypt(key,decodeData))); }引用:https://github.com/fred-ye/summary/issues/47
参考:
安卓sqlite加密---第三方开源-sqlCipher
http://www.bafenbaosoft.com/post/11.htmlhttp://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