zoukankan      html  css  js  c++  java
  • AES工具类

    package com.sinosoft.cms.common.util;
    import org.apache.commons.lang3.StringUtils;

    import net.sf.json.JSONObject;

    import javax.crypto.BadPaddingException;
    import javax.crypto.Cipher;
    import javax.crypto.IllegalBlockSizeException;
    import javax.crypto.KeyGenerator;
    import javax.crypto.NoSuchPaddingException;
    import javax.crypto.SecretKey;
    import javax.crypto.spec.SecretKeySpec;
    import java.io.UnsupportedEncodingException;
    import java.security.InvalidKeyException;
    import java.security.NoSuchAlgorithmException;
    import java.security.SecureRandom;
    import java.util.List;

    public class AESUtil {

    /**
    * AES加密
    *
    * @param content 要加密的内容
    * @return encrypt
    */
    public static String encryptWithAes(String content, String password) {
    try {
    SecureRandom random = SecureRandom.getInstance("SHA1PRNG");
    random.setSeed(password.getBytes());
    KeyGenerator kgen = KeyGenerator.getInstance("AES");
    kgen.init(128, random);
    SecretKey secretKey = kgen.generateKey();
    byte[] enCodeFormat = secretKey.getEncoded();
    SecretKeySpec key = new SecretKeySpec(enCodeFormat, "AES");
    Cipher cipher = Cipher.getInstance("AES");
    byte[] byteContent = content.getBytes("utf-8");
    cipher.init(Cipher.ENCRYPT_MODE, key);
    byte[] result = cipher.doFinal(byteContent);

    StringBuilder sb = new StringBuilder();
    for (byte aResult : result) {
    String hex = Integer.toHexString(aResult & 0xFF);
    if (hex.length() == 1) {
    hex = '0' + hex;
    }
    sb.append(hex.toUpperCase());
    }
    return sb.toString();
    } catch (NoSuchAlgorithmException | NoSuchPaddingException | InvalidKeyException
    | UnsupportedEncodingException | IllegalBlockSizeException | BadPaddingException e) {
    e.printStackTrace();
    }
    return null;
    }

    /**
    * AES解密
    *
    * @param hexStr 待解密内容
    * @return decrypt
    */
    public static String decryptWithAes(String hexStr, String password) {
    if (StringUtils.isBlank(hexStr)) {
    return hexStr;
    }

    byte[] content = new byte[hexStr.length() / 2];
    for (int i = 0; i < hexStr.length() / 2; i++) {
    int high = Integer.parseInt(hexStr.substring(i * 2, i * 2 + 1), 16);
    int low = Integer.parseInt(hexStr.substring(i * 2 + 1, i * 2 + 2), 16);
    content[i] = (byte) (high * 16 + low);
    }trySecureRandom random = SecureRandom.getInstance("SHA1PRNG");random.setSeed(password.getBytes());KeyGenerator kgen = KeyGenerator.getInstance("AES")kgen.init(128, random);

    SecretKey secretKey = kgen.generateKey();
    byte[] enCodeFormat = secretKey.getEncoded();
    SecretKeySpec key = new SecretKeySpec(enCodeFormat, "AES");
    Cipher cipher = Cipher.getInstance("AES");
    cipher.init(Cipher.DECRYPT_MODE, key);
    byte[] result = cipher.doFinal(content);
    return new String(result);
    } catch (NoSuchAlgorithmException | NoSuchPaddingException | InvalidKeyException
    | IllegalBlockSizeException | BadPaddingException e) {
    e.printStackTrace();
    }
    return null;
    }

    // public static void main(String[] args) {
    // String content = "{'projUnique':'5','serialNOS':['D4277efa99b99f','Lcb9cfc5e23f745baf']}";
    // JSONObject josn = JSONObject.fromObject(content);
    // List<String > ss = (List<String>) josn.get("serialNOS");
    // for(int w=0;w<ss.size();w++){
    // System.out.println(ss.get(w));
    // }
    // String password = "自定义";
    // System.out.println("加密前:" + content);
    // String encryptResultStr = AESUtil.encryptWithAes(content, password);
    // System.out.println("加密后:" + encryptResultStr);
    // //解密
    // String hexStr = "9F79FBABDD5137B78F1F83350A051C069986B40E23E77341759AD505C";
    // String decryptFrom = AESUtil.decryptWithAes(hexStr, password);
    // System.out.println("解密后:" + decryptFrom);
    // }
    }

    qq 891451702
  • 相关阅读:
    创建onlineworkspace问题仍没解决?
    Javascript 脚本错误.
    从HtmlInputFile控件中读取文件 保存到document libary中.
    Request.Form.Get
    DataGrid 模板列里 寻找所在列的隐藏列的值
    test
    重写ProgressDialog,实现各种个性进度条需求(含源码)
    Android自定义控件实现环形播放进度条
    为你的应用添加悬浮窗功能
    Android 左右滑动 控件
  • 原文地址:https://www.cnblogs.com/duoyan/p/12470697.html
Copyright © 2011-2022 走看看