Android AES加密解密 CTR 模式

最近在搞AES加密 默认的一般是CBC模式,偏偏公司要用CTR模式,现总结如下,便于以后遇到使用

package com.example.administrator.shanghaijk.utils;
import org.apache.shiro.codec.Hex;
import java.security.InvalidAlgorithmParameterException;
import java.security.InvalidKeyException;
import java.security.Key;
import java.security.NoSuchAlgorithmException;
import javax.crypto.BadPaddingException;
import javax.crypto.Cipher;
import javax.crypto.IllegalBlockSizeException;
import javax.crypto.NoSuchPaddingException;
import javax.crypto.spec.IvParameterSpec;
import javax.crypto.spec.SecretKeySpec;
/**
 * Created by Jane on 2018/8/8.
 * AES CRT加密模式工具类
 */
public class AESCrtUtils {
    /*
     * AES CRT加密
     * @param content 要加密的内容
     * @param key     加密文件的秘钥
     * @param iv      加密的偏移量
     * @return  输出Hex十六进制再次加密的结果
     **/
    public static String encrypt(String content, String key, String iv) {
        String encodeStr = "";
        try {
            //产生密钥
            byte[] keyBytes = key.getBytes();
            //构建SecretKeySpec,初始化Cipher对象时需要该参数
            SecretKeySpec secretKeySpec = new SecretKeySpec(keyBytes, "AES");
            //构建Cipher对象,需要传入一个字符串,格式必须为"algorithm/mode/padding"或者"algorithm/",意为"算法/加密模式/填充方式"
            Cipher cipher = Cipher.getInstance("AES/CTR/PKCS5Padding");
            //初始化Cipher对象
            byte[] ivBytes = iv.getBytes();
            IvParameterSpec ivParameterSpec = new IvParameterSpec(ivBytes);
            cipher.init(Cipher.ENCRYPT_MODE, secretKeySpec, ivParameterSpec);
            //加密数据
            byte[] resultBytes = cipher.doFinal(content.getBytes());
            //结果用Hex十六进制转码
            encodeStr = new String(Hex.encode(resultBytes));
            return encodeStr;
        } catch (NoSuchAlgorithmException e) {
            e.printStackTrace();
        } catch (NoSuchPaddingException e) {
            e.printStackTrace();
        } catch (InvalidKeyException e) {
            e.printStackTrace();
        } catch (BadPaddingException e) {
            e.printStackTrace();
        } catch (IllegalBlockSizeException e) {
            e.printStackTrace();
        } catch (InvalidAlgorithmParameterException e) {
            e.printStackTrace();
        }
        return encodeStr;
    }

    /**
     * AES CTR 解密
     *
     * @param encode 加密过后的文件
     * @param iv     偏移量
     * @param key    秘钥文件
     */
    public static String decrypt(String encode, String iv, String key) {
        String decoded = "";
        try {
            byte[] bytes = Hex.decode(encode);
            IvParameterSpec ivSpec = new IvParameterSpec(
                    iv.toString().getBytes());
            Key keys = new SecretKeySpec(key.getBytes(), "AES");
            Cipher cipher = Cipher.getInstance("AES/CTR/PKCS5Padding");
            cipher.init(Cipher.DECRYPT_MODE, keys, ivSpec);
            byte[] ret = cipher.doFinal(bytes);
            decoded = new String(ret);
            return decoded.trim();
        } catch (NoSuchAlgorithmException e) {
            e.printStackTrace();
        } catch (InvalidKeyException e) {
            e.printStackTrace();
        } catch (InvalidAlgorithmParameterException e) {
            e.printStackTrace();
        } catch (NoSuchPaddingException e) {
            e.printStackTrace();
        } catch (IllegalBlockSizeException e) {
            e.printStackTrace();
        } catch (BadPaddingException e) {
            e.printStackTrace();
        }
        return decoded;
    }
}

 需要用的工具类 shiro-core-1.2.2.jar 下载地址 自行下载即可。。

猜你喜欢

转载自blog.csdn.net/zww986736788/article/details/81531859