Android 对内容加密

想要高大上的加密,请划走。

在APP中,使用SQDlite进行本地存储,有的时候为安全提供加密。这里就讨论使用

Java : javax.crypto.Cipher

首先,需要有一个二级key str="xxxxxxxx",使用Base64生成密钥

byte[] encodedKey = Base64.decode(str,Base64.DEFAULT)

再使用SecretKey 生成加密key供加密解密使用

SecretKey secretKey = new SecretKeySpec(encodedKey,0,encodedKey.length,)

加密操作:

String encipher(String str) {
try{
cipher.init(Cipher.ENCRYPT_MODE, secretKeey)
byte[] data = cipher.doFinal(str.getBytes());
return Base64.encodeToString(data, Base64. DEFAULT);
} catch (IllegalBlockSizeException |
BadPaddingException | InvalidKeyException e)(
Log.e("encipher", "Exception:" + e);
return "";

解密操作

public String decryption(String dbData) {
try
cipher.init(Cipher.DECRYPT_MODE, secretkey)
byte[] decryptionData = cipher.doFinal(Base64.decode(dbDat:a, Base64.DEFAULT))
return new String(decryptionData);
catch (InvalidKeyException | IllegalBlockSizeException | BadPaddingException e)
Log.e(tag: "decryption", msg: Exception: + e);
return ""

  • 3
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值