AES 256加密

/**
 * AES256 算法
 * CBC 模式
 * NoPadding 无填充模式
 * CBC模式需要添加一个参数iv
 *
 * 介于java 不支持PKCS7Padding,只支持PKCS5Padding 但是PKCS7Padding 和 PKCS5Padding 没有什么区别
 * 要实现在java端用PKCS7Padding填充,需要用到bouncycastle组件来实现
 */
public class AesEncoder {
    /*
     * 加密用的Key 可以用26个字母和数字组成 此处使用AES-128-CBC加密模式,key需要为16位。
     */
    private byte[] ivParameter = {0x68, 0x7c, 0x39, 0x30, 0x2a, 0x71, 0x51, 0x5e, 0x0f, 0x13, 0x2a, 0x76, 0x4d, 0x76, 0x61, 0x7b};
    public static byte[] cSrc = {0x6c, 0x26, 0x66, 0x4e, 0x23, 0x1c, 0x76, 0x6c, 0x4f, 0x67, 0x37, 0x6d, 0x12, 0x3e};
    private static AesEncoder instance = null;

    private AesEncoder() {

    }

    public static AesEncoder getInstance() {
        if (instance == null)
            instance = new AesEncoder();
        return instance;
    }

    // 加密
    public byte[] encrypt(byte[]  sSrc, String sKey) throws Exception {
        Cipher cipher = Cipher.getInstance("AES/CBC/NoPadding");
        byte[] raw = sKey.getBytes();
        SecretKeySpec skeySpec = new SecretKeySpec(raw, "AES");
        IvParameterSpec iv = new IvParameterSpec(ivParameter);// 使用CBC模式,需要一个向量iv,可增加加密算法的强度
        cipher.init(Cipher.ENCRYPT_MODE, skeySpec, iv);
        return cipher.doFinal(sSrc);
    }

    // 解密
    public String decrypt(byte[] sSrc, String sKey) throws Exception {
        try {
            byte[] raw = sKey.getBytes("ASCII");
            SecretKeySpec skeySpec = new SecretKeySpec(raw, "AES");
            Cipher cipher = Cipher.getInstance("AES/CBC/NoPadding");
            IvParameterSpec iv = new IvParameterSpec(ivParameter);
            cipher.init(Cipher.DECRYPT_MODE, skeySpec, iv);
            //byte[] encrypted1 = new BASE64Decoder().decodeBuffer(sSrc);// 先用base64解密
            byte[] original = cipher.doFinal(sSrc);
            String originalString = new String(original, "utf-8");
            return originalString;
        } catch (Exception ex) {
            return null;
        }
    }

    public static void main(String[] args) throws Exception {
        // 需要加密的字串
        byte[] cSrc = {0x10, 0x00, 0x6c, 0x26, 0x66, 0x4e, 0x23, 0x1c, 0x76, 0x6c, 0x4f, 0x67, 0x37, 0x6d, 0x12, 0x3e};
        //String cSrc = "123456789";
       /* System.out.print("加密前的字串是:");
        for (byte b : cSrc) {
            System.out.print(b);
        }*/
        System.out.println();
        // 加密
        byte[] enString = AesEncoder.getInstance().encrypt(cSrc,"0123456789123456");
        System.out.println("加密后的字串长度是:" + enString.length);
        for (byte b : enString) {
            System.out.printf("%02x ",b);
        }
        byte[] bSrc = {0x10, 0x00, 0x6c, 0x26, 0x66, 0x4e, 0x23, 0x1c, 0x76, 0x6c, 0x4f, 0x67, 0x37, 0x6d, 0x12, 0x3e};
        System.out.println("数组是否相等:"+Arrays.equals(enString,bSrc));
        String desc = AesEncoder.getInstance().decrypt(enString,"0123456789123456");
       System.out.println("解密后的字串是:" + desc);
    }
}
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值