zoukankan      html  css  js  c++  java
  • AESUtil_1

    package com.tebon.ams.util;

    import org.apache.commons.codec.binary.Base64;

    import javax.crypto.Cipher;
    import javax.crypto.KeyGenerator;
    import javax.crypto.SecretKey;
    import javax.crypto.spec.SecretKeySpec;
    import java.io.BufferedWriter;
    import java.io.FileWriter;
    import java.io.IOException;
    import java.security.MessageDigest;
    import java.security.SecureRandom;

    public class AESUtil {
    /**
    * 功能:生成AES秘钥并写入指定文件中
    *
    * @param filePath
    * 文件夹绝对路径,必须是已存在文件
    * @throws Exception
    */
    public static void genAESKeyPair(String filePath) throws Exception {
    // keySpec 生成对称密钥
    KeyGenerator keyGenerator = KeyGenerator.getInstance("AES");
    keyGenerator.init(128);
    SecretKey secretKey = keyGenerator.generateKey();
    SecretKeySpec keySpec = new SecretKeySpec(secretKey.getEncoded(), "AES");

    try {
    String publicKeyString = Base64.encodeBase64String(keySpec.getEncoded());
    System.out.println(publicKeyString);

    FileWriter pubfw = new FileWriter(filePath + "AESKey.key");
    BufferedWriter pubbw = new BufferedWriter(pubfw);
    pubbw.write(publicKeyString);

    pubbw.flush();
    pubbw.close();
    pubfw.close();
    } catch (IOException e) {
    e.printStackTrace();
    }
    }

    /**
    * 获取签名
    *
    * @param data
    * 要加签的数据
    * @param MD5key
    * 加签时使用的key
    * @return 签名字符
    * @throws Exception
    */
    public static String getSignature(String data, String MD5key) throws Exception {
    data = data + "|" + MD5key; // data与key
    MessageDigest md = MessageDigest.getInstance("MD5");
    byte[] b = md.digest(data.getBytes("UTF-8")); // md5签名
    return Base64.encodeBase64String(b); // base64转码
    };

    /**
    * AES加密
    *
    * @param plainTextData
    * 要加密的数据
    * @param key
    * 加密时使用的key
    * @return 返回加密后的数据
    * @throws Exception
    */
    public static String encryptAES(String plainTextData, String key) throws Exception {

    // 密钥
    KeyGenerator kgen = KeyGenerator.getInstance("AES");
    SecureRandom secureRandom = SecureRandom.getInstance("SHA1PRNG");
    secureRandom.setSeed(key.getBytes());
    kgen.init(128, secureRandom);

    SecretKey skey = kgen.generateKey();
    byte[] raw = skey.getEncoded();
    SecretKeySpec skeySpec = new SecretKeySpec(raw, "AES");

    // 加密数据
    Cipher cipher = Cipher.getInstance("AES");
    cipher.init(Cipher.ENCRYPT_MODE, skeySpec);
    byte[] encryptedData = cipher.doFinal(plainTextData.getBytes("UTF-8"));

    return Base64.encodeBase64String(encryptedData); // base64转码
    }

    /**
    * AES解密
    *
    * @param encryptedData
    * 要解密的数据
    * @param key
    * 解密使用的key
    * @return 解密后的数据
    * @throws Exception
    */
    public static String decryptAES(String encryptedData, String key) throws Exception {
    // 密钥
    KeyGenerator kgen = KeyGenerator.getInstance("AES");
    SecureRandom secureRandom = SecureRandom.getInstance("SHA1PRNG");
    secureRandom.setSeed(key.getBytes());
    kgen.init(128, secureRandom);
    SecretKey skey = kgen.generateKey();
    byte[] raw = skey.getEncoded();
    SecretKeySpec skeySpec = new SecretKeySpec(raw, "AES");

    // 解密数据
    Cipher cipher = Cipher.getInstance("AES");
    cipher.init(Cipher.DECRYPT_MODE, skeySpec);
    byte[] decryptedData = cipher.doFinal(Base64.decodeBase64(encryptedData));

    return new String(decryptedData, "UTF-8");
    }

    /**
    * 验签
    *
    * @param encryptedSignature MD5加签的数�?
    * @param plainTextData DATA明文
    * @param key MD5 key
    * @return
    * @throws Exception
    */
    public static boolean verify(String encryptedSignature, String plainTextData, String key) throws Exception {
    String signature = getSignature(plainTextData, key);
    if (encryptedSignature.equals(signature)) {
    return true;
    }
    return false;
    }

    public static void main(String[] args) throws Exception {
    // genAESKeyPair("c:/"); // 生成密钥
    String sendStr =
    "20171121|25|1|22195560| "
    +"850|CON2015020449|刘崟波|N|13820666177|天津市|120105198001196035||1|37|M|首付款产品|0.16|360|0|FX|1|1|1|0|0|31480000|12592000|0|18888000|0|22195560|4624075|4444721|1|24|20150330|15|2015-04-15|2017-03-29|否||0|0|0|2015-03-26|沃尔沃|S|2010 款T5|沃尔�? C70(进口)|红色|YV1MC6755BJ112639|H|24| "
    +"850|CON2015020449|2015-04-15|2015-04-15|924815|924815| "
    +"850|CON2015020449|2015-05-15|2015-05-15|924815|924815| "
    +"850|CON2015020449|2015-06-15|2015-06-16|924815|924815| "
    +"850|CON2015020449|2015-07-15|2015-07-15|924815|924815| "
    +"850|CON2015020449|2015-08-15|2015-08-15|924815|924815| "
    +"850|CON2015020449|2015-09-15|2015-09-15|924815|924815| "
    +"850|CON2015020449|2015-10-15|2015-10-16|924815|924815| "
    +"850|CON2015020449|2015-11-15|2015-11-15|924815|924815| "
    +"850|CON2015020449|2015-12-15|2015-12-15|924815|924815| "
    +"850|CON2015020449|2016-01-15|2016-01-16|924815|924815| "
    +"850|CON2015020449|2016-02-15|2016-02-15|924815|924815| "
    +"850|CON2015020449|2016-03-15|2016-03-16|924815|924815| "
    +"850|CON2015020449|2016-04-15|2016-04-15|924815|924815| "
    +"850|CON2015020449|2016-05-15|2016-05-16|924815|924815| "
    +"850|CON2015020449|2016-06-15|2016-06-15|924815|924815| "
    +"850|CON2015020449|2016-07-15|2016-07-15|924815|924815| "
    +"850|CON2015020449|2016-08-15|2016-08-16|924815|924815| "
    +"850|CON2015020449|2016-09-15|2016-09-15|924815|924815| "
    +"850|CON2015020449|2016-10-15|2016-10-16|924815|924815| "
    +"850|CON2015020449|2016-11-15||924815|0| "
    +"850|CON2015020449|2016-12-15||924815|0| "
    +"850|CON2015020449|2017-01-15||924815|0| "
    +"850|CON2015020449|2017-02-15||924815|0| "
    +"850|CON2015020449|2017-03-30||924815|0| ";


    //加密过程
    //MD5加签
    String MD5Key = "vVwU4bhXdlSjWHMjxpCcbg==";
    String md5Sign = AESUtil.getSignature(sendStr, MD5Key);

    String AESKey = "qkMlGXKh0106YuZNW7guzA==";
    String aesSendStr = md5Sign +" "+ sendStr;
    System.out.println(md5Sign);

    //对MD5加签后的结果进行AES加密
    String encryptedData = AESUtil.encryptAES(aesSendStr, AESKey);

    //验签过程
    //AES解签
    String decryptAESStr = AESUtil.decryptAES(encryptedData, AESKey);

    //解签后获取Md5加签
    String getMd5Sign = decryptAESStr.substring(0,aesSendStr.indexOf(" "));
    System.out.println(getMd5Sign);
    //解签后获取明
    String str = decryptAESStr.substring(aesSendStr.indexOf(" ")+2);
    System.out.println(str);

    //验签
    boolean flag = AESUtil.verify(getMd5Sign, str, MD5Key);
    System.out.println(flag);

    }
    }
  • 相关阅读:
    【spring-boot】mybatis-generator 使用入门
    【spring-boot】mybatis 使用入门
    【spring-boot】logback+slf4j 日志组合
    【spring-boot】写一个简单的单元测试
    github执行clone操作时报错
    enable device: BAR 0 [mem 0x00000000-0x003fffff] not claimed
    Meson version is 0.44.1 but project requires >=0.45.
    tig
    Unknown command 'run'
    systemctl enable rc-local.service error
  • 原文地址:https://www.cnblogs.com/muliu/p/10874348.html
Copyright © 2011-2022 走看看