aes 工具类

/**
 *
 */
package com.hlmedicals.app.util;


import javax.crypto.Cipher;
import javax.crypto.spec.SecretKeySpec;

import com.itextpdf.xmp.impl.Base64;

public class CryptographyTool {
    private static CryptographyTool instance = new CryptographyTool();

    private CryptographyTool() {
    }

    public static CryptographyTool getInstance() {
        return instance;
    }
public static void main(String[] args) {
    String testStr = "ISoGK6h4zdhAk1bKiFpogT+zmza5I9q97hyJmVd+2Sg=";
    CryptographyTool ct = new CryptographyTool();
    try {
        //System.out.println(ct.encrypt(testStr));
        
        System.out.println(ct.decrypt(testStr));
    } catch (Exception e) {
        e.printStackTrace();
    }
}
    /**
     * 加密方法
     *
     * @return
     * @throws Exception
     */
    public String encrypt(String encryptString) throws Exception {
        // 判断Key是否为16位
        byte[] raw = IConst.KEY.getBytes("utf-8");
        SecretKeySpec skeySpec = new SecretKeySpec(raw, "AES");
        Cipher cipher = Cipher.getInstance("AES/ECB/PKCS5Padding");// "算法/模式/补码方式"
        cipher.init(Cipher.ENCRYPT_MODE, skeySpec);
        byte[] encrypted = cipher.doFinal(encryptString.getBytes("utf-8"));
        return new String(new Base64().encode(encrypted));
    }

    /**
     * 解密方法
     *
     * @param decryptString
     * @return
     * @throws Exception
     */
    public String decrypt(String decryptString) throws Exception {
        byte[] raw = IConst.KEY.getBytes("utf-8");
        SecretKeySpec skeySpec = new SecretKeySpec(raw, "AES");
        Cipher cipher = Cipher.getInstance("AES/ECB/PKCS5Padding");
        cipher.init(Cipher.DECRYPT_MODE, skeySpec);
        byte[] encrypted1 = new Base64().decode(decryptString.getBytes());// 先用base64解密
        byte[] original = cipher.doFinal(encrypted1);
        String originalString = new String(original, "utf-8");
        return originalString;
    }
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值