java实现AES方式加密
admin
2023-01-31 05:41:57
0
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;
import org.apache.commons.codec.binary.Base64;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.net.URLDecoder;
import java.net.URLEncoder;
import java.security.InvalidAlgorithmParameterException;
import java.security.InvalidKeyException;
import java.security.NoSuchAlgorithmException;
public class AESUtil {
    private static final String IV_STRING = "sdf4ddfsFD86Vdf2";
    private static final String encoding = "UTF-8";
    public static String encryptAES(String content, String key)
            throws InvalidKeyException, NoSuchAlgorithmException,
            NoSuchPaddingException, UnsupportedEncodingException,
            InvalidAlgorithmParameterException, IllegalBlockSizeException, BadPaddingException {
        byte[] byteContent = content.getBytes(encoding);
        // 注意,为了能与 iOS 统一
        // 这里的 key 不可以使用 KeyGenerator、SecureRandom、SecretKey 生成
        byte[] enCodeFormat = key.getBytes(encoding);
        SecretKeySpec secretKeySpec = new SecretKeySpec(enCodeFormat, "AES");
        byte[] initParam = IV_STRING.getBytes(encoding);
        IvParameterSpec ivParameterSpec = new IvParameterSpec(initParam);
        // 指定加密的算法、工作模式和填充方式
        Cipher cipher = Cipher.getInstance("AES/CBC/PKCS5Padding");
        cipher.init(Cipher.ENCRYPT_MODE, secretKeySpec, ivParameterSpec);
        byte[] encryptedBytes = cipher.doFinal(byteContent);
        // 同样对加密后数据进行 base64 编码
        String base64 = new Base64().encodeToString(encryptedBytes);
        //进行url编码 去掉= ? &
        return URLEncoder.encode(base64,encoding);
    }
    public static String decryptAES(String content, String key)
            throws InvalidKeyException, NoSuchAlgorithmException,
            NoSuchPaddingException, InvalidAlgorithmParameterException,
            IllegalBlockSizeException, BadPaddingException, IOException {
        //URL解码
        content = URLDecoder.decode(content,encoding);
        // base64 解码
        byte[] encryptedBytes = Base64.decodeBase64(content);
        byte[] enCodeFormat = key.getBytes(encoding);
        SecretKeySpec secretKey = new SecretKeySpec(enCodeFormat, "AES");
        byte[] initParam = IV_STRING.getBytes(encoding);
        IvParameterSpec ivParameterSpec = new IvParameterSpec(initParam);
        Cipher cipher = Cipher.getInstance("AES/CBC/PKCS5Padding");
        cipher.init(Cipher.DECRYPT_MODE, secretKey, ivParameterSpec);
        byte[] result = cipher.doFinal(encryptedBytes);
        return new String(result, encoding);
    }
    
    public static void main(String[] args) throws Exception {
    JSONObject json = new JSONObject();
    json.put("custNum", "111111");
    /*json.put("name", "张三");
    json.put("cityCode", "100001");
    json.put("cityName", "北京市");
    json.put("mobileNo", "15651876590");*/
        String content = json.toJSONString();
        System.out.println("加密前:" + content);
        String key = "djadiKJdj49dFJLd";
        System.out.println("加密密钥和解密密钥:" + key);
        String encrypt = encryptAES(content, key);
        System.out.println("加密后:" + encrypt);
        String decrypt = decryptAES(encrypt , key);
        System.out.println("解密后:" + decrypt);
    }
}


相关内容

热门资讯

第16次登榜《财富》中国500... 宇通客车第16次上榜《财富》中国500强位列榜单第362位,排名较去年上升13名自2010年首次登榜...
填报志愿没看清,山东644分考... 极目新闻记者 柳琛琛今年高考考了644分,本以为能冲刺厦门大学本部,没想到填报志愿时没看清楚,被录取...
为抗议毒油案蓝营青年绝食第5天... 国民党籍台北市议员杨植斗等4名青年为抗议毒油案于台北市凯达格兰大道绝食静坐,23日将迈入第五天。杨植...
西门子洗衣机怎么恢复出厂设置 西门子洗衣机怎么恢复出厂设置如果洗衣机是开机模式,先按电源关机键,等洗衣机关机后,然后重新按开机键,...
空调程序乱了怎么办 只需要把空调的电源拔下来,然后大概十分钟之后,再将空调的插头插上去,开启空调就可以了,系统自动重新启...
海尔洗衣机程序乱了怎么复位 海尔洗衣机程序乱了怎么复位:海尔洗衣机程序乱了,针对这种情况,先关闭一下洗衣机的电源,然后重新接通洗...
夏新洗衣机程序乱了怎么办 夏新洗衣机程序乱了,是很常见的问题。通常情况下,这可能是由于电子控制面板出现故障,或者是洗衣机内部发...
西门子洗衣机自动暂停是怎么回事 西门子洗衣机自动暂停可能有以下原因:1. 电源问题:检查电源线是否插好,电源是否稳定。2. 过载保护...
最短的黄金时代:韩国杠杆一代的... 在两个月前,整个韩国社会只有两种人:一种是买了海力士和三星股票的,一种是没买的。买了股票的人进入“黄...