parent
52f15a571a
commit
a3dad3795b
@ -0,0 +1,160 @@ |
|||||||
|
package com.bellmann.common.util; |
||||||
|
|
||||||
|
import java.io.*; |
||||||
|
|
||||||
|
public class Base64 { |
||||||
|
|
||||||
|
protected static char getChar(int sixbit) { |
||||||
|
if (sixbit >= 0 && sixbit <= 25) { |
||||||
|
return (char)(65 + sixbit); |
||||||
|
} |
||||||
|
|
||||||
|
if (sixbit >= 26 && sixbit <= 51) { |
||||||
|
return (char)(97 + (sixbit - 26)); |
||||||
|
} |
||||||
|
|
||||||
|
if (sixbit >= 52 && sixbit <= 61) { |
||||||
|
return (char)(48 + (sixbit - 52)); |
||||||
|
} |
||||||
|
|
||||||
|
if (sixbit == 62) { |
||||||
|
return '+'; |
||||||
|
} |
||||||
|
|
||||||
|
return sixbit != 63 ? '?' : '/'; |
||||||
|
} |
||||||
|
|
||||||
|
protected static int getValue(char c) { |
||||||
|
if (c >= 'A' && c <= 'Z') { |
||||||
|
return c - 65; |
||||||
|
} |
||||||
|
|
||||||
|
if (c >= 'a' && c <= 'z') { |
||||||
|
return (c - 97) + 26; |
||||||
|
} |
||||||
|
|
||||||
|
if (c >= '0' && c <= '9') { |
||||||
|
return (c - 48) + 52; |
||||||
|
} |
||||||
|
|
||||||
|
if (c == '+') { |
||||||
|
return 62; |
||||||
|
} |
||||||
|
|
||||||
|
if (c == '/') { |
||||||
|
return 63; |
||||||
|
} |
||||||
|
|
||||||
|
return c != '=' ? -1 : 0; |
||||||
|
} |
||||||
|
|
||||||
|
public static String encode(byte raw[]) { |
||||||
|
StringBuffer encoded = new StringBuffer(); |
||||||
|
|
||||||
|
for (int i = 0; i < raw.length; i += 3) { |
||||||
|
encoded.append(encodeBlock(raw, i)); |
||||||
|
} |
||||||
|
|
||||||
|
return encoded.toString(); |
||||||
|
} |
||||||
|
|
||||||
|
protected static char[] encodeBlock(byte raw[], int offset) { |
||||||
|
int block = 0; |
||||||
|
int slack = raw.length - offset - 1; |
||||||
|
int end = slack < 2 ? slack : 2; |
||||||
|
|
||||||
|
for (int i = 0; i <= end; i++) { |
||||||
|
byte b = raw[offset + i]; |
||||||
|
|
||||||
|
int neuter = b >= 0 ? ((int) (b)) : b + 256; |
||||||
|
block += neuter << 8 * (2 - i); |
||||||
|
} |
||||||
|
|
||||||
|
char base64[] = new char[4]; |
||||||
|
|
||||||
|
for (int i = 0; i < 4; i++) { |
||||||
|
int sixbit = block >>> 6 * (3 - i) & 0x3f; |
||||||
|
base64[i] = getChar(sixbit); |
||||||
|
} |
||||||
|
|
||||||
|
if (slack < 1) { |
||||||
|
base64[2] = '='; |
||||||
|
} |
||||||
|
|
||||||
|
if (slack < 2) { |
||||||
|
base64[3] = '='; |
||||||
|
} |
||||||
|
|
||||||
|
return base64; |
||||||
|
} |
||||||
|
|
||||||
|
public static byte[] decode(String base64) { |
||||||
|
int pad = 0; |
||||||
|
|
||||||
|
for (int i = base64.length() - 1; base64.charAt(i) == '='; i--) { |
||||||
|
pad++; |
||||||
|
} |
||||||
|
|
||||||
|
int length = (base64.length() * 6) / 8 - pad; |
||||||
|
byte raw[] = new byte[length]; |
||||||
|
int rawindex = 0; |
||||||
|
|
||||||
|
for (int i = 0; i < base64.length(); i += 4) { |
||||||
|
int block = (getValue(base64.charAt(i)) << 18) + |
||||||
|
(getValue(base64.charAt(i + 1)) << 12) + |
||||||
|
(getValue(base64.charAt(i + 2)) << 6) + |
||||||
|
getValue(base64.charAt(i + 3)); |
||||||
|
|
||||||
|
for (int j = 0; j < 3 && rawindex + j < raw.length; j++) { |
||||||
|
raw[rawindex + j] = (byte)(block >> 8 * (2 - j) & 0xff); |
||||||
|
} |
||||||
|
|
||||||
|
rawindex += 3; |
||||||
|
} |
||||||
|
|
||||||
|
return raw; |
||||||
|
} |
||||||
|
|
||||||
|
public static String objectToString(Object o) { |
||||||
|
if (o == null) { |
||||||
|
return null; |
||||||
|
} |
||||||
|
|
||||||
|
ByteArrayOutputStream baos = new ByteArrayOutputStream(32000); |
||||||
|
|
||||||
|
try { |
||||||
|
ObjectOutputStream os = |
||||||
|
new ObjectOutputStream(new BufferedOutputStream(baos)); |
||||||
|
os.flush(); |
||||||
|
os.writeObject(o); |
||||||
|
os.flush(); |
||||||
|
} |
||||||
|
catch(IOException e) { |
||||||
|
e.printStackTrace(); |
||||||
|
} |
||||||
|
|
||||||
|
return encode(baos.toByteArray()); |
||||||
|
} |
||||||
|
|
||||||
|
public static Object stringToObject(String s) { |
||||||
|
if (s == null) { |
||||||
|
return null; |
||||||
|
} |
||||||
|
|
||||||
|
byte byteArray[] = decode(s); |
||||||
|
|
||||||
|
ByteArrayInputStream baos = new ByteArrayInputStream(byteArray); |
||||||
|
|
||||||
|
try { |
||||||
|
ObjectInputStream is = |
||||||
|
new ObjectInputStream(new BufferedInputStream(baos)); |
||||||
|
|
||||||
|
return is.readObject(); |
||||||
|
} |
||||||
|
catch(Exception e) { |
||||||
|
e.printStackTrace(); |
||||||
|
} |
||||||
|
|
||||||
|
return null; |
||||||
|
} |
||||||
|
} |
@ -0,0 +1,140 @@ |
|||||||
|
package com.bellmann.common.util; |
||||||
|
|
||||||
|
|
||||||
|
import java.io.*; |
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
/** |
||||||
|
* <p> |
||||||
|
* BASE64编码解码工具包 |
||||||
|
* </p> |
||||||
|
* <p> |
||||||
|
* 依赖javabase64-1.3.1.jar |
||||||
|
* </p> |
||||||
|
* |
||||||
|
* @author IceWee |
||||||
|
* @date 2012-5-19 |
||||||
|
* @version 1.0 |
||||||
|
*/ |
||||||
|
public class Base64Utils { |
||||||
|
|
||||||
|
/** |
||||||
|
* 文件读取缓冲区大小 |
||||||
|
*/ |
||||||
|
private static final int CACHE_SIZE = 1024; |
||||||
|
|
||||||
|
/** |
||||||
|
* <p> |
||||||
|
* BASE64字符串解码为二进制数据 |
||||||
|
* </p> |
||||||
|
* |
||||||
|
* @param base64 |
||||||
|
* @return |
||||||
|
* @throws Exception |
||||||
|
*/ |
||||||
|
public static byte[] decode(String base64) throws Exception { |
||||||
|
// return Base64.decode(base64.getBytes());
|
||||||
|
return Base64.decode(base64); |
||||||
|
} |
||||||
|
|
||||||
|
/** |
||||||
|
* <p> |
||||||
|
* 二进制数据编码为BASE64字符串 |
||||||
|
* </p> |
||||||
|
* |
||||||
|
* @param bytes |
||||||
|
* @return |
||||||
|
* @throws Exception |
||||||
|
*/ |
||||||
|
public static String encode(byte[] bytes) throws Exception { |
||||||
|
return new String(Base64.encode(bytes)); |
||||||
|
} |
||||||
|
|
||||||
|
/** |
||||||
|
* <p> |
||||||
|
* 将文件编码为BASE64字符串 |
||||||
|
* </p> |
||||||
|
* <p> |
||||||
|
* 大文件慎用,可能会导致内存溢出 |
||||||
|
* </p> |
||||||
|
* |
||||||
|
* @param filePath 文件绝对路径 |
||||||
|
* @return |
||||||
|
* @throws Exception |
||||||
|
*/ |
||||||
|
public static String encodeFile(String filePath) throws Exception { |
||||||
|
byte[] bytes = fileToByte(filePath); |
||||||
|
return encode(bytes); |
||||||
|
} |
||||||
|
|
||||||
|
/** |
||||||
|
* <p> |
||||||
|
* BASE64字符串转回文件 |
||||||
|
* </p> |
||||||
|
* |
||||||
|
* @param filePath 文件绝对路径 |
||||||
|
* @param base64 编码字符串 |
||||||
|
* @throws Exception |
||||||
|
*/ |
||||||
|
public static void decodeToFile(String filePath, String base64) throws Exception { |
||||||
|
byte[] bytes = decode(base64); |
||||||
|
byteArrayToFile(bytes, filePath); |
||||||
|
} |
||||||
|
|
||||||
|
/** |
||||||
|
* <p> |
||||||
|
* 文件转换为二进制数组 |
||||||
|
* </p> |
||||||
|
* |
||||||
|
* @param filePath 文件路径 |
||||||
|
* @return |
||||||
|
* @throws Exception |
||||||
|
*/ |
||||||
|
public static byte[] fileToByte(String filePath) throws Exception { |
||||||
|
byte[] data = new byte[0]; |
||||||
|
File file = new File(filePath); |
||||||
|
if (file.exists()) { |
||||||
|
FileInputStream in = new FileInputStream(file); |
||||||
|
ByteArrayOutputStream out = new ByteArrayOutputStream(2048); |
||||||
|
byte[] cache = new byte[CACHE_SIZE]; |
||||||
|
int nRead = 0; |
||||||
|
while ((nRead = in.read(cache)) != -1) { |
||||||
|
out.write(cache, 0, nRead); |
||||||
|
out.flush(); |
||||||
|
} |
||||||
|
out.close(); |
||||||
|
in.close(); |
||||||
|
data = out.toByteArray(); |
||||||
|
} |
||||||
|
return data; |
||||||
|
} |
||||||
|
|
||||||
|
/** |
||||||
|
* <p> |
||||||
|
* 二进制数据写文件 |
||||||
|
* </p> |
||||||
|
* |
||||||
|
* @param bytes 二进制数据 |
||||||
|
* @param filePath 文件生成目录 |
||||||
|
*/ |
||||||
|
public static void byteArrayToFile(byte[] bytes, String filePath) throws Exception { |
||||||
|
InputStream in = new ByteArrayInputStream(bytes); |
||||||
|
File destFile = new File(filePath); |
||||||
|
if (!destFile.getParentFile().exists()) { |
||||||
|
destFile.getParentFile().mkdirs(); |
||||||
|
} |
||||||
|
destFile.createNewFile(); |
||||||
|
OutputStream out = new FileOutputStream(destFile); |
||||||
|
byte[] cache = new byte[CACHE_SIZE]; |
||||||
|
int nRead = 0; |
||||||
|
while ((nRead = in.read(cache)) != -1) { |
||||||
|
out.write(cache, 0, nRead); |
||||||
|
out.flush(); |
||||||
|
} |
||||||
|
out.close(); |
||||||
|
in.close(); |
||||||
|
} |
||||||
|
|
||||||
|
|
||||||
|
} |
@ -0,0 +1,58 @@ |
|||||||
|
package com.bellmann.common.util; |
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
/** |
||||||
|
*功能说明: |
||||||
|
* |
||||||
|
*创建人:李涌 |
||||||
|
* |
||||||
|
*创建时间:2013-10-31 上午8:40:02 |
||||||
|
* |
||||||
|
*修改人 修改时间 修改描述 |
||||||
|
* |
||||||
|
* |
||||||
|
*Copyright (c)2013 福建富士通信息软件有限公司-版权所有 |
||||||
|
* |
||||||
|
*/ |
||||||
|
public class HexByteUtil { |
||||||
|
|
||||||
|
// ---------------------------
|
||||||
|
// 16进制字符串转数组
|
||||||
|
public static byte[] hexStr2ByteArr(String strIn) throws Exception { |
||||||
|
byte[] arrB = strIn.getBytes(); |
||||||
|
int iLen = arrB.length; |
||||||
|
|
||||||
|
// 两个字符表示一个字节,所以字节数组长度是字符串长度除以2
|
||||||
|
byte[] arrOut = new byte[iLen / 2]; |
||||||
|
for (int i = 0; i < iLen; i = i + 2) { |
||||||
|
String strTmp = new String(arrB, i, 2); |
||||||
|
arrOut[i / 2] = (byte) Integer.parseInt(strTmp, 16); |
||||||
|
} |
||||||
|
return arrOut; |
||||||
|
} |
||||||
|
|
||||||
|
// 数组转16进制字符串
|
||||||
|
public static String byteArr2HexStr(byte[] arrB) throws Exception { |
||||||
|
int iLen = arrB.length; |
||||||
|
// 每个byte用两个字符才能表示,所以字符串的长度是数组长度的两倍
|
||||||
|
StringBuffer sb = new StringBuffer(iLen * 2); |
||||||
|
for (int i = 0; i < iLen; i++) { |
||||||
|
int intTmp = arrB[i]; |
||||||
|
// 把负数转换为正数
|
||||||
|
while (intTmp < 0) { |
||||||
|
intTmp = intTmp + 256; |
||||||
|
} |
||||||
|
// 小于0F的数需要在前面补0
|
||||||
|
if (intTmp < 16) { |
||||||
|
sb.append("0"); |
||||||
|
} |
||||||
|
sb.append(Integer.toString(intTmp, 16)); |
||||||
|
} |
||||||
|
// 最大128位
|
||||||
|
String result = sb.toString(); |
||||||
|
return result; |
||||||
|
} |
||||||
|
|
||||||
|
} |
@ -0,0 +1,479 @@ |
|||||||
|
package com.bellmann.common.util; |
||||||
|
|
||||||
|
|
||||||
|
import javax.crypto.Cipher; |
||||||
|
import java.io.ByteArrayOutputStream; |
||||||
|
import java.nio.charset.Charset; |
||||||
|
import java.security.*; |
||||||
|
import java.security.interfaces.RSAPrivateKey; |
||||||
|
import java.security.interfaces.RSAPublicKey; |
||||||
|
import java.security.spec.PKCS8EncodedKeySpec; |
||||||
|
import java.security.spec.X509EncodedKeySpec; |
||||||
|
import java.util.HashMap; |
||||||
|
import java.util.Map; |
||||||
|
|
||||||
|
/** |
||||||
|
* <p> |
||||||
|
* RSA公钥/私钥/签名工具包 |
||||||
|
* </p> |
||||||
|
* <p> |
||||||
|
* 罗纳德·李维斯特(Ron [R]ivest)、阿迪·萨莫尔(Adi [S]hamir)和伦纳德·阿德曼(Leonard [A]dleman) |
||||||
|
* </p> |
||||||
|
* <p> |
||||||
|
* 字符串格式的密钥在未在特殊说明情况下都为BASE64编码格式<br/> |
||||||
|
* 由于非对称加密速度极其缓慢,一般文件不使用它来加密而是使用对称加密,<br/> |
||||||
|
* 非对称加密算法可以用来对对称加密的密钥加密,这样保证密钥的安全也就保证了数据的安全 |
||||||
|
* </p> |
||||||
|
* |
||||||
|
* @author IceWee |
||||||
|
* @date 2012-4-26 |
||||||
|
* @version 1.0 |
||||||
|
*/ |
||||||
|
public class RSAUtils { |
||||||
|
|
||||||
|
private static final String DEFAULT_CHARSET_NAME = "UTF-8"; |
||||||
|
|
||||||
|
/** |
||||||
|
* 加密算法RSA |
||||||
|
*/ |
||||||
|
public static final String KEY_ALGORITHM = "RSA"; |
||||||
|
/** |
||||||
|
* Cipher转换名称. |
||||||
|
*/ |
||||||
|
public static final String TRANSFORMATION_NAME = "RSA/ECB/PKCS1Padding"; |
||||||
|
/** |
||||||
|
* 签名算法 |
||||||
|
*/ |
||||||
|
public static final String SIGNATURE_ALGORITHM = "MD5withRSA"; |
||||||
|
|
||||||
|
/** |
||||||
|
* 获取公钥的key |
||||||
|
*/ |
||||||
|
private static final String PUBLIC_KEY = "RSAPublicKey"; |
||||||
|
|
||||||
|
/** |
||||||
|
* 获取私钥的key |
||||||
|
*/ |
||||||
|
private static final String PRIVATE_KEY = "RSAPrivateKey"; |
||||||
|
|
||||||
|
/** |
||||||
|
* RSA最大加密明文大小 |
||||||
|
*/ |
||||||
|
private static final int MAX_ENCRYPT_BLOCK = 117; |
||||||
|
|
||||||
|
/** |
||||||
|
* RSA最大解密密文大小 |
||||||
|
*/ |
||||||
|
private static final int MAX_DECRYPT_BLOCK = 128; |
||||||
|
|
||||||
|
/** |
||||||
|
* <p> |
||||||
|
* 生成密钥对(公钥和私钥) |
||||||
|
* </p> |
||||||
|
* |
||||||
|
* @return |
||||||
|
* @throws Exception |
||||||
|
*/ |
||||||
|
public static Map<String, Object> genKeyPair() throws Exception { |
||||||
|
KeyPairGenerator keyPairGen = KeyPairGenerator |
||||||
|
.getInstance(KEY_ALGORITHM); |
||||||
|
keyPairGen.initialize(1024); |
||||||
|
KeyPair keyPair = keyPairGen.generateKeyPair(); |
||||||
|
RSAPublicKey publicKey = (RSAPublicKey) keyPair.getPublic(); |
||||||
|
RSAPrivateKey privateKey = (RSAPrivateKey) keyPair.getPrivate(); |
||||||
|
Map<String, Object> keyMap = new HashMap<String, Object>(2); |
||||||
|
keyMap.put(PUBLIC_KEY, publicKey); |
||||||
|
keyMap.put(PRIVATE_KEY, privateKey); |
||||||
|
return keyMap; |
||||||
|
} |
||||||
|
|
||||||
|
/** |
||||||
|
* <p> |
||||||
|
* 用私钥对信息生成数字签名 |
||||||
|
* </p> |
||||||
|
* |
||||||
|
* @param data |
||||||
|
* 已加密数据 |
||||||
|
* @param privateKey |
||||||
|
* 私钥(BASE64编码) |
||||||
|
* |
||||||
|
* @return |
||||||
|
* @throws Exception |
||||||
|
*/ |
||||||
|
public static String sign(byte[] data, String privateKey) throws Exception { |
||||||
|
byte[] keyBytes = Base64Utils.decode(privateKey); |
||||||
|
PKCS8EncodedKeySpec pkcs8KeySpec = new PKCS8EncodedKeySpec(keyBytes); |
||||||
|
KeyFactory keyFactory = KeyFactory.getInstance(KEY_ALGORITHM); |
||||||
|
PrivateKey privateK = keyFactory.generatePrivate(pkcs8KeySpec); |
||||||
|
Signature signature = Signature.getInstance(SIGNATURE_ALGORITHM); |
||||||
|
signature.initSign(privateK); |
||||||
|
signature.update(data); |
||||||
|
return Base64Utils.encode(signature.sign()); |
||||||
|
} |
||||||
|
|
||||||
|
/** |
||||||
|
* <p> |
||||||
|
* 校验数字签名 |
||||||
|
* </p> |
||||||
|
* |
||||||
|
* @param data |
||||||
|
* 已加密数据 |
||||||
|
* @param publicKey |
||||||
|
* 公钥(BASE64编码) |
||||||
|
* @param sign |
||||||
|
* 数字签名 |
||||||
|
* |
||||||
|
* @return |
||||||
|
* @throws Exception |
||||||
|
* |
||||||
|
*/ |
||||||
|
public static boolean verify(byte[] data, String publicKey, String sign) |
||||||
|
throws Exception { |
||||||
|
byte[] keyBytes = Base64Utils.decode(publicKey); |
||||||
|
X509EncodedKeySpec keySpec = new X509EncodedKeySpec(keyBytes); |
||||||
|
KeyFactory keyFactory = KeyFactory.getInstance(KEY_ALGORITHM); |
||||||
|
PublicKey publicK = keyFactory.generatePublic(keySpec); |
||||||
|
Signature signature = Signature.getInstance(SIGNATURE_ALGORITHM); |
||||||
|
signature.initVerify(publicK); |
||||||
|
signature.update(data); |
||||||
|
return signature.verify(Base64Utils.decode(sign)); |
||||||
|
} |
||||||
|
|
||||||
|
/** |
||||||
|
* <P> |
||||||
|
* 私钥解密 |
||||||
|
* </p> |
||||||
|
* |
||||||
|
* @param encryptedData |
||||||
|
* 已加密数据 |
||||||
|
* @param privateKey |
||||||
|
* 私钥(BASE64编码) |
||||||
|
* @return |
||||||
|
* @throws Exception |
||||||
|
*/ |
||||||
|
public static byte[] decryptByPrivateKey(byte[] encryptedData, |
||||||
|
String privateKey) throws Exception { |
||||||
|
byte[] keyBytes = Base64Utils.decode(privateKey); |
||||||
|
PKCS8EncodedKeySpec pkcs8KeySpec = new PKCS8EncodedKeySpec(keyBytes); |
||||||
|
KeyFactory keyFactory = KeyFactory.getInstance(KEY_ALGORITHM); |
||||||
|
Key privateK = keyFactory.generatePrivate(pkcs8KeySpec); |
||||||
|
Cipher cipher = Cipher.getInstance(TRANSFORMATION_NAME); |
||||||
|
cipher.init(Cipher.DECRYPT_MODE, privateK); |
||||||
|
int inputLen = encryptedData.length; |
||||||
|
ByteArrayOutputStream out = new ByteArrayOutputStream(); |
||||||
|
int offSet = 0; |
||||||
|
byte[] cache; |
||||||
|
int i = 0; |
||||||
|
// 对数据分段解密
|
||||||
|
while (inputLen - offSet > 0) { |
||||||
|
if (inputLen - offSet > MAX_DECRYPT_BLOCK) { |
||||||
|
cache = cipher |
||||||
|
.doFinal(encryptedData, offSet, MAX_DECRYPT_BLOCK); |
||||||
|
} else { |
||||||
|
cache = cipher |
||||||
|
.doFinal(encryptedData, offSet, inputLen - offSet); |
||||||
|
} |
||||||
|
out.write(cache, 0, cache.length); |
||||||
|
i++; |
||||||
|
offSet = i * MAX_DECRYPT_BLOCK; |
||||||
|
} |
||||||
|
byte[] decryptedData = out.toByteArray(); |
||||||
|
out.close(); |
||||||
|
return decryptedData; |
||||||
|
} |
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
/** |
||||||
|
* <p> |
||||||
|
* 公钥解密 |
||||||
|
* </p> |
||||||
|
* |
||||||
|
* @param encryptedData |
||||||
|
* 已加密数据 |
||||||
|
* @param publicKey |
||||||
|
* 公钥(BASE64编码) |
||||||
|
* @return |
||||||
|
* @throws Exception |
||||||
|
*/ |
||||||
|
public static byte[] decryptByPublicKey(byte[] encryptedData, |
||||||
|
String publicKey) throws Exception { |
||||||
|
byte[] keyBytes = Base64Utils.decode(publicKey); |
||||||
|
X509EncodedKeySpec x509KeySpec = new X509EncodedKeySpec(keyBytes); |
||||||
|
KeyFactory keyFactory = KeyFactory.getInstance(KEY_ALGORITHM); |
||||||
|
Key publicK = keyFactory.generatePublic(x509KeySpec); |
||||||
|
Cipher cipher = Cipher.getInstance("RSA/ECB/PKCS1Padding"); |
||||||
|
cipher.init(Cipher.DECRYPT_MODE, publicK); |
||||||
|
int inputLen = encryptedData.length; |
||||||
|
ByteArrayOutputStream out = new ByteArrayOutputStream(); |
||||||
|
int offSet = 0; |
||||||
|
byte[] cache; |
||||||
|
int i = 0; |
||||||
|
// 对数据分段解密
|
||||||
|
while (inputLen - offSet > 0) { |
||||||
|
if (inputLen - offSet > MAX_DECRYPT_BLOCK) { |
||||||
|
cache = cipher |
||||||
|
.doFinal(encryptedData, offSet, MAX_DECRYPT_BLOCK); |
||||||
|
} else { |
||||||
|
cache = cipher |
||||||
|
.doFinal(encryptedData, offSet, inputLen - offSet); |
||||||
|
} |
||||||
|
out.write(cache, 0, cache.length); |
||||||
|
i++; |
||||||
|
offSet = i * MAX_DECRYPT_BLOCK; |
||||||
|
} |
||||||
|
byte[] decryptedData = out.toByteArray(); |
||||||
|
out.close(); |
||||||
|
return decryptedData; |
||||||
|
} |
||||||
|
|
||||||
|
|
||||||
|
/** |
||||||
|
* <p> |
||||||
|
* 公钥加密 |
||||||
|
* </p> |
||||||
|
* |
||||||
|
* @param data |
||||||
|
* 源数据 |
||||||
|
* @param publicKey |
||||||
|
* 公钥(BASE64编码) |
||||||
|
* @return |
||||||
|
* @throws Exception |
||||||
|
*/ |
||||||
|
public static byte[] encryptByPublicKey(byte[] data, String publicKey) |
||||||
|
throws Exception { |
||||||
|
byte[] keyBytes = Base64Utils.decode(publicKey); |
||||||
|
X509EncodedKeySpec x509KeySpec = new X509EncodedKeySpec(keyBytes); |
||||||
|
KeyFactory keyFactory = KeyFactory.getInstance(KEY_ALGORITHM); |
||||||
|
Key publicK = keyFactory.generatePublic(x509KeySpec); |
||||||
|
// 对数据加密
|
||||||
|
Cipher cipher = Cipher.getInstance(TRANSFORMATION_NAME); |
||||||
|
cipher.init(Cipher.ENCRYPT_MODE, publicK); |
||||||
|
int inputLen = data.length; |
||||||
|
ByteArrayOutputStream out = new ByteArrayOutputStream(); |
||||||
|
int offSet = 0; |
||||||
|
byte[] cache; |
||||||
|
int i = 0; |
||||||
|
// 对数据分段加密
|
||||||
|
while (inputLen - offSet > 0) { |
||||||
|
if (inputLen - offSet > MAX_ENCRYPT_BLOCK) { |
||||||
|
cache = cipher.doFinal(data, offSet, MAX_ENCRYPT_BLOCK); |
||||||
|
} else { |
||||||
|
cache = cipher.doFinal(data, offSet, inputLen - offSet); |
||||||
|
} |
||||||
|
out.write(cache, 0, cache.length); |
||||||
|
i++; |
||||||
|
offSet = i * MAX_ENCRYPT_BLOCK; |
||||||
|
} |
||||||
|
byte[] encryptedData = out.toByteArray(); |
||||||
|
out.close(); |
||||||
|
return encryptedData; |
||||||
|
} |
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
/** |
||||||
|
* <p> |
||||||
|
* 私钥加密 |
||||||
|
* </p> |
||||||
|
* |
||||||
|
* @param data |
||||||
|
* 源数据 |
||||||
|
* @param privateKey |
||||||
|
* 私钥(BASE64编码) |
||||||
|
* @return |
||||||
|
* @throws Exception |
||||||
|
*/ |
||||||
|
public static byte[] encryptByPrivateKey(byte[] data, String privateKey) |
||||||
|
throws Exception { |
||||||
|
byte[] keyBytes = Base64Utils.decode(privateKey); |
||||||
|
PKCS8EncodedKeySpec pkcs8KeySpec = new PKCS8EncodedKeySpec(keyBytes); |
||||||
|
KeyFactory keyFactory = KeyFactory.getInstance(KEY_ALGORITHM); |
||||||
|
Key privateK = keyFactory.generatePrivate(pkcs8KeySpec); |
||||||
|
Cipher cipher = Cipher.getInstance(TRANSFORMATION_NAME); |
||||||
|
cipher.init(Cipher.ENCRYPT_MODE, privateK); |
||||||
|
int inputLen = data.length; |
||||||
|
ByteArrayOutputStream out = new ByteArrayOutputStream(); |
||||||
|
int offSet = 0; |
||||||
|
byte[] cache; |
||||||
|
int i = 0; |
||||||
|
// 对数据分段加密
|
||||||
|
while (inputLen - offSet > 0) { |
||||||
|
if (inputLen - offSet > MAX_ENCRYPT_BLOCK) { |
||||||
|
cache = cipher.doFinal(data, offSet, MAX_ENCRYPT_BLOCK); |
||||||
|
} else { |
||||||
|
cache = cipher.doFinal(data, offSet, inputLen - offSet); |
||||||
|
} |
||||||
|
out.write(cache, 0, cache.length); |
||||||
|
i++; |
||||||
|
offSet = i * MAX_ENCRYPT_BLOCK; |
||||||
|
} |
||||||
|
byte[] encryptedData = out.toByteArray(); |
||||||
|
out.close(); |
||||||
|
return encryptedData; |
||||||
|
} |
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
/** |
||||||
|
* <p> |
||||||
|
* 获取私钥 |
||||||
|
* </p> |
||||||
|
* |
||||||
|
* @param keyMap |
||||||
|
* 密钥对 |
||||||
|
* @return |
||||||
|
* @throws Exception |
||||||
|
*/ |
||||||
|
public static String getPrivateKey(Map<String, Object> keyMap) |
||||||
|
throws Exception { |
||||||
|
Key key = (Key) keyMap.get(PRIVATE_KEY); |
||||||
|
return Base64Utils.encode(key.getEncoded()); |
||||||
|
} |
||||||
|
|
||||||
|
/** |
||||||
|
* <p> |
||||||
|
* 获取公钥 |
||||||
|
* </p> |
||||||
|
* |
||||||
|
* @param keyMap |
||||||
|
* 密钥对 |
||||||
|
* @return |
||||||
|
* @throws Exception |
||||||
|
*/ |
||||||
|
public static String getPublicKey(Map<String, Object> keyMap) |
||||||
|
throws Exception { |
||||||
|
Key key = (Key) keyMap.get(PUBLIC_KEY); |
||||||
|
return Base64Utils.encode(key.getEncoded()); |
||||||
|
} |
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
/** |
||||||
|
* 对信息进行加密,通过判断是否为DES 和 RSA,进行不同的加密 |
||||||
|
* @param algorithm |
||||||
|
* @param dataStr |
||||||
|
* @param privateKey |
||||||
|
* @return |
||||||
|
* @throws Exception |
||||||
|
*/ |
||||||
|
public static String encryptByPrivateKeyStr(String algorithm,String dataStr, String privateKey) |
||||||
|
throws Exception { |
||||||
|
|
||||||
|
if(RSAUtils.KEY_ALGORITHM.equals(algorithm)){ |
||||||
|
byte[] data =dataStr.getBytes(DEFAULT_CHARSET_NAME); |
||||||
|
String encryptedDateStr = HexByteUtil.byteArr2HexStr(encryptByPrivateKey(data, privateKey)); |
||||||
|
return encryptedDateStr; |
||||||
|
}else if(SimpleDESCry.DES_ALGORITHM.equals(algorithm)){ |
||||||
|
return encryptWithDes(dataStr, privateKey); |
||||||
|
} |
||||||
|
return null; |
||||||
|
} |
||||||
|
|
||||||
|
/** |
||||||
|
* DEC算法加密 |
||||||
|
* @param json |
||||||
|
* @param key |
||||||
|
* @return |
||||||
|
* @throws Exception |
||||||
|
*/ |
||||||
|
public static String encryptWithDes(String json,String key) throws Exception{ |
||||||
|
SimpleDESCry sc = new SimpleDESCry(); |
||||||
|
return sc.cry(new StringBuffer(Base64.encode(json.getBytes(Charset.forName("UTF-8")))),key).toString(); |
||||||
|
} |
||||||
|
|
||||||
|
/** |
||||||
|
* 公钥加密 |
||||||
|
* @param dataStr |
||||||
|
* @param publicKey |
||||||
|
* @return |
||||||
|
* @throws Exception |
||||||
|
*/ |
||||||
|
public static String encryptByPublicKeyStr(String dataStr, String publicKey) |
||||||
|
throws Exception { |
||||||
|
byte[] data =dataStr.getBytes(DEFAULT_CHARSET_NAME); |
||||||
|
String encryptedDateStr = HexByteUtil.byteArr2HexStr(encryptByPublicKey(data, publicKey)); |
||||||
|
//String encryptedDateStr = Base64Utils.encode(encryptByPublicKey(data, publicKey));
|
||||||
|
return encryptedDateStr; |
||||||
|
} |
||||||
|
|
||||||
|
|
||||||
|
/** |
||||||
|
* 对信息进行解密,通过判断是否为DES 和 RSA,进行不同公钥的解密 |
||||||
|
* @param algorithm |
||||||
|
* @param encryptedDataStr |
||||||
|
* @param publicKey |
||||||
|
* @return |
||||||
|
* @throws Exception |
||||||
|
*/ |
||||||
|
public static String decryptByPublicKeyStr(String algorithm,String encryptedDataStr, |
||||||
|
String publicKey) throws Exception { |
||||||
|
|
||||||
|
if(RSAUtils.KEY_ALGORITHM.equals(algorithm)){ |
||||||
|
byte[] encryptedData = HexByteUtil.hexStr2ByteArr(encryptedDataStr); |
||||||
|
String str = new String(decryptByPublicKey(encryptedData, publicKey),DEFAULT_CHARSET_NAME); |
||||||
|
return str; |
||||||
|
}else if(SimpleDESCry.DES_ALGORITHM.equals(algorithm)){ |
||||||
|
return decryptAccessTokenWithDes(encryptedDataStr, publicKey); |
||||||
|
} |
||||||
|
return null; |
||||||
|
|
||||||
|
} |
||||||
|
/** |
||||||
|
* 公钥解密 |
||||||
|
* @param encryptString |
||||||
|
* @param key |
||||||
|
* @return |
||||||
|
* @throws Exception |
||||||
|
*/ |
||||||
|
public static String decryptAccessTokenWithDes(String encryptString ,String key) throws Exception{ |
||||||
|
SimpleDESCry sc = new SimpleDESCry(); |
||||||
|
return new String(Base64.decode(sc.decry(new StringBuffer(encryptString), key).toString()),Charset.forName("UTF-8")); |
||||||
|
} |
||||||
|
|
||||||
|
|
||||||
|
/** |
||||||
|
* 私钥解密 |
||||||
|
* @param encryptedDataStr |
||||||
|
* @param privateKey |
||||||
|
* @return |
||||||
|
* @throws Exception |
||||||
|
*/ |
||||||
|
public static String decryptByPrivateKeyStr(String encryptedDataStr, |
||||||
|
String privateKey) throws Exception { |
||||||
|
byte[] encryptedData = HexByteUtil.hexStr2ByteArr(encryptedDataStr); |
||||||
|
String str = new String(decryptByPrivateKey(encryptedData, privateKey),DEFAULT_CHARSET_NAME); |
||||||
|
return str; |
||||||
|
} |
||||||
|
|
||||||
|
|
||||||
|
/** |
||||||
|
* 采用RSAUtils进行加密、解密的样例 |
||||||
|
* @param args |
||||||
|
*/ |
||||||
|
public static void main(String[] args) { |
||||||
|
|
||||||
|
//采用RSA 进行加密、解密
|
||||||
|
String pri_key="MIICdgIBADANBgkqhkiG9w0BAQEFAASCAmAwggJcAgEAAoGBAJUmTjX8Ac7cu/CKGgEbEHztvD0JkueVCasiwBBKYDLGnmxoQRPOcQ4Qa5T6NAgoedDwdzTMcx11fZOBFeYE737JNISgzAm5bV5kv7puJGR/xRFOh34xK0fdV36uX+/ou834BA518IWXYHtfNu+J0oXhdFRUF2N6BkjV6cxOMHxxAgMBAAECgYBLCSFx4V3bf4uBwUjB46bQ4x2/q3Bf18gsc+giifiJswkCyxRqbYLNK4+kPJguvoZCiqe+RVloqbJKojetKDCAV0etcxNWn7fatBi/I9Ip3OgDtmOjViQPtJfEuNM2gnMUdPLUjGmlA/3avQ4jL0M94OVk6o6Quu08AaZADfvkzQJBAN86uy/u4xaUG6xu8zIvMhcovlsEwMOZwD6ZGBXP6Ks0Xq141Uh9oUdAnrKENDiWrnePDPwwtcy80gX259hVfn8CQQCrC43N7/dlp1sF3uFlVPklSVdRxopy3FdIGN59O4CfspES6OGlx6sBvHDj0Y/xOUE7y9gOfiLmZPdUXGHArm0PAkEAxWFM+kkkCmbTQVipOQp6I/gmuJwvRcTBM5hyr3ayqA8aWBCrpflPjY0ZxYcYFaFGivi15pe7aFjc6+ExhxuxSQJAJnZNp95rIle0tyg8RP4LnF/Wm9PqdBOqKNM2zLBhpwePadImU5IHmTdXp1MLbDH0wk5/QhsJHAfeOgEgFiTi7wJAFJSLH9cfF3Bm8dO621dg6salg7Lc8u96geBHkCq9tngX1SgH7sTChIdRKR/IPG+JBskuPl/C5rImhdRbMk/Cfw=="; |
||||||
|
|
||||||
|
String pub_key = "MIGfMA0GCSqGSIb3DQEBAQUAA4GNADCBiQKBgQCVJk41/AHO3LvwihoBGxB87bw9CZLnlQmrIsAQSmAyxp5saEETznEOEGuU+jQIKHnQ8Hc0zHMddX2TgRXmBO9+yTSEoMwJuW1eZL+6biRkf8URTod+MStH3Vd+rl/v6LvN+AQOdfCFl2B7XzbvidKF4XRUVBdjegZI1enMTjB8cQIDAQAB"; |
||||||
|
|
||||||
|
String pwd = "76&#K7e#"; |
||||||
|
|
||||||
|
try { |
||||||
|
pwd = RSAUtils.encryptByPrivateKeyStr("RSA",pwd, pri_key); |
||||||
|
System.out.println("加密== "+ pwd); |
||||||
|
|
||||||
|
pwd = RSAUtils.decryptByPublicKeyStr("RSA",pwd, pub_key); |
||||||
|
System.out.println("解密== "+ pwd); |
||||||
|
|
||||||
|
} catch (Exception e) { |
||||||
|
// TODO Auto-generated catch block
|
||||||
|
e.printStackTrace(); |
||||||
|
} |
||||||
|
|
||||||
|
} |
||||||
|
|
||||||
|
|
||||||
|
} |
@ -0,0 +1,259 @@ |
|||||||
|
package com.bellmann.common.util; |
||||||
|
|
||||||
|
import java.nio.charset.Charset; |
||||||
|
import java.util.ArrayList; |
||||||
|
import java.util.List; |
||||||
|
|
||||||
|
|
||||||
|
/** |
||||||
|
* |
||||||
|
*功能说明:内部DES加密算法实现,兼容C语言加解密.该方法源码由CRM-曾臻提供. |
||||||
|
* |
||||||
|
*创建人:李涌 |
||||||
|
* |
||||||
|
*创建时间:2013-11-29 下午5:11:18 |
||||||
|
* |
||||||
|
*修改人 修改时间 修改描述 |
||||||
|
* |
||||||
|
* |
||||||
|
*Copyright (c)2013 福建富士通信息软件有限公司-版权所有 |
||||||
|
* |
||||||
|
*/ |
||||||
|
public class SimpleDESCry { |
||||||
|
|
||||||
|
public static final String DES_ALGORITHM = "DES"; |
||||||
|
|
||||||
|
public static final String PRIVATE_KEY = "FFCS_EAM2014"; |
||||||
|
// 明文字符串最大长度
|
||||||
|
private static final int SOURCE_STRING_MAX_LEN = 2048; |
||||||
|
// 密文字符串最大长度
|
||||||
|
private static final int ENCRYPT_STRING_MAX_LEN = 2048; |
||||||
|
|
||||||
|
private static final Charset default_charset = Charset.forName("UTF-8"); |
||||||
|
|
||||||
|
private static final StringBuffer str_set=new StringBuffer("abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890-_=+{}[]|\':;.>,<?/`~!@#$%^&*()");//="abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890-_=+{}[]|\':;.>,<?/`~!@#$%^&*()";
|
||||||
|
private static final StringBuffer str_pw_set=new StringBuffer("abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890");//="abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890";
|
||||||
|
private static final char ch_double_quotes = '"';// = '"';
|
||||||
|
//StringBuffer key =new StringBuffer();//= "*bv_.azqadec;d7efbikop,01-fre382";
|
||||||
|
|
||||||
|
StringBuffer des=new StringBuffer(); |
||||||
|
StringBuffer org=new StringBuffer(); |
||||||
|
StringBuffer errormsg=new StringBuffer(); |
||||||
|
|
||||||
|
public SimpleDESCry() { |
||||||
|
//str_set.append("abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890-_=+{}[]|\':;.>,<?/`~!@#$%^&*()");
|
||||||
|
//str_pw_set.append("abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890");
|
||||||
|
//ch_double_quotes = '"';
|
||||||
|
//key.append(privateKey);
|
||||||
|
} |
||||||
|
public StringBuffer cry(StringBuffer src2,String privateKey){ |
||||||
|
byte[] src=src2.toString().getBytes(default_charset); |
||||||
|
StringBuffer tmpch=new StringBuffer(); |
||||||
|
int k; |
||||||
|
int n_invalid = 1; |
||||||
|
int i=src.length; |
||||||
|
int l_set=str_set.length(); |
||||||
|
|
||||||
|
if(!isvalid_org_text(src2)) return des; |
||||||
|
|
||||||
|
des.setLength(0); |
||||||
|
|
||||||
|
for(k=0;k<i;k++) |
||||||
|
{ |
||||||
|
String s=String.format("%1$02x", src[k] ^ privateKey.getBytes(default_charset)[k%privateKey.length()]) ; |
||||||
|
des.append(s); |
||||||
|
} |
||||||
|
|
||||||
|
while(k<16) |
||||||
|
{ |
||||||
|
byte[] des2=des.toString().getBytes(default_charset); |
||||||
|
byte[] key2=privateKey.getBytes(default_charset); |
||||||
|
//String s=String.format("%1$02x", des2[2*k]^key2[k%key.length()]);
|
||||||
|
String s=String.format("%1$02x", 0^key2[k%privateKey.length()]); |
||||||
|
des.append(s); |
||||||
|
k++; |
||||||
|
} |
||||||
|
|
||||||
|
return des; |
||||||
|
} |
||||||
|
|
||||||
|
/*判断输入的需要加密的源字符串是否合法 不允许为空格,退格键,回车及其它键盘上没有的字符*/ |
||||||
|
boolean isvalid_org_text(StringBuffer src_org) |
||||||
|
{ |
||||||
|
int i=src_org.length(); |
||||||
|
short n_invalid; |
||||||
|
|
||||||
|
int l_set = str_set.length(); |
||||||
|
|
||||||
|
if(i>SOURCE_STRING_MAX_LEN) |
||||||
|
{ |
||||||
|
errormsg.setLength(0); |
||||||
|
errormsg.append("source string is too long!\n"); |
||||||
|
return false; |
||||||
|
} |
||||||
|
|
||||||
|
for(int k = 0;k<i;k++) |
||||||
|
{ |
||||||
|
n_invalid = 1; |
||||||
|
for(int j=0;j<l_set;j++) |
||||||
|
{ |
||||||
|
if(src_org.charAt(k)==str_set.charAt(j) || src_org.charAt(k) ==ch_double_quotes) |
||||||
|
{ |
||||||
|
n_invalid = 0; |
||||||
|
break; |
||||||
|
} |
||||||
|
} |
||||||
|
|
||||||
|
if(n_invalid==1) |
||||||
|
{ |
||||||
|
errormsg.setLength(0); |
||||||
|
errormsg.append("Invalid char in source string! \n"); |
||||||
|
return false; |
||||||
|
} |
||||||
|
} |
||||||
|
|
||||||
|
return true; |
||||||
|
} |
||||||
|
|
||||||
|
|
||||||
|
/*判断输入的需要解密的字符串是否合法*/ |
||||||
|
boolean isvalid_pw_text(StringBuffer src_pw) |
||||||
|
{ |
||||||
|
if(src_pw.length()>ENCRYPT_STRING_MAX_LEN || src_pw.length()%2>0 || src_pw.length()==0) |
||||||
|
{ |
||||||
|
errormsg.setLength(0); |
||||||
|
errormsg.append("length of the source string is error!\n"); |
||||||
|
return false; |
||||||
|
} |
||||||
|
|
||||||
|
char p_src; |
||||||
|
char p_pw_set; |
||||||
|
short n_valid; |
||||||
|
for(int i=0;i<src_pw.length();i++) |
||||||
|
{ |
||||||
|
p_src=src_pw.charAt(i); |
||||||
|
|
||||||
|
n_valid = 0; |
||||||
|
for(int j=0;j<str_pw_set.length();j++) |
||||||
|
{ |
||||||
|
p_pw_set=str_pw_set.charAt(j); |
||||||
|
if(p_pw_set == p_src) |
||||||
|
{ |
||||||
|
n_valid = 1; |
||||||
|
break; |
||||||
|
} |
||||||
|
} |
||||||
|
|
||||||
|
if(n_valid==0) |
||||||
|
{ |
||||||
|
errormsg.setLength(0); |
||||||
|
errormsg.append("invalid char in password text!\n"); |
||||||
|
return false; |
||||||
|
} |
||||||
|
} |
||||||
|
|
||||||
|
return true; |
||||||
|
} |
||||||
|
|
||||||
|
/*解密字符串,输出结果为解密后的字符串,若输入为非法密文,返回空,记录错误信息*/ |
||||||
|
public StringBuffer decry(StringBuffer src,String privateKey) |
||||||
|
{ |
||||||
|
//char tmpch[3]={0};
|
||||||
|
//char tmpdesch[2]={0};
|
||||||
|
|
||||||
|
int k = 0; |
||||||
|
if(!isvalid_pw_text(src)) return new StringBuffer(""); |
||||||
|
|
||||||
|
List<Byte> list=new ArrayList<Byte>(); |
||||||
|
|
||||||
|
for(int i=0;i<src.length();i+=2) |
||||||
|
{ |
||||||
|
String s=src.substring(i,i+2); |
||||||
|
int hex=hexstrtodec(new StringBuffer(s)); |
||||||
|
byte b=(byte)hex; |
||||||
|
b^=privateKey.getBytes(default_charset)[k%privateKey.length()]; |
||||||
|
|
||||||
|
if(b==0) |
||||||
|
break; |
||||||
|
|
||||||
|
list.add(b); |
||||||
|
|
||||||
|
k++; |
||||||
|
} |
||||||
|
|
||||||
|
Byte[] bytes=(Byte[])list.toArray(new Byte[list.size()]); |
||||||
|
byte[] bytes2=new byte[bytes.length]; |
||||||
|
for(int i=0;i<bytes.length;i++){ |
||||||
|
bytes2[i]=bytes[i].byteValue(); |
||||||
|
} |
||||||
|
String ss=new String(bytes2,default_charset); |
||||||
|
return new StringBuffer(ss); |
||||||
|
} |
||||||
|
|
||||||
|
/*将十六进制字符串转换为十进制数*/ |
||||||
|
int hexstrtodec(StringBuffer strhex) |
||||||
|
{ |
||||||
|
int v=0; |
||||||
|
int total=0; |
||||||
|
int l = strhex.length(); |
||||||
|
for(int i=0;i<l;i++) |
||||||
|
{ |
||||||
|
switch(strhex.charAt(i)) |
||||||
|
{ |
||||||
|
case '0':v = 0;break; |
||||||
|
case '1':v = 1;break; |
||||||
|
case '2':v = 2;break; |
||||||
|
case '3':v = 3;break; |
||||||
|
case '4':v = 4;break; |
||||||
|
case '5':v = 5;break; |
||||||
|
case '6':v = 6;break; |
||||||
|
case '7':v = 7;break; |
||||||
|
case '8':v = 8;break; |
||||||
|
case '9':v = 9;break; |
||||||
|
case 'a':v = 10;break; |
||||||
|
case 'b':v = 11;break; |
||||||
|
case 'c':v = 12;break; |
||||||
|
case 'd':v = 13;break; |
||||||
|
case 'e':v = 14;break; |
||||||
|
case 'f':v = 15;break; |
||||||
|
} |
||||||
|
total = total*16+v; |
||||||
|
} |
||||||
|
return total; |
||||||
|
} |
||||||
|
|
||||||
|
|
||||||
|
public static final void main(String[] args){ |
||||||
|
SimpleDESCry sc=new SimpleDESCry(); |
||||||
|
StringBuffer a=sc.cry(new StringBuffer("QmFzZTY0IOWKoOWvhuino+Wvhg=="),"*bv_.azqadec;d7efbikop,01-fre382"); |
||||||
|
System.out.println(a); |
||||||
|
|
||||||
|
StringBuffer b=sc.decry(new StringBuffer("7b0f302574352341282b3228542b60130e170005005b7b46594a5b4f"),"*bv_.azqadec;d7efbikop,01-fre382"); |
||||||
|
System.out.println(b); |
||||||
|
SimpleDESCry sc2 = new SimpleDESCry(); |
||||||
|
String a2 =sc2.cry(new StringBuffer(Base64.encode("123456aaa".getBytes(Charset.forName("UTF-8")))),PRIVATE_KEY).toString(); |
||||||
|
System.out.println(a2); |
||||||
|
String b2 = "5c0f654c1a2a00164626015d2530702c7a11280163563d530f5c3d507c7e2f256e1c7d4e14372a357139571b2620511678300e397f3136320c162914725238595d0c75752143145c42246a51112f49105430063255083e29103f431b667c3013742262070d2617156e257d180a2368707a3c33187e22291b1728140a677b2b5b5b445b5a221c140445396e440d0d4e2c7b3c240a7f0c3d5100383a51656f381c772162020e3517587e1c6242090d7b296c1201507f2222091005040a4f602f075a1f6600250b041643367e451e0c672f6c4a5b515308350816053a1b647c0a1c771c444e0d080b146a22715b0d0a10336c150e136b30260e3b5e460f62520e02742248030d3617596a2575500933642c793b280a6b3e2e510d02251772553712701c405f0d081b1b6a25795109334a767a2c27187c0c3d1917383a57625116025b456558162a181c7d32711e0d0d6703642c3b52601c3d5114283a53656c02116b3261700d361b166835535f1623463c7a3c38237c3226251002431062552f5a5a18655a221c1c1a441c5c1920304d2e7c120d097c333223170629276552301c741c7e070e0800296a416d5f0a336c717a2c20257e222d5517073a1b667d2c027535654c2340773844367e511e3468737c15575153211308152f3917490a2b1b5b317967213577197d22711e0d0d64717a2c23527e32211b1438361b625116025a31694c23412a1a441c69410b0e683d78122f507f323d083f3d4e5e"; |
||||||
|
System.out.println(new String(Base64.decode(sc.decry(new StringBuffer(b2), "9v/6@rNl'q8(Dg!E5xb`1fdaYlsc+8ak").toString()),Charset.forName("UTF-8"))); |
||||||
|
System.out.println(encry("aaa888888")); |
||||||
|
System.out.println(decry("")); |
||||||
|
|
||||||
|
System.out.println(encry("4A@12345")); |
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
} |
||||||
|
|
||||||
|
public static String encry(String data){ |
||||||
|
SimpleDESCry sdc = new SimpleDESCry(); |
||||||
|
String enData = sdc.cry(new StringBuffer(data), PRIVATE_KEY).toString(); |
||||||
|
return enData; |
||||||
|
} |
||||||
|
|
||||||
|
public static String decry(String enData){ |
||||||
|
SimpleDESCry sdc = new SimpleDESCry(); |
||||||
|
StringBuffer data = sdc.decry(new StringBuffer(enData),PRIVATE_KEY); |
||||||
|
return data.toString(); |
||||||
|
} |
||||||
|
|
||||||
|
} |
@ -0,0 +1,59 @@ |
|||||||
|
package com.bellmann.config; |
||||||
|
|
||||||
|
|
||||||
|
import org.springframework.boot.context.properties.ConfigurationProperties; |
||||||
|
import org.springframework.stereotype.Component; |
||||||
|
|
||||||
|
@Component |
||||||
|
@ConfigurationProperties(prefix = "login") |
||||||
|
public class Config4A { |
||||||
|
|
||||||
|
private String url; |
||||||
|
|
||||||
|
private String clientId; |
||||||
|
|
||||||
|
private String security; |
||||||
|
|
||||||
|
private String redirectUrl; |
||||||
|
|
||||||
|
private String httpTokenAuthUrl; |
||||||
|
public String getUrl() { |
||||||
|
return url; |
||||||
|
} |
||||||
|
|
||||||
|
public void setUrl(String url) { |
||||||
|
this.url = url; |
||||||
|
} |
||||||
|
|
||||||
|
public String getClientId() { |
||||||
|
return clientId; |
||||||
|
} |
||||||
|
|
||||||
|
public void setClientId(String clientId) { |
||||||
|
this.clientId = clientId; |
||||||
|
} |
||||||
|
|
||||||
|
public String getSecurity() { |
||||||
|
return security; |
||||||
|
} |
||||||
|
|
||||||
|
public void setSecurity(String security) { |
||||||
|
this.security = security; |
||||||
|
} |
||||||
|
|
||||||
|
public String getRedirectUrl() { |
||||||
|
return redirectUrl; |
||||||
|
} |
||||||
|
|
||||||
|
public void setRedirectUrl(String redirectUrl) { |
||||||
|
this.redirectUrl = redirectUrl; |
||||||
|
} |
||||||
|
|
||||||
|
public String getHttpTokenAuthUrl() { |
||||||
|
return httpTokenAuthUrl; |
||||||
|
} |
||||||
|
|
||||||
|
public void setHttpTokenAuthUrl(String httpTokenAuthUrl) { |
||||||
|
this.httpTokenAuthUrl = httpTokenAuthUrl; |
||||||
|
} |
||||||
|
} |
@ -0,0 +1,98 @@ |
|||||||
|
package com.bellmann.model.login; |
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
import java.io.Serializable; |
||||||
|
|
||||||
|
/** |
||||||
|
* 自定义 token 信息体 |
||||||
|
* |
||||||
|
* @author xiangy |
||||||
|
* @date 2023-1-15 |
||||||
|
*/ |
||||||
|
|
||||||
|
public class AccessTokenResponse implements Serializable { |
||||||
|
|
||||||
|
private static final long serialVersionUID = -1L; |
||||||
|
|
||||||
|
/** |
||||||
|
* 事务ID |
||||||
|
*/ |
||||||
|
|
||||||
|
private String sessionid; |
||||||
|
|
||||||
|
/** |
||||||
|
* token 信息 |
||||||
|
*/ |
||||||
|
|
||||||
|
private String tokenvalue; |
||||||
|
|
||||||
|
/** |
||||||
|
* 用户信息 |
||||||
|
*/ |
||||||
|
|
||||||
|
private LdapStaffVo user; |
||||||
|
|
||||||
|
/** |
||||||
|
* 系统发生跳转时传递的组织UUID. |
||||||
|
*/ |
||||||
|
|
||||||
|
private String currentorguuid; |
||||||
|
|
||||||
|
/** |
||||||
|
* 单点使用的备用字段 |
||||||
|
*/ |
||||||
|
private String ssoSpare; |
||||||
|
|
||||||
|
public String getSessionid() { |
||||||
|
return sessionid; |
||||||
|
} |
||||||
|
|
||||||
|
public void setSessionid(String sessionid) { |
||||||
|
this.sessionid = sessionid; |
||||||
|
} |
||||||
|
|
||||||
|
public String getTokenvalue() { |
||||||
|
return tokenvalue; |
||||||
|
} |
||||||
|
|
||||||
|
public void setTokenvalue(String tokenvalue) { |
||||||
|
this.tokenvalue = tokenvalue; |
||||||
|
} |
||||||
|
|
||||||
|
public LdapStaffVo getUser() { |
||||||
|
return user; |
||||||
|
} |
||||||
|
|
||||||
|
public void setUser(LdapStaffVo user) { |
||||||
|
this.user = user; |
||||||
|
} |
||||||
|
|
||||||
|
public String getCurrentorguuid() { |
||||||
|
return currentorguuid; |
||||||
|
} |
||||||
|
|
||||||
|
public void setCurrentorguuid(String currentorguuid) { |
||||||
|
this.currentorguuid = currentorguuid; |
||||||
|
} |
||||||
|
|
||||||
|
public String getSsoSpare() { |
||||||
|
return ssoSpare; |
||||||
|
} |
||||||
|
|
||||||
|
public void setSsoSpare(String ssoSpare) { |
||||||
|
this.ssoSpare = ssoSpare; |
||||||
|
} |
||||||
|
|
||||||
|
public static long getSerialversionuid() { |
||||||
|
return serialVersionUID; |
||||||
|
} |
||||||
|
|
||||||
|
@Override |
||||||
|
public String toString() { |
||||||
|
return "AccessTokenResponse [sessionid=" + sessionid + ", tokenvalue=" + tokenvalue + ", user=" + user |
||||||
|
+ ", currentorguuid=" + currentorguuid + ", ssoSpare=" + ssoSpare + "]"; |
||||||
|
} |
||||||
|
|
||||||
|
|
||||||
|
} |
@ -0,0 +1,36 @@ |
|||||||
|
package com.bellmann.model.login; |
||||||
|
|
||||||
|
|
||||||
|
import com.fasterxml.jackson.annotation.JsonProperty; |
||||||
|
|
||||||
|
/** |
||||||
|
* @author xiangy |
||||||
|
*/ |
||||||
|
|
||||||
|
public class BsOrCsResponeResult { |
||||||
|
//token 的超时时间,默认 129590 描述
|
||||||
|
@JsonProperty("expires_in") |
||||||
|
private String expires_in ; |
||||||
|
//token 加密报文
|
||||||
|
@JsonProperty("access_token") |
||||||
|
private String access_token ; |
||||||
|
public String getExpires_in() { |
||||||
|
return expires_in; |
||||||
|
} |
||||||
|
public void setExpires_in(String expires_in) { |
||||||
|
this.expires_in = expires_in; |
||||||
|
} |
||||||
|
public String getAccess_token() { |
||||||
|
return access_token; |
||||||
|
} |
||||||
|
public void setAccess_token(String access_token) { |
||||||
|
this.access_token = access_token; |
||||||
|
} |
||||||
|
@Override |
||||||
|
public String toString() { |
||||||
|
return "BsOrCsResponeResult [expires_in=" + expires_in + ", access_token=" + access_token + "]"; |
||||||
|
} |
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
} |
@ -0,0 +1,156 @@ |
|||||||
|
package com.bellmann.model.login; |
||||||
|
|
||||||
|
|
||||||
|
import java.io.Serializable; |
||||||
|
|
||||||
|
|
||||||
|
public class LdapStaffVo implements Serializable { |
||||||
|
|
||||||
|
/** |
||||||
|
* |
||||||
|
*/ |
||||||
|
private static final long serialVersionUID = -8056124373704500401L; |
||||||
|
|
||||||
|
/** |
||||||
|
* 用户主账号. |
||||||
|
*/ |
||||||
|
|
||||||
|
private String staffAccount; |
||||||
|
|
||||||
|
/** |
||||||
|
* 姓名. |
||||||
|
*/ |
||||||
|
|
||||||
|
private String staffName; |
||||||
|
|
||||||
|
/** |
||||||
|
* 手机号码. |
||||||
|
*/ |
||||||
|
private String mobilePhone; |
||||||
|
|
||||||
|
/** |
||||||
|
* 身份证号. |
||||||
|
*/ |
||||||
|
private String certNumber; |
||||||
|
|
||||||
|
/** |
||||||
|
* 业务系统从账号:用户真实使用的账号 |
||||||
|
*/ |
||||||
|
private String relaStaffAccount; |
||||||
|
|
||||||
|
/** |
||||||
|
* 集团人力编码 |
||||||
|
*/ |
||||||
|
|
||||||
|
private String ctHrCode; |
||||||
|
|
||||||
|
/** |
||||||
|
* 集团邮箱 |
||||||
|
*/ |
||||||
|
|
||||||
|
private String ctMail; |
||||||
|
|
||||||
|
/** |
||||||
|
* 新增主数据中心组织的区域标识 |
||||||
|
*/ |
||||||
|
private String uooOrgAreaCode; |
||||||
|
|
||||||
|
/** |
||||||
|
* 新增主数据中心组织id标识 |
||||||
|
*/ |
||||||
|
private Long uooOrgId; |
||||||
|
|
||||||
|
public String getStaffAccount() { |
||||||
|
return staffAccount; |
||||||
|
} |
||||||
|
|
||||||
|
public void setStaffAccount(String staffAccount) { |
||||||
|
this.staffAccount = staffAccount; |
||||||
|
} |
||||||
|
|
||||||
|
public String getStaffName() { |
||||||
|
return staffName; |
||||||
|
} |
||||||
|
|
||||||
|
public void setStaffName(String staffName) { |
||||||
|
this.staffName = staffName; |
||||||
|
} |
||||||
|
|
||||||
|
public String getMobilePhone() { |
||||||
|
return mobilePhone; |
||||||
|
} |
||||||
|
|
||||||
|
public void setMobilePhone(String mobilePhone) { |
||||||
|
this.mobilePhone = mobilePhone; |
||||||
|
} |
||||||
|
|
||||||
|
public String getCertNumber() { |
||||||
|
return certNumber; |
||||||
|
} |
||||||
|
|
||||||
|
public void setCertNumber(String certNumber) { |
||||||
|
this.certNumber = certNumber; |
||||||
|
} |
||||||
|
|
||||||
|
public String getRelaStaffAccount() { |
||||||
|
return relaStaffAccount; |
||||||
|
} |
||||||
|
|
||||||
|
public void setRelaStaffAccount(String relaStaffAccount) { |
||||||
|
this.relaStaffAccount = relaStaffAccount; |
||||||
|
} |
||||||
|
|
||||||
|
public String getCtHrCode() { |
||||||
|
return ctHrCode; |
||||||
|
} |
||||||
|
|
||||||
|
public void setCtHrCode(String ctHrCode) { |
||||||
|
this.ctHrCode = ctHrCode; |
||||||
|
} |
||||||
|
|
||||||
|
public String getCtMail() { |
||||||
|
return ctMail; |
||||||
|
} |
||||||
|
|
||||||
|
public void setCtMail(String ctMail) { |
||||||
|
this.ctMail = ctMail; |
||||||
|
} |
||||||
|
|
||||||
|
public String getUooOrgAreaCode() { |
||||||
|
return uooOrgAreaCode; |
||||||
|
} |
||||||
|
|
||||||
|
public void setUooOrgAreaCode(String uooOrgAreaCode) { |
||||||
|
this.uooOrgAreaCode = uooOrgAreaCode; |
||||||
|
} |
||||||
|
|
||||||
|
public Long getUooOrgId() { |
||||||
|
return uooOrgId; |
||||||
|
} |
||||||
|
|
||||||
|
public void setUooOrgId(Long uooOrgId) { |
||||||
|
this.uooOrgId = uooOrgId; |
||||||
|
} |
||||||
|
|
||||||
|
public static long getSerialversionuid() { |
||||||
|
return serialVersionUID; |
||||||
|
} |
||||||
|
|
||||||
|
|
||||||
|
@Override |
||||||
|
public String toString() { |
||||||
|
return "Test{" + |
||||||
|
"staffAccount='" + staffAccount + '\'' + |
||||||
|
", staffName='" + staffName + '\'' + |
||||||
|
", mobilePhone='" + mobilePhone + '\'' + |
||||||
|
", certNumber='" + certNumber + '\'' + |
||||||
|
", relaStaffAccount='" + relaStaffAccount + '\'' + |
||||||
|
", ctHrCode='" + ctHrCode + '\'' + |
||||||
|
", ctMail='" + ctMail + '\'' + |
||||||
|
", uooOrgAreaCode='" + uooOrgAreaCode + '\'' + |
||||||
|
", uooOrgId=" + uooOrgId + |
||||||
|
'}'; |
||||||
|
} |
||||||
|
|
||||||
|
|
||||||
|
} |
@ -0,0 +1,39 @@ |
|||||||
|
package com.bellmann; |
||||||
|
|
||||||
|
|
||||||
|
import com.alibaba.fastjson.JSONObject; |
||||||
|
import com.bellmann.common.util.RSAUtils; |
||||||
|
import com.bellmann.config.Config4A; |
||||||
|
import com.bellmann.model.login.BsOrCsResponeResult; |
||||||
|
import lombok.extern.slf4j.Slf4j; |
||||||
|
import org.junit.jupiter.api.Test; |
||||||
|
import org.springframework.beans.factory.annotation.Autowired; |
||||||
|
import org.springframework.boot.test.context.SpringBootTest; |
||||||
|
|
||||||
|
@SpringBootTest |
||||||
|
@Slf4j |
||||||
|
public class DemoTest { |
||||||
|
|
||||||
|
|
||||||
|
@Autowired |
||||||
|
private Config4A config4A; |
||||||
|
|
||||||
|
@Test |
||||||
|
public void test1() { |
||||||
|
BsOrCsResponeResult bsOrCsResponeResult = new BsOrCsResponeResult(); |
||||||
|
|
||||||
|
String content = "{\"expires_in\":\"105819\",\"access_token\":\"20e2ffab563a5b6ef683820529270d7ab89893a1c40aa1531b4805bfad6f290976d941d9dd5fb1506ff07f406963a6609126358b47974efd6bfda9e97952a378f9313d5792bb0ecb1a7c6d6eb108d6dc64489f18e4c70d220a9d87daf3edfcd67e087d7b48136ced65d237e9b6468bce2f09d5eef9e09cbd1b8009bb25dd1b9504231cf8b5c03de351976fad120e6962644a8b83ce87c22de0adc851f44a444d35cb582fbcfed3ff9e97866070f51f94ac921784c4f7c0c56f92cdb1eca78c1f04bc7a27e524b33a3f70dcf14844a57d3004cab133edf4fa753cb7e5746277057550351bef7c8c48aba6f527768d5e160f0a0398c89b67a740ddf71d6c4e3a9228e999035fb8063a31429cb4d637783e010f4378611f8961df7ec1dd403f3e28cd79a27988bd6b474817bdbe528fe4467e2b95acb64177d20b12a152cbac2751571a82a97e0357d28e31a88d59b6e1232963566f51b469e814dc836b4c500f04e3838c7a8ba82dcc382a36bd2ee266b4b1b0c38940fe328070d3b6b789f8f2ae3aae0d1560213318084b03aa63c193c1d11f6bad6f56233a86c055bd23cf300d2806f48ad39c690359a192a72da63a31b9291c20f74019cb192326d4209580597f69d13aede9989eac01225ac016f923d5a7d5dde883d035b58662ecbb91af23972cd3637d3248595f82354661b10065239892f1f050f1ce7ad2c0d91eb8af70116d51d23a1bc90803c564896c6377f40b712d4e267020389fc315e37dc5b48c02d113aefba9f669fc1e139a3924e99c070c96114de543c01faee3633db5857e9202bb514fffca4ae2ba69538dc600cf119b6bd819cf5a4923c3d0937789018ec30aa6b359ef1b5fa234d894f2298b6e4537333fb3c37153707285b02ecde7d545cae9eef71097d02a3506c4087a7d1db565215d37ace9bb63598476d3609cf2b4512e5cd4e30e49e6b746f974525890863a5a7243e185da1900464a66914542a1725f03dc6aef689d3f6515c2d34f709b5d9b2c21ab86fdeb7eadf9f47b21777a6761848114c792891593dd14e425cc9a6ecd0c4e705f3398681a2f418c2512032bfe77f86b36b2bf4e2141fb40f714ad228a616913a494c81869401fa810c70957a9dfb0e876c123ddee2bd156d290649176510bf40c27f819bc35300711febc502cf957007c4a811b19af0b3e25e66ea6d0509f1c732eb3870a86c2084a6e69dc784741403ae878889e0203bdfb97d0965e73b6e4cd2fcecb5a9a2f02dfb071992a9886060c1f850ef845479ea2eed62918012e5eb4efced2e592a9b678ec262a20ed08aadebd3c7663448967d6d52fe8d6a305c2af772b7fe42aca0bc8de4749370771162c88d6dcc2268eb8cf2a75a9db90006b2fc5b2eaf0a5a876ddf48b208ca91d008a687f1b4065320f24091791951526a1232969d8080226966f2265aacef937d64fdc21fcb26e703d921f402efb8525af798529cd1e4080210e36a62ccce7527cce9fa62eee0d24ffd4c3dc8fe9ef63084540cd5b386e2339a47e453c4f74df88a4224bfc3b11f2851caf5f1e1f40d3e37ec9b62cc2466c52bf961f4eea7aae0926ba02275a02e964a269da47268c8c2654e0b1bdeae1817712bf\"}"; |
||||||
|
|
||||||
|
bsOrCsResponeResult = JSONObject.parseObject(content, BsOrCsResponeResult.class); |
||||||
|
|
||||||
|
String token = bsOrCsResponeResult.getAccess_token(); |
||||||
|
|
||||||
|
// String config4A = "MIICdgIBADANBgkqhkiG9w0BAQEFAASCAmAwggJcAgEAAoGBAJESA0kRw+/cIwUNGp9iJS/dZAg3fEy3izQfN0gAE8EYWhCMwPt8EH1UWNuK3+ka0qUbbwzwXOAR2e54p4zK06GqI8Vym83XOQ7G2kfT1Q657VDMnMym9eYdRf+i5qndb/7vjsV5qVuijxmwvtc0ITmciIHZSIS9Wt6W0/AJ2iBhAgMBAAECgYAA9pLHQssPXgGtBEjhl0YaLuD9xXmJrFvMHytvqtF2wh3B959F5C1bWaqj3YSOUIdmwXt/pV5mffPZl5s8LfIRAYb6PChTMElll5AL0ETs6MSusxO9+PhPzkpFmUcNr4WmtbNIhbHB8GIJAGPDwH/jdt8AGgBXEQ8ORuv+owFd2QJBAMzXHVuIF0KyHBpyaFLolkJR2N4WC5BiOXpav+dkdXTzJqIoYp4ZJ8XrIUOHz305d7uivwOMLMT/E0OZZCU0Q5kCQQC1TWKDcVlpA3PWUSXYTFaGHVzc1kwtuPHtOWqBP02Wsc5QfigJgnH+7SOj2OLvYGM5Y65jofm/qL4G8+ldecAJAkBZeo5kcWpXxB3qn5dawCcDCWXGfbLHe2DIReQWXBfC4oQ5yZJqpWpuVmvNaAIHHZdj64+tKQpVc2ci3FqWteHhAkAKe1/fs5T0wlNXJ7flanxyIo504hrjZdbu3SvwLwXAWaVQ+Ao+ioEmuPUfivGL32m9LqstbXbwAsSByGZMljZpAkEAlBOzSpUJdRWsIIrDA9W5jBFXpMS46fYqWR2gVvglTmN1PSZqU694tOf3uwSaqGchHibMqen5z+9HU4XR5vZj9A==";
|
||||||
|
try { |
||||||
|
token = RSAUtils.decryptByPrivateKeyStr(token, config4A.getSecurity()); |
||||||
|
}catch (Exception e) { |
||||||
|
log.info("decry pass error : {}", token); |
||||||
|
} |
||||||
|
log.info("token success : {}", token); |
||||||
|
} |
||||||
|
} |
Loading…
Reference in new issue