Android中常用的加密算法——AES加密

上一篇博客介绍了MD5加密算法,MD5加密不可逆的特性决定了在很多场景下并不适用。如在某些需要对加密后的密文进行解密使之可读的场景下,就需要使用可逆加密算法实现,常用的可逆加密算法有:

  • AES对称加密算法
  • RSA非对称加密算法

对称与非对称

对称加密

对称密钥加密(英语:Symmetric-key algorithm)又称为对称加密、私钥加密、共享密钥加密,是密码学中的一类加密算法。这类算法在加密和解密时使用相同的密钥,或是使用两个可以简单地相互推算的密钥。事实上,这组密钥成为在两个或多个成员间的共同秘密,以便维持专属的通信联系。与公开密钥加密相比,要求双方获取相同的密钥是对称密钥加密的主要缺点之一。维基百科

非对称加密

公开密钥加密(英语:Public-key cryptography),也称为非对称加密(英语:asymmetric cryptography),是密码学的一种算法,它需要两个密钥,一个是公开密钥,另一个是私有密钥;一个用作加密的时候,另一个则用作解密。使用其中一个密钥把明文加密后所得的密文,只能用相对应的另一个密钥才能解密得到原本的明文;甚至连最初用来加密的密钥也不能用作解密。由于加密和解密需要两个不同的密钥,故被称为非对称加密;不同于加密和解密都使用同一个密钥的对称加密。虽然两个密钥在数学上相关,但如果知道了其中一个,并不能凭此计算出另外一个;因此其中一个可以公开,称为公钥,任意向外发布;不公开的密钥为私钥,必须由用户自行严格秘密保管,绝不透过任何途径向任何人提供,也不会透露给要通信的另一方,即使他被信任。维基百科

非对称加密的安全性更高,但对称加密的加密速度更快而且快得多。


AES

  • 高级加密标准(英语:Advanced Encryption Standard,缩写:AES),在密码学中又称Rijndael加密法,是美国联邦政府采用的一种区块加密标准。这个标准用来替代原先的DES,已经被多方分析且广为全世界所使用。

算法实现

生成秘钥
AES加密支持128、192、256位秘钥长度,通常使用AES-128。注意:当秘钥长度不是128、192、256比特时会报错抛出异常。
Android提供了KeyGenerator用于帮助我们生成秘钥。

    /**
     * 生产秘钥
     * @param seed 种子
     * @return the secretKey
     * @throws Exception
     */
    public static SecretKey generateKey(String seed) throws Exception {
        // 获取秘钥生成器
        KeyGenerator keyGenerator = KeyGenerator.getInstance("AES");
        // 通过种子初始化
        SecureRandom secureRandom = new SecureRandom();
        secureRandom.setSeed(seed.getBytes("UTF-8"));
        //AES-128
        keyGenerator.init(128, secureRandom);
        // 生成秘钥并返回
        return keyGenerator.generateKey();
    }

即使种子相同生成的秘钥也是不同的,对密文进行解密时必须使用同一个秘钥。秘钥可以不用KeyGenerator生成,任意16位、24位、32位字符串都可以作为秘钥。

加密实现

    /**
     * 用秘钥进行加密
     * @param content   明文
     * @param secretKey 秘钥
     * @return byte数组的密文
     * @throws Exception
     */
    public static byte[] encrypt(String content, SecretKey secretKey) throws Exception {
        // 秘钥
        byte[] enCodeFormat = secretKey.getEncoded();
        return encrypt(content, enCodeFormat);
    }

    /**
     * 用秘钥进行加密
     * @param content   明文
     * @param secretKeyEncoded 秘钥Encoded
     * @return byte数组的密文
     * @throws Exception
     */
    public static byte[] encrypt(String content, byte[] secretKeyEncoded) throws Exception {
        // 创建AES秘钥
        SecretKeySpec key = new SecretKeySpec(secretKeyEncoded, "AES");
        // 创建密码器
        Cipher cipher = Cipher.getInstance("AES/CBC/PKCS5Padding");
        // Cipher cipher = Cipher.getInstance("AES");
        // 初始化加密器
        cipher.init(Cipher.ENCRYPT_MODE, key, new IvParameterSpec(new byte[cipher.getBlockSize()]));
        // 加密
        return cipher.doFinal(content.getBytes("UTF-8"));
    }


    /**
     * 用秘钥进行加密返回16进制字符串
     * @param content      明文
     * @param secretKey    秘钥
     * @return 密文的16进制字符串
     * @throws Exception
     */
    public static String encryptToStr(String content, SecretKey secretKey)   
                         throws Exception {
        byte[] result = encrypt(content, secretKey);
        return HexUtil.encode(result);
    }

解密实现

    /**
     * 解密
     * @param content   密文
     * @param secretKeyEncoded 秘钥
     * @return 解密后的明文
     * @throws Exception
     */
    public static byte[] decrypt(byte[] content, byte[] secretKeyEncoded) throws Exception {
        // 创建AES秘钥
        SecretKeySpec key = new SecretKeySpec(secretKeyEncoded, "AES");
        // 创建密码器
        Cipher cipher = Cipher.getInstance("AES/CBC/PKCS5Padding");
        // 初始化解密器
        cipher.init(Cipher.DECRYPT_MODE, key, new IvParameterSpec(new byte[cipher.getBlockSize()]));
        // 解密
        return cipher.doFinal(content);
    }

    /**
     * 解密
     * @param content   密文
     * @param secretKey 秘钥
     * @return 解密后的明文
     * @throws Exception
     */
    public static byte[] decrypt(byte[] content, SecretKey secretKey) throws Exception {
        // 秘钥
        byte[] enCodeFormat = secretKey.getEncoded();
        return decrypt(content,enCodeFormat);
    }

    /**
     * 解密
     * @param content
     * @param secretKey
     * @return
     * @throws Exception
     */
    public static String decryptToStr(byte[] content, SecretKey secretKey) throws Exception {
        byte[] result = decrypt(content, secretKey);
        return new String(result);
    }

加密模式与填充方式

这两个比较复杂就不深入去探讨了。
参考这几篇文章:

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值