parent
76b053e21f
commit
caf6396194
15 changed files with 368 additions and 672 deletions
@ -1,73 +1,73 @@ |
||||
package com.daqing.financial.hrauth.util; |
||||
|
||||
import java.security.AlgorithmParameters; |
||||
import java.security.InvalidAlgorithmParameterException; |
||||
import java.security.InvalidKeyException; |
||||
import java.security.Key; |
||||
import java.security.NoSuchAlgorithmException; |
||||
import java.security.NoSuchProviderException; |
||||
import java.security.Security; |
||||
|
||||
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.bouncycastle.jce.provider.BouncyCastleProvider; |
||||
|
||||
public class AES { |
||||
public static boolean initialized = false; |
||||
|
||||
/** |
||||
* AES对称解密工具类 |
||||
* |
||||
* @param content |
||||
* 密文 |
||||
* @return |
||||
* @throws InvalidAlgorithmParameterException |
||||
* @throws NoSuchProviderException |
||||
*/ |
||||
public byte[] decrypt(byte[] content, byte[] keyByte, byte[] ivByte) throws InvalidAlgorithmParameterException { |
||||
initialize(); |
||||
try { |
||||
// java是没有
|
||||
Cipher cipher = Cipher.getInstance("AES/CBC/PKCS7Padding"); |
||||
Key sKeySpec = new SecretKeySpec(keyByte, "AES"); |
||||
|
||||
cipher.init(Cipher.DECRYPT_MODE, sKeySpec, generateIV(ivByte));// 初始化
|
||||
byte[] result = cipher.doFinal(content); |
||||
return result; |
||||
} catch (NoSuchAlgorithmException e) { |
||||
e.printStackTrace(); |
||||
} catch (NoSuchPaddingException e) { |
||||
e.printStackTrace(); |
||||
} catch (InvalidKeyException e) { |
||||
e.printStackTrace(); |
||||
} catch (IllegalBlockSizeException e) { |
||||
e.printStackTrace(); |
||||
} catch (BadPaddingException e) { |
||||
e.printStackTrace(); |
||||
} catch (NoSuchProviderException e) { |
||||
e.printStackTrace(); |
||||
} catch (Exception e) { |
||||
e.printStackTrace(); |
||||
} |
||||
return null; |
||||
} |
||||
|
||||
public static void initialize() { |
||||
if (initialized) |
||||
return; |
||||
Security.addProvider(new BouncyCastleProvider()); |
||||
initialized = true; |
||||
} |
||||
|
||||
// 生成iv
|
||||
public static AlgorithmParameters generateIV(byte[] iv) throws Exception { |
||||
AlgorithmParameters params = AlgorithmParameters.getInstance("AES"); |
||||
params.init(new IvParameterSpec(iv)); |
||||
return params; |
||||
} |
||||
} |
||||
//package com.daqing.financial.hrauth.util;
|
||||
//
|
||||
//import java.security.AlgorithmParameters;
|
||||
//import java.security.InvalidAlgorithmParameterException;
|
||||
//import java.security.InvalidKeyException;
|
||||
//import java.security.Key;
|
||||
//import java.security.NoSuchAlgorithmException;
|
||||
//import java.security.NoSuchProviderException;
|
||||
//import java.security.Security;
|
||||
//
|
||||
//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.bouncycastle.jce.provider.BouncyCastleProvider;
|
||||
//
|
||||
//public class AES {
|
||||
// public static boolean initialized = false;
|
||||
//
|
||||
// /**
|
||||
// * AES对称解密工具类
|
||||
// *
|
||||
// * @param content
|
||||
// * 密文
|
||||
// * @return
|
||||
// * @throws InvalidAlgorithmParameterException
|
||||
// * @throws NoSuchProviderException
|
||||
// */
|
||||
// public byte[] decrypt(byte[] content, byte[] keyByte, byte[] ivByte) throws InvalidAlgorithmParameterException {
|
||||
// initialize();
|
||||
// try {
|
||||
// // java是没有
|
||||
// Cipher cipher = Cipher.getInstance("AES/CBC/PKCS7Padding");
|
||||
// Key sKeySpec = new SecretKeySpec(keyByte, "AES");
|
||||
//
|
||||
// cipher.init(Cipher.DECRYPT_MODE, sKeySpec, generateIV(ivByte));// 初始化
|
||||
// byte[] result = cipher.doFinal(content);
|
||||
// return result;
|
||||
// } catch (NoSuchAlgorithmException e) {
|
||||
// e.printStackTrace();
|
||||
// } catch (NoSuchPaddingException e) {
|
||||
// e.printStackTrace();
|
||||
// } catch (InvalidKeyException e) {
|
||||
// e.printStackTrace();
|
||||
// } catch (IllegalBlockSizeException e) {
|
||||
// e.printStackTrace();
|
||||
// } catch (BadPaddingException e) {
|
||||
// e.printStackTrace();
|
||||
// } catch (NoSuchProviderException e) {
|
||||
// e.printStackTrace();
|
||||
// } catch (Exception e) {
|
||||
// e.printStackTrace();
|
||||
// }
|
||||
// return null;
|
||||
// }
|
||||
//
|
||||
// public static void initialize() {
|
||||
// if (initialized)
|
||||
// return;
|
||||
// Security.addProvider(new BouncyCastleProvider());
|
||||
// initialized = true;
|
||||
// }
|
||||
//
|
||||
// // 生成iv
|
||||
// public static AlgorithmParameters generateIV(byte[] iv) throws Exception {
|
||||
// AlgorithmParameters params = AlgorithmParameters.getInstance("AES");
|
||||
// params.init(new IvParameterSpec(iv));
|
||||
// return params;
|
||||
// }
|
||||
//}
|
@ -1,171 +1,171 @@ |
||||
package com.daqing.financial.hrauth.util; |
||||
|
||||
import org.apache.commons.codec.binary.Base64; |
||||
import org.bouncycastle.jce.provider.BouncyCastleProvider; |
||||
|
||||
import javax.crypto.*; |
||||
import javax.crypto.spec.IvParameterSpec; |
||||
import javax.crypto.spec.SecretKeySpec; |
||||
import java.io.UnsupportedEncodingException; |
||||
import java.nio.charset.StandardCharsets; |
||||
import java.security.*; |
||||
import java.security.spec.InvalidParameterSpecException; |
||||
import java.util.Arrays; |
||||
|
||||
/** |
||||
* Created by yfs on 2018/3/25. |
||||
* <p> |
||||
* AES-128-CBC 加密方式 |
||||
* 注: |
||||
* AES-128-CBC可以自己定义“密钥”和“偏移量“。 |
||||
* AES-128是jdk自动生成的“密钥”。 |
||||
*/ |
||||
public class AesCbcUtil { |
||||
|
||||
|
||||
static { |
||||
//BouncyCastle是一个开源的加解密解决方案,主页在http://www.bouncycastle.org/
|
||||
Security.addProvider(new BouncyCastleProvider()); |
||||
} |
||||
|
||||
/** |
||||
* AES解密 |
||||
* |
||||
* @param data //密文,被加密的数据
|
||||
* @param key //秘钥
|
||||
* @param iv //偏移量
|
||||
* @param encodingFormat //解密后的结果需要进行的编码
|
||||
* @return |
||||
* @throws Exception |
||||
*/ |
||||
public static String decrypt(String data, String key, String iv, String encodingFormat) throws Exception { |
||||
// initialize();
|
||||
|
||||
//被加密的数据
|
||||
byte[] dataByte = Base64.decodeBase64(data); |
||||
//加密秘钥
|
||||
byte[] keyByte = Base64.decodeBase64(key); |
||||
//偏移量
|
||||
byte[] ivByte = Base64.decodeBase64(iv); |
||||
|
||||
|
||||
try { |
||||
Cipher cipher = Cipher.getInstance("AES/CBC/PKCS7Padding"); |
||||
|
||||
SecretKeySpec spec = new SecretKeySpec(keyByte, "AES"); |
||||
|
||||
AlgorithmParameters parameters = AlgorithmParameters.getInstance("AES"); |
||||
parameters.init(new IvParameterSpec(ivByte)); |
||||
|
||||
cipher.init(Cipher.DECRYPT_MODE, spec, parameters);// 初始化
|
||||
|
||||
byte[] resultByte = cipher.doFinal(dataByte); |
||||
if (null != resultByte && resultByte.length > 0) { |
||||
String result = new String(resultByte, encodingFormat); |
||||
return result; |
||||
} |
||||
return null; |
||||
} catch (NoSuchAlgorithmException e) { |
||||
e.printStackTrace(); |
||||
} catch (NoSuchPaddingException e) { |
||||
e.printStackTrace(); |
||||
} catch (InvalidParameterSpecException e) { |
||||
e.printStackTrace(); |
||||
} catch (InvalidKeyException e) { |
||||
e.printStackTrace(); |
||||
} catch (InvalidAlgorithmParameterException e) { |
||||
e.printStackTrace(); |
||||
} catch (IllegalBlockSizeException e) { |
||||
e.printStackTrace(); |
||||
} catch (BadPaddingException e) { |
||||
e.printStackTrace(); |
||||
} catch (UnsupportedEncodingException e) { |
||||
e.printStackTrace(); |
||||
} |
||||
|
||||
return null; |
||||
} |
||||
|
||||
/** |
||||
* AES解密 |
||||
* |
||||
* @param encryptedData 消息密文 |
||||
* @param ivStr iv字符串 |
||||
*/ |
||||
public static byte[] decrypt2(String sessionKey, String encryptedData, String ivStr) { |
||||
try { |
||||
AlgorithmParameters params = AlgorithmParameters.getInstance("AES"); |
||||
params.init(new IvParameterSpec(Base64.decodeBase64(ivStr))); |
||||
|
||||
Cipher cipher = Cipher.getInstance("AES/CBC/NoPadding"); |
||||
cipher.init(Cipher.DECRYPT_MODE, new SecretKeySpec(Base64.decodeBase64(sessionKey), "AES"), params); |
||||
|
||||
String encodingFormat = "UTF-8"; |
||||
byte[] resultByte = cipher.doFinal(Base64.decodeBase64(encryptedData)); |
||||
return resultByte; |
||||
/* System.out.println("resultByte===="+resultByte); |
||||
if (null != resultByte && resultByte.length > 0) { |
||||
String result = new String(resultByte, encodingFormat); |
||||
return result; |
||||
} |
||||
return null;*/ |
||||
//return new String(PKCS7Encoder.decode(cipher.doFinal(Base64.decodeBase64(encryptedData))), StandardCharsets.UTF_8);
|
||||
} catch (Exception e) { |
||||
throw new RuntimeException("AES解密失败", e); |
||||
} |
||||
} |
||||
|
||||
|
||||
public static String decryptNew(String encryptedData, String sessionKey, String iv) throws Exception { |
||||
String result = ""; |
||||
// 被加密的数据
|
||||
byte[] dataByte = Base64.decodeBase64(encryptedData); |
||||
// 加密秘钥
|
||||
byte[] keyByte = Base64.decodeBase64(sessionKey); |
||||
// 偏移量
|
||||
byte[] ivByte = Base64.decodeBase64(iv); |
||||
try { |
||||
// 如果密钥不足16位,那么就补足. 这个if 中的内容很重要
|
||||
int base = 16; |
||||
if (keyByte.length % base != 0) { |
||||
int groups = keyByte.length / base + (keyByte.length % base != 0 ? 1 : 0); |
||||
byte[] temp = new byte[groups * base]; |
||||
Arrays.fill(temp, (byte) 0); |
||||
System.arraycopy(keyByte, 0, temp, 0, keyByte.length); |
||||
keyByte = temp; |
||||
} |
||||
// 初始化
|
||||
Security.addProvider(new BouncyCastleProvider()); |
||||
Cipher cipher = Cipher.getInstance("AES/CBC/PKCS7Padding", "BC"); |
||||
SecretKeySpec spec = new SecretKeySpec(keyByte, "AES"); |
||||
AlgorithmParameters parameters = AlgorithmParameters.getInstance("AES"); |
||||
parameters.init(new IvParameterSpec(ivByte)); |
||||
// 初始化
|
||||
cipher.init(Cipher.DECRYPT_MODE, spec, parameters); |
||||
byte[] resultByte = cipher.doFinal(dataByte); |
||||
if (null != resultByte && resultByte.length > 0) { |
||||
result = new String(resultByte, "UTF-8"); |
||||
} |
||||
} catch (NoSuchAlgorithmException e) { |
||||
e.printStackTrace(); |
||||
} catch (NoSuchPaddingException e) { |
||||
e.printStackTrace(); |
||||
} catch (InvalidParameterSpecException e) { |
||||
e.printStackTrace(); |
||||
} catch (IllegalBlockSizeException e) { |
||||
e.printStackTrace(); |
||||
} catch (BadPaddingException e) { |
||||
e.printStackTrace(); |
||||
} catch (UnsupportedEncodingException e) { |
||||
e.printStackTrace(); |
||||
} catch (InvalidKeyException e) { |
||||
e.printStackTrace(); |
||||
} catch (InvalidAlgorithmParameterException e) { |
||||
e.printStackTrace(); |
||||
} catch (NoSuchProviderException e) { |
||||
e.printStackTrace(); |
||||
} |
||||
return result; |
||||
} |
||||
|
||||
} |
||||
//package com.daqing.financial.hrauth.util;
|
||||
//
|
||||
//import org.apache.commons.codec.binary.Base64;
|
||||
//import org.bouncycastle.jce.provider.BouncyCastleProvider;
|
||||
//
|
||||
//import javax.crypto.*;
|
||||
//import javax.crypto.spec.IvParameterSpec;
|
||||
//import javax.crypto.spec.SecretKeySpec;
|
||||
//import java.io.UnsupportedEncodingException;
|
||||
//import java.nio.charset.StandardCharsets;
|
||||
//import java.security.*;
|
||||
//import java.security.spec.InvalidParameterSpecException;
|
||||
//import java.util.Arrays;
|
||||
//
|
||||
///**
|
||||
// * Created by yfs on 2018/3/25.
|
||||
// * <p>
|
||||
// * AES-128-CBC 加密方式
|
||||
// * 注:
|
||||
// * AES-128-CBC可以自己定义“密钥”和“偏移量“。
|
||||
// * AES-128是jdk自动生成的“密钥”。
|
||||
// */
|
||||
//public class AesCbcUtil {
|
||||
//
|
||||
//
|
||||
// static {
|
||||
// //BouncyCastle是一个开源的加解密解决方案,主页在http://www.bouncycastle.org/
|
||||
// Security.addProvider(new BouncyCastleProvider());
|
||||
// }
|
||||
//
|
||||
// /**
|
||||
// * AES解密
|
||||
// *
|
||||
// * @param data //密文,被加密的数据
|
||||
// * @param key //秘钥
|
||||
// * @param iv //偏移量
|
||||
// * @param encodingFormat //解密后的结果需要进行的编码
|
||||
// * @return
|
||||
// * @throws Exception
|
||||
// */
|
||||
// public static String decrypt(String data, String key, String iv, String encodingFormat) throws Exception {
|
||||
//// initialize();
|
||||
//
|
||||
// //被加密的数据
|
||||
// byte[] dataByte = Base64.decodeBase64(data);
|
||||
// //加密秘钥
|
||||
// byte[] keyByte = Base64.decodeBase64(key);
|
||||
// //偏移量
|
||||
// byte[] ivByte = Base64.decodeBase64(iv);
|
||||
//
|
||||
//
|
||||
// try {
|
||||
// Cipher cipher = Cipher.getInstance("AES/CBC/PKCS7Padding");
|
||||
//
|
||||
// SecretKeySpec spec = new SecretKeySpec(keyByte, "AES");
|
||||
//
|
||||
// AlgorithmParameters parameters = AlgorithmParameters.getInstance("AES");
|
||||
// parameters.init(new IvParameterSpec(ivByte));
|
||||
//
|
||||
// cipher.init(Cipher.DECRYPT_MODE, spec, parameters);// 初始化
|
||||
//
|
||||
// byte[] resultByte = cipher.doFinal(dataByte);
|
||||
// if (null != resultByte && resultByte.length > 0) {
|
||||
// String result = new String(resultByte, encodingFormat);
|
||||
// return result;
|
||||
// }
|
||||
// return null;
|
||||
// } catch (NoSuchAlgorithmException e) {
|
||||
// e.printStackTrace();
|
||||
// } catch (NoSuchPaddingException e) {
|
||||
// e.printStackTrace();
|
||||
// } catch (InvalidParameterSpecException e) {
|
||||
// e.printStackTrace();
|
||||
// } catch (InvalidKeyException e) {
|
||||
// e.printStackTrace();
|
||||
// } catch (InvalidAlgorithmParameterException e) {
|
||||
// e.printStackTrace();
|
||||
// } catch (IllegalBlockSizeException e) {
|
||||
// e.printStackTrace();
|
||||
// } catch (BadPaddingException e) {
|
||||
// e.printStackTrace();
|
||||
// } catch (UnsupportedEncodingException e) {
|
||||
// e.printStackTrace();
|
||||
// }
|
||||
//
|
||||
// return null;
|
||||
// }
|
||||
//
|
||||
// /**
|
||||
// * AES解密
|
||||
// *
|
||||
// * @param encryptedData 消息密文
|
||||
// * @param ivStr iv字符串
|
||||
// */
|
||||
// public static byte[] decrypt2(String sessionKey, String encryptedData, String ivStr) {
|
||||
// try {
|
||||
// AlgorithmParameters params = AlgorithmParameters.getInstance("AES");
|
||||
// params.init(new IvParameterSpec(Base64.decodeBase64(ivStr)));
|
||||
//
|
||||
// Cipher cipher = Cipher.getInstance("AES/CBC/NoPadding");
|
||||
// cipher.init(Cipher.DECRYPT_MODE, new SecretKeySpec(Base64.decodeBase64(sessionKey), "AES"), params);
|
||||
//
|
||||
// String encodingFormat = "UTF-8";
|
||||
// byte[] resultByte = cipher.doFinal(Base64.decodeBase64(encryptedData));
|
||||
// return resultByte;
|
||||
///* System.out.println("resultByte===="+resultByte);
|
||||
// if (null != resultByte && resultByte.length > 0) {
|
||||
// String result = new String(resultByte, encodingFormat);
|
||||
// return result;
|
||||
// }
|
||||
// return null;*/
|
||||
// //return new String(PKCS7Encoder.decode(cipher.doFinal(Base64.decodeBase64(encryptedData))), StandardCharsets.UTF_8);
|
||||
// } catch (Exception e) {
|
||||
// throw new RuntimeException("AES解密失败", e);
|
||||
// }
|
||||
// }
|
||||
//
|
||||
//
|
||||
// public static String decryptNew(String encryptedData, String sessionKey, String iv) throws Exception {
|
||||
// String result = "";
|
||||
// // 被加密的数据
|
||||
// byte[] dataByte = Base64.decodeBase64(encryptedData);
|
||||
// // 加密秘钥
|
||||
// byte[] keyByte = Base64.decodeBase64(sessionKey);
|
||||
// // 偏移量
|
||||
// byte[] ivByte = Base64.decodeBase64(iv);
|
||||
// try {
|
||||
// // 如果密钥不足16位,那么就补足. 这个if 中的内容很重要
|
||||
// int base = 16;
|
||||
// if (keyByte.length % base != 0) {
|
||||
// int groups = keyByte.length / base + (keyByte.length % base != 0 ? 1 : 0);
|
||||
// byte[] temp = new byte[groups * base];
|
||||
// Arrays.fill(temp, (byte) 0);
|
||||
// System.arraycopy(keyByte, 0, temp, 0, keyByte.length);
|
||||
// keyByte = temp;
|
||||
// }
|
||||
// // 初始化
|
||||
// Security.addProvider(new BouncyCastleProvider());
|
||||
// Cipher cipher = Cipher.getInstance("AES/CBC/PKCS7Padding", "BC");
|
||||
// SecretKeySpec spec = new SecretKeySpec(keyByte, "AES");
|
||||
// AlgorithmParameters parameters = AlgorithmParameters.getInstance("AES");
|
||||
// parameters.init(new IvParameterSpec(ivByte));
|
||||
// // 初始化
|
||||
// cipher.init(Cipher.DECRYPT_MODE, spec, parameters);
|
||||
// byte[] resultByte = cipher.doFinal(dataByte);
|
||||
// if (null != resultByte && resultByte.length > 0) {
|
||||
// result = new String(resultByte, "UTF-8");
|
||||
// }
|
||||
// } catch (NoSuchAlgorithmException e) {
|
||||
// e.printStackTrace();
|
||||
// } catch (NoSuchPaddingException e) {
|
||||
// e.printStackTrace();
|
||||
// } catch (InvalidParameterSpecException e) {
|
||||
// e.printStackTrace();
|
||||
// } catch (IllegalBlockSizeException e) {
|
||||
// e.printStackTrace();
|
||||
// } catch (BadPaddingException e) {
|
||||
// e.printStackTrace();
|
||||
// } catch (UnsupportedEncodingException e) {
|
||||
// e.printStackTrace();
|
||||
// } catch (InvalidKeyException e) {
|
||||
// e.printStackTrace();
|
||||
// } catch (InvalidAlgorithmParameterException e) {
|
||||
// e.printStackTrace();
|
||||
// } catch (NoSuchProviderException e) {
|
||||
// e.printStackTrace();
|
||||
// }
|
||||
// return result;
|
||||
// }
|
||||
//
|
||||
//}
|
@ -1,198 +0,0 @@ |
||||
package com.daqing.financial.hrauth.util; |
||||
|
||||
import com.google.common.primitives.Bytes; |
||||
import org.apache.commons.codec.binary.Base64; |
||||
|
||||
import java.io.UnsupportedEncodingException; |
||||
import java.security.InvalidAlgorithmParameterException; |
||||
import java.security.InvalidKeyException; |
||||
import java.security.NoSuchAlgorithmException; |
||||
import java.security.SecureRandom; |
||||
|
||||
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; |
||||
|
||||
public class TestUtil { |
||||
/** |
||||
* 加密 |
||||
* |
||||
* @param content |
||||
* 需要加密的内容 |
||||
* @param password |
||||
* 加密密码 |
||||
* @return |
||||
*/ |
||||
public static byte[] encrypt(String content, String password) { |
||||
try { |
||||
KeyGenerator kgen = KeyGenerator.getInstance("AES"); |
||||
kgen.init(128, new SecureRandom(password.getBytes())); |
||||
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); |
||||
return result; // 加密
|
||||
} catch (NoSuchAlgorithmException e) { |
||||
e.printStackTrace(); |
||||
} catch (NoSuchPaddingException e) { |
||||
e.printStackTrace(); |
||||
} catch (InvalidKeyException e) { |
||||
e.printStackTrace(); |
||||
} catch (UnsupportedEncodingException e) { |
||||
e.printStackTrace(); |
||||
} catch (IllegalBlockSizeException e) { |
||||
e.printStackTrace(); |
||||
} catch (BadPaddingException e) { |
||||
e.printStackTrace(); |
||||
} |
||||
return null; |
||||
} |
||||
|
||||
/** |
||||
* 解密 |
||||
* |
||||
* @param content |
||||
* 待解密内容 |
||||
* @param password |
||||
* 解密密钥 |
||||
* @return |
||||
*/ |
||||
public static byte[] decrypt(byte[] content, String password) { |
||||
try { |
||||
KeyGenerator kgen = KeyGenerator.getInstance("AES"); |
||||
kgen.init(128, new SecureRandom(password.getBytes())); |
||||
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 result; // 加密
|
||||
} catch (NoSuchAlgorithmException e) { |
||||
e.printStackTrace(); |
||||
} catch (NoSuchPaddingException e) { |
||||
e.printStackTrace(); |
||||
} catch (InvalidKeyException e) { |
||||
e.printStackTrace(); |
||||
} catch (IllegalBlockSizeException e) { |
||||
e.printStackTrace(); |
||||
} catch (BadPaddingException e) { |
||||
e.printStackTrace(); |
||||
} |
||||
return null; |
||||
} |
||||
|
||||
/** |
||||
* 将二进制转换成16进制 |
||||
* |
||||
* @param buf |
||||
* @return |
||||
*/ |
||||
public static String parseByte2HexStr(byte buf[]) { |
||||
StringBuffer sb = new StringBuffer(); |
||||
for (int i = 0; i < buf.length; i++) { |
||||
String hex = Integer.toHexString(buf[i] & 0xFF); |
||||
if (hex.length() == 1) { |
||||
hex = '0' + hex; |
||||
} |
||||
sb.append(hex.toUpperCase()); |
||||
} |
||||
return sb.toString(); |
||||
} |
||||
|
||||
/** |
||||
* 将16进制转换为二进制 |
||||
* |
||||
* @param hexStr |
||||
* @return |
||||
*/ |
||||
public static byte[] parseHexStr2Byte(String hexStr) { |
||||
if (hexStr.length() < 1) |
||||
return null; |
||||
byte[] result = 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); |
||||
result[i] = (byte) (high * 16 + low); |
||||
} |
||||
return result; |
||||
} |
||||
|
||||
/** |
||||
* 加密 |
||||
* |
||||
* @param content |
||||
* 需要加密的内容 |
||||
* @param password |
||||
* 加密密码 |
||||
* @return |
||||
*/ |
||||
public static byte[] encrypt2(String content, String password) { |
||||
try { |
||||
SecretKeySpec key = new SecretKeySpec(password.getBytes(), "AES"); |
||||
Cipher cipher = Cipher.getInstance("AES/ECB/NoPadding"); |
||||
byte[] byteContent = content.getBytes("utf-8"); |
||||
cipher.init(Cipher.ENCRYPT_MODE, key);// 初始化
|
||||
byte[] result = cipher.doFinal(byteContent); |
||||
return result; // 加密
|
||||
} catch (NoSuchAlgorithmException e) { |
||||
e.printStackTrace(); |
||||
} catch (NoSuchPaddingException e) { |
||||
e.printStackTrace(); |
||||
} catch (InvalidKeyException e) { |
||||
e.printStackTrace(); |
||||
} catch (UnsupportedEncodingException e) { |
||||
e.printStackTrace(); |
||||
} catch (IllegalBlockSizeException e) { |
||||
e.printStackTrace(); |
||||
} catch (BadPaddingException e) { |
||||
e.printStackTrace(); |
||||
} |
||||
return null; |
||||
} |
||||
|
||||
public static void main(String[] args) throws UnsupportedEncodingException, InvalidAlgorithmParameterException { |
||||
String encryptedData="+WQuaG5Tr9EU6f+Cx7OR7O8/HbVAUK77P9/BRqhdA4qYKLcw0sbS1R5DfoU+D4HEpoUCXtmRH/gP" + |
||||
"eJqYFJpauBysbadiYrZ6wYWjp5wP+HgxWnfCOk7jwR8WUdvFFDuQMhAVudnFhhNP3g4iHfCeiU5yfmN4iw" + |
||||
"gpL4txJpuZDHhyjqBqGGxntyxgxHFonbFi94e+IaPrFqigvqJXI9RGW9eXUVhZEQGeadv+eW72ucPq90KGH" + |
||||
"wPj0uUMWRWPJC8ZWp+uRUbrENiKfo9QJqiMljq/wcbz4c/u/Wd7PF4o4s02uutMjsrpYdnpw20E7DDpUIBU" + |
||||
"fIkeDmIFOIEQYPAGbTFtbHwPyyrx/r+fSihmO6PRwAlUrfhCOZ3KNkmtqZPDjqfHGrW+MeiquW5vryXL8FWy++z" + |
||||
"OVdGzymGvW6E/hSVDoTNL4+zeuxhJAXCnuPjLtYeL0EkGam8ks0KDqhZy3gUwRwrM0HFzQ1IryTMsqeLCcLMm" + |
||||
"0vWG4PEuPv5j5+V66MTGYvyyM2v67zK7Z2cn2A=="; |
||||
String iv="zI+P8RVxEwMFBXcJDCemHg=="; |
||||
String session_key="Piqc8iFe6pN0i5ofq4Y6PA=="; |
||||
|
||||
String content = encryptedData; |
||||
String password = ""; |
||||
// 加密
|
||||
System.out.println("加密前:" + content); |
||||
byte[] encode = encrypt(content, password); |
||||
|
||||
//传输过程,不转成16进制的字符串,就等着程序崩溃掉吧
|
||||
String code = parseByte2HexStr(encode); |
||||
System.out.println("密文字符串:" + code); |
||||
byte[] decode = parseHexStr2Byte(code); |
||||
// 解密
|
||||
byte[] decryptResult = decrypt(decode, password); |
||||
System.out.println("decryptResult==="+decryptResult); |
||||
System.out.println("解密后:" + new String(decryptResult, "UTF-8")); //不转码会乱码
|
||||
System.out.println("========================================================="); |
||||
|
||||
byte[] dataByte = Base64.decodeBase64(session_key); |
||||
//byte[] d = TestUtil.parseHexStr2Byte(encryptedData);
|
||||
byte[] dr = decrypt(dataByte, ""); |
||||
//String re = new String(dr, "UTF-8");
|
||||
//System.out.println("result====="+re);
|
||||
|
||||
|
||||
|
||||
|
||||
} |
||||
} |
Loading…
Reference in new issue