Commit 9c03b231 by chenweitao

增加竞品数据补充加解密

parent 51a4bfa2
package com.zhiwei.searchhotcrawler.util; package com.zhiwei.searchhotcrawler.util;
import org.apache.commons.codec.binary.Base64;
import org.apache.commons.lang3.StringUtils;
import sun.misc.BASE64Decoder;
import java.io.UnsupportedEncodingException; import java.io.UnsupportedEncodingException;
import java.math.BigInteger;
import java.security.SecureRandom;
import java.util.Arrays; import java.util.Arrays;
import java.util.Objects;
import javax.crypto.Cipher; import javax.crypto.Cipher;
import javax.crypto.KeyGenerator;
import javax.crypto.spec.IvParameterSpec; import javax.crypto.spec.IvParameterSpec;
import javax.crypto.spec.SecretKeySpec; import javax.crypto.spec.SecretKeySpec;
import static java.util.Objects.isNull;
/** /**
* @author * @author
* @version V1.0 * @version V1.0
...@@ -14,6 +24,9 @@ import javax.crypto.spec.SecretKeySpec; ...@@ -14,6 +24,9 @@ import javax.crypto.spec.SecretKeySpec;
* @date 2017-12-28 14:26 * @date 2017-12-28 14:26
**/ **/
public class AESUtils { public class AESUtils {
//默认偏移
public static final String VI_STR = "0102030405060708";
private static final String ALGORITHMSTR = "AES/ECB/PKCS5Padding";
private AESUtils() { private AESUtils() {
} }
...@@ -25,11 +38,19 @@ public class AESUtils { ...@@ -25,11 +38,19 @@ public class AESUtils {
* @return 加密后的字符串 * @return 加密后的字符串
*/ */
public static String encrypt(String secret, String value) { public static String encrypt(String secret, String value) {
return encrypt(secret, value,VI_STR);
}
public static String encrypt(String secret, String value,String ivStr) {
SecretKeySpec keySpec = getKey(secret); SecretKeySpec keySpec = getKey(secret);
IvParameterSpec iv = new IvParameterSpec("0102030405060708".getBytes());
try { try {
Cipher cipher = Cipher.getInstance("AES/CBC/PKCS5Padding"); Cipher cipher = Cipher.getInstance(ALGORITHMSTR);
if (isNull(ivStr)){
cipher.init(Cipher.ENCRYPT_MODE, keySpec);
}else {
IvParameterSpec iv = new IvParameterSpec(ivStr.getBytes());
cipher.init(Cipher.ENCRYPT_MODE, keySpec, iv); cipher.init(Cipher.ENCRYPT_MODE, keySpec, iv);
}
byte[] encrypted = cipher.doFinal(value.getBytes("UTF-8")); byte[] encrypted = cipher.doFinal(value.getBytes("UTF-8"));
return parseByte2HexStr(encrypted); return parseByte2HexStr(encrypted);
} catch (Exception e) { } catch (Exception e) {
...@@ -37,6 +58,24 @@ public class AESUtils { ...@@ -37,6 +58,24 @@ public class AESUtils {
} }
} }
public static byte[] encrypt(byte[] secretBytes, byte[] valueBytes,String ivStr) {
SecretKeySpec keySpec = new SecretKeySpec(Arrays.copyOf(secretBytes, 16), "AES");
try {
Cipher cipher = Cipher.getInstance(ALGORITHMSTR);
if (isNull(ivStr)){
cipher.init(Cipher.ENCRYPT_MODE, keySpec);
}else {
IvParameterSpec iv = new IvParameterSpec(ivStr.getBytes());
cipher.init(Cipher.ENCRYPT_MODE, keySpec, iv);
}
byte[] encrypted = cipher.doFinal(valueBytes);
return encrypted;
// return parseByte2HexStr(encrypted);
} catch (Exception e) {
throw new RuntimeException(e);
}
}
public static void main(String[] args) { public static void main(String[] args) {
String jsm=AESUtils.encrypt("wechat", "shenjinzhu"); String jsm=AESUtils.encrypt("wechat", "shenjinzhu");
System.out.println(jsm); System.out.println(jsm);
...@@ -52,11 +91,27 @@ public class AESUtils { ...@@ -52,11 +91,27 @@ public class AESUtils {
* @return 解密后的字符串 * @return 解密后的字符串
*/ */
public static String decrypt(String secret, String value) { public static String decrypt(String secret, String value) {
return decrypt(secret,value,VI_STR);
}
/**
* 解密
*
* @param secret 密钥
* @param value 待解密字符串
* @param ivStr 偏移字符
* @return 解密后的字符串
*/
public static String decrypt(String secret, String value,String ivStr) {
SecretKeySpec keySpec = getKey(secret); SecretKeySpec keySpec = getKey(secret);
IvParameterSpec iv = new IvParameterSpec("0102030405060708".getBytes());
try { try {
Cipher cipher = Cipher.getInstance("AES/CBC/PKCS5Padding"); Cipher cipher = Cipher.getInstance(ALGORITHMSTR);
if (isNull(ivStr)){
cipher.init(Cipher.DECRYPT_MODE, keySpec);
}else {
IvParameterSpec iv = new IvParameterSpec(ivStr.getBytes());
cipher.init(Cipher.DECRYPT_MODE, keySpec, iv); cipher.init(Cipher.DECRYPT_MODE, keySpec, iv);
}
byte[] encrypted1 = parseHexStr2Byte(value); byte[] encrypted1 = parseHexStr2Byte(value);
byte[] original = cipher.doFinal(encrypted1); byte[] original = cipher.doFinal(encrypted1);
return new String(original, "UTF-8"); return new String(original, "UTF-8");
...@@ -66,6 +121,32 @@ public class AESUtils { ...@@ -66,6 +121,32 @@ public class AESUtils {
} }
/** /**
* 解密
*
* @param secretBytes 密钥组
* @param valueBytes 待解密字符组
* @param ivStr 偏移字符
* @return 解密后的字符串
*/
public static String decrypt(byte[] secretBytes, byte[] valueBytes,String ivStr) {
SecretKeySpec keySpec = new SecretKeySpec(Arrays.copyOf(secretBytes, 16), "AES");
try {
Cipher cipher = Cipher.getInstance(ALGORITHMSTR);
if (isNull(ivStr)){
cipher.init(Cipher.DECRYPT_MODE, keySpec);
}else {
IvParameterSpec iv = new IvParameterSpec(ivStr.getBytes());
cipher.init(Cipher.DECRYPT_MODE, keySpec, iv);
}
// byte[] encrypted1 = parseHexStr2Byte(value);
byte[] original = cipher.doFinal(valueBytes);
return new String(original, "UTF-8");
} catch (Exception e) {
throw new RuntimeException(e);
}
}
/**
* 生成加密的密钥,保证长度为16位 * 生成加密的密钥,保证长度为16位
* *
* @param secret 用户的密钥 * @param secret 用户的密钥
...@@ -75,6 +156,7 @@ public class AESUtils { ...@@ -75,6 +156,7 @@ public class AESUtils {
byte[] bytes; byte[] bytes;
try { try {
bytes = secret.getBytes("UTF-8"); bytes = secret.getBytes("UTF-8");
// return new SecretKeySpec(Arrays.copyOf(bytes, 32), "AES");
return new SecretKeySpec(Arrays.copyOf(bytes, 16), "AES"); return new SecretKeySpec(Arrays.copyOf(bytes, 16), "AES");
} catch (UnsupportedEncodingException e) { } catch (UnsupportedEncodingException e) {
e.printStackTrace(); e.printStackTrace();
...@@ -107,8 +189,9 @@ public class AESUtils { ...@@ -107,8 +189,9 @@ public class AESUtils {
* @return * @return
*/ */
public static byte[] parseHexStr2Byte(String hexStr) { public static byte[] parseHexStr2Byte(String hexStr) {
if (hexStr.length() < 1) if (hexStr.length() < 1){
return null; return null;
}
byte[] result = new byte[hexStr.length() / 2]; byte[] result = new byte[hexStr.length() / 2];
for (int i = 0; i < hexStr.length() / 2; i++) { for (int i = 0; i < hexStr.length() / 2; i++) {
int high = Integer.parseInt(hexStr.substring(i * 2, i * 2 + 1), 16); int high = Integer.parseInt(hexStr.substring(i * 2, i * 2 + 1), 16);
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment