安卓高强度本地数据加密指北

本文写于安卓P、Q流行于市面期间,适合人群:

不想写so而又想加密本地数据有一定安全强度。

 

对于对称加密,AES作为DES的升级版,安全性得到提升,是现在本地加密的主流。本文给出AES的最简实践源码。

做这个的起因是因为东家的加密方法是使用so库,优点是性能过得去,不依赖安卓版本,不用频繁改动,但是缺点就是秘钥存储在本地,存在本地的内容即使扔到so,又或者拆成几块,也还是可以反出来的。写代码拆秘钥也是神烦的。

把秘钥的生成以及保存交给系统对于我这种懒人来说还是比较省心的。

使用系统AES缺点就是安卓M以前还要回退到其他方案,还有每次加密后要保存一个initializationVector,在解密时要使用这个变量。

安卓M以上的版本实现参考下面这个大神的译文,链接:

https://www.jianshu.com/p/dc5a9f906eb8

整理片段,给出完整代码:

@SuppressLint("NewApi")
public class TestAes {

    public static final String ANDROID_KEY_STORE = "AndroidKeyStore";
    public static final String TRANSFORMATION = "AES/GCM/NoPadding";
    private static final String ALIAS = "i_am_alias";
    private byte[] initializationVector;

    public void test(){
        try {
            genKey();

            String e = encode("abcdefghijklmnopqrstuvwxyz");
            log("encode = " + e);

            String d = decode(e);
            log("decode = " + d);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    private void genKey() throws Exception {
        KeyStore keyStore = KeyStore.getInstance(ANDROID_KEY_STORE);
        keyStore.load(null);

        KeyGenParameterSpec keyGenParameterSpec = new KeyGenParameterSpec.Builder(ALIAS,
                KeyProperties.PURPOSE_DECRYPT | KeyProperties.PURPOSE_ENCRYPT)
                .setBlockModes(KeyProperties.BLOCK_MODE_GCM)
                .setEncryptionPaddings(KeyProperties.ENCRYPTION_PADDING_NONE)
                .build();

        KeyGenerator kg = KeyGenerator.getInstance(KeyProperties.KEY_ALGORITHM_AES, ANDROID_KEY_STORE);
        kg.init(keyGenParameterSpec);
        SecretKey secretKey = kg.generateKey();
        log("key generated: " + secretKey.toString());
    }

    private SecretKey getKey() throws Exception {
        KeyStore keyStore = KeyStore.getInstance(ANDROID_KEY_STORE);
        keyStore.load(null);
        KeyStore.Entry entry = keyStore.getEntry(ALIAS, null);

        KeyStore.SecretKeyEntry secretKeyEntry = (KeyStore.SecretKeyEntry) entry;
        SecretKey secretKey = secretKeyEntry.getSecretKey();
        return secretKey;
    }

    private String encode(String src) throws Exception {
        SecretKey secretKey = getKey();

        Cipher cipher = Cipher.getInstance(TRANSFORMATION);
        cipher.init(Cipher.ENCRYPT_MODE, secretKey);

        this.initializationVector = cipher.getIV();

        byte[] result = cipher.doFinal(src.getBytes(StandardCharsets.UTF_8));
        return Base64Utils.encode(result);
    }

    private String decode(String src) throws Exception {
        SecretKey secretKey = getKey();

        GCMParameterSpec spec = new GCMParameterSpec(128, this.initializationVector);

        Cipher cipher = Cipher.getInstance(TRANSFORMATION);
        cipher.init(Cipher.DECRYPT_MODE, secretKey, spec);

        byte[] result = cipher.doFinal(Base64Utils.decode(src));
        return new String(result, StandardCharsets.UTF_8);
    }

    private void log(String log){
        Log.i("aes ", log);
    }
}

可耻地给两个传送门:

要研究老版本怎么兼容的,可以看这篇,不过这篇只有秘钥怎么生成的代码:

https://richardcao.me/2019/03/14/Android-Local-Security-Storage/

要参考下老版本代码怎么撸的,看这篇:

https://blog.csdn.net/q4878802/article/details/76690493

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值