parent
d01fed211d
commit
dfb7392515
@ -1,173 +0,0 @@ |
||||
package com.hai.common.security; |
||||
|
||||
import java.io.*; |
||||
import java.security.Key; |
||||
import java.security.SecureRandom; |
||||
import java.security.Security; |
||||
|
||||
import javax.crypto.*; |
||||
import javax.crypto.spec.DESKeySpec; |
||||
import javax.crypto.spec.IvParameterSpec; |
||||
|
||||
import java.util.Base64; |
||||
import org.bouncycastle.jce.provider.BouncyCastleProvider; |
||||
public class DESUtil { |
||||
|
||||
/** |
||||
* 偏移变量,固定占8位字节 |
||||
*/ |
||||
private final static String IV_PARAMETER = "12345678"; |
||||
/** |
||||
* 密钥算法 |
||||
*/ |
||||
private static final String ALGORITHM = "DES"; |
||||
/** |
||||
* 加密/解密算法-工作模式-填充模式 |
||||
*/ |
||||
private static final String CIPHER_ALGORITHM = "DES/CBC/PKCS5Padding"; |
||||
/** |
||||
* 默认编码 |
||||
*/ |
||||
private static final String CHARSET = "utf-8"; |
||||
|
||||
/** |
||||
* 生成key |
||||
* |
||||
* @param password |
||||
* @return |
||||
* @throws Exception |
||||
*/ |
||||
private static Key generateKey(String password) throws Exception { |
||||
DESKeySpec dks = new DESKeySpec(password.getBytes(CHARSET)); |
||||
SecretKeyFactory keyFactory = SecretKeyFactory.getInstance(ALGORITHM); |
||||
return keyFactory.generateSecret(dks); |
||||
} |
||||
|
||||
|
||||
/** |
||||
* DES加密字符串 |
||||
* |
||||
* @param password 加密密码,长度不能够小于8位 |
||||
* @param data 待加密字符串 |
||||
* @return 加密后内容 |
||||
*/ |
||||
public static String encrypt(String password, String data) { |
||||
if (password== null || password.length() < 8) { |
||||
throw new RuntimeException("加密失败,key不能小于8位"); |
||||
} |
||||
if (data == null) |
||||
return null; |
||||
try { |
||||
Key secretKey = generateKey(password); |
||||
Cipher cipher = Cipher.getInstance(CIPHER_ALGORITHM); |
||||
IvParameterSpec iv = new IvParameterSpec(IV_PARAMETER.getBytes(CHARSET)); |
||||
cipher.init(Cipher.ENCRYPT_MODE, secretKey, iv); |
||||
byte[] bytes = cipher.doFinal(data.getBytes(CHARSET)); |
||||
|
||||
//JDK1.8及以上可直接使用Base64,JDK1.7及以下可以使用BASE64Encoder
|
||||
//Android平台可以使用android.util.Base64
|
||||
return new String(Base64.getEncoder().encode(bytes)); |
||||
|
||||
} catch (Exception e) { |
||||
e.printStackTrace(); |
||||
return data; |
||||
} |
||||
} |
||||
|
||||
/** |
||||
* DES解密字符串 |
||||
* |
||||
* @param password 解密密码,长度不能够小于8位 |
||||
* @param data 待解密字符串 |
||||
* @return 解密后内容 |
||||
*/ |
||||
public static String decrypt(String password, String data) { |
||||
if (password== null || password.length() < 8) { |
||||
throw new RuntimeException("加密失败,key不能小于8位"); |
||||
} |
||||
if (data == null) |
||||
return null; |
||||
try { |
||||
Key secretKey = generateKey(password); |
||||
Cipher cipher = Cipher.getInstance(CIPHER_ALGORITHM); |
||||
IvParameterSpec iv = new IvParameterSpec(IV_PARAMETER.getBytes(CHARSET)); |
||||
cipher.init(Cipher.DECRYPT_MODE, secretKey, iv); |
||||
return new String(cipher.doFinal(Base64.getDecoder().decode(data.getBytes(CHARSET))), CHARSET); |
||||
} catch (Exception e) { |
||||
e.printStackTrace(); |
||||
return data; |
||||
} |
||||
} |
||||
|
||||
/** |
||||
* DES加密文件 |
||||
* |
||||
* @param srcFile 待加密的文件 |
||||
* @param destFile 加密后存放的文件路径 |
||||
* @return 加密后的文件路径 |
||||
*/ |
||||
/* public static String encryptFile(String password, String srcFile, String destFile) { |
||||
|
||||
if (password== null || password.length() < 8) { |
||||
throw new RuntimeException("加密失败,key不能小于8位"); |
||||
} |
||||
try { |
||||
IvParameterSpec iv = new IvParameterSpec(IV_PARAMETER.getBytes(CHARSET)); |
||||
Cipher cipher = Cipher.getInstance(CIPHER_ALGORITHM); |
||||
cipher.init(Cipher.ENCRYPT_MODE, generateKey(key), iv); |
||||
InputStream is = new FileInputStream(srcFile); |
||||
OutputStream out = new FileOutputStream(destFile); |
||||
CipherInputStream cis = new CipherInputStream(is, cipher); |
||||
byte[] buffer = new byte[1024]; |
||||
int r; |
||||
while ((r = cis.read(buffer)) > 0) { |
||||
out.write(buffer, 0, r); |
||||
} |
||||
cis.close(); |
||||
is.close(); |
||||
out.close(); |
||||
return destFile; |
||||
} catch (Exception ex) { |
||||
ex.printStackTrace(); |
||||
} |
||||
return null; |
||||
}*/ |
||||
|
||||
/** |
||||
* DES解密文件 |
||||
* |
||||
* @param srcFile 已加密的文件 |
||||
* @param destFile 解密后存放的文件路径 |
||||
* @return 解密后的文件路径 |
||||
*/ |
||||
/* public static String decryptFile(String password, String srcFile, String destFile) { |
||||
if (password== null || password.length() < 8) { |
||||
throw new RuntimeException("加密失败,key不能小于8位"); |
||||
} |
||||
try { |
||||
File file = new File(destFile); |
||||
if (!file.exists()) { |
||||
file.getParentFile().mkdirs(); |
||||
file.createNewFile(); |
||||
} |
||||
IvParameterSpec iv = new IvParameterSpec(IV_PARAMETER.getBytes(CHARSET)); |
||||
Cipher cipher = Cipher.getInstance(CIPHER_ALGORITHM); |
||||
cipher.init(Cipher.DECRYPT_MODE, generateKey(key), iv); |
||||
InputStream is = new FileInputStream(srcFile); |
||||
OutputStream out = new FileOutputStream(destFile); |
||||
CipherOutputStream cos = new CipherOutputStream(out, cipher); |
||||
byte[] buffer = new byte[1024]; |
||||
int r; |
||||
while ((r = is.read(buffer)) >= 0) { |
||||
cos.write(buffer, 0, r); |
||||
} |
||||
cos.close(); |
||||
is.close(); |
||||
out.close(); |
||||
return destFile; |
||||
} catch (Exception ex) { |
||||
ex.printStackTrace(); |
||||
} |
||||
return null; |
||||
}*/ |
||||
} |
@ -0,0 +1,277 @@ |
||||
package com.hai.common.security; |
||||
|
||||
import java.nio.charset.Charset; |
||||
import java.security.Key; |
||||
import java.security.SecureRandom; |
||||
import java.security.spec.AlgorithmParameterSpec; |
||||
import java.util.regex.Matcher; |
||||
import java.util.regex.Pattern; |
||||
|
||||
import javax.crypto.Cipher; |
||||
import javax.crypto.SecretKey; |
||||
import javax.crypto.SecretKeyFactory; |
||||
import javax.crypto.spec.DESKeySpec; |
||||
import javax.crypto.spec.DESedeKeySpec; |
||||
import javax.crypto.spec.IvParameterSpec; |
||||
|
||||
import org.apache.commons.codec.binary.Base64; |
||||
//import org.apache.commons.lang3.RandomStringUtils;
|
||||
|
||||
|
||||
import com.thoughtworks.xstream.core.util.Base64Encoder; |
||||
|
||||
public class DesUtil { |
||||
|
||||
public static final String ALGORITHM_DES = "DES/CBC/PKCS5Padding"; |
||||
|
||||
/** |
||||
* DES算法,加密 |
||||
* |
||||
* @param data |
||||
* 待加密字符串 |
||||
* @param key |
||||
* 加密私钥,长度不能够小于8位 |
||||
* @return 加密后的字节数组,一般结合Base64编码使用 |
||||
* @throws CryptException |
||||
* 异常 |
||||
*/ |
||||
public static byte[] encode(String key, String data) throws Exception { |
||||
return encode(key, data.getBytes()); |
||||
} |
||||
|
||||
/** |
||||
* DES算法,加密 |
||||
* |
||||
* @param data |
||||
* 待加密字符串 |
||||
* @param key |
||||
* 加密私钥,长度不能够小于8位 |
||||
* @return 加密后的字节数组,一般结合Base64编码使用 |
||||
* @throws CryptException |
||||
* 异常 |
||||
*/ |
||||
public static byte[] encode(String key, byte[] data) throws Exception { |
||||
try { |
||||
DESKeySpec dks = new DESKeySpec(key.getBytes()); |
||||
|
||||
SecretKeyFactory keyFactory = SecretKeyFactory.getInstance("DES"); |
||||
// key的长度不能够小于8位字节
|
||||
Key secretKey = keyFactory.generateSecret(dks); |
||||
Cipher cipher = Cipher.getInstance(ALGORITHM_DES); |
||||
IvParameterSpec iv = new IvParameterSpec(key.getBytes()); |
||||
AlgorithmParameterSpec paramSpec = iv; |
||||
cipher.init(Cipher.ENCRYPT_MODE, secretKey, paramSpec); |
||||
|
||||
byte[] bytes = cipher.doFinal(data); |
||||
return bytes; |
||||
// return byte2HexStr(bytes);
|
||||
// return byte2hex(new String(bytes));
|
||||
// return new String(new BASE64Encoder().encode(bytes));
|
||||
// return new String(bytes);
|
||||
} catch (Exception e) { |
||||
throw new Exception(e); |
||||
} |
||||
} |
||||
|
||||
/** |
||||
* DES算法,解密 |
||||
* |
||||
* @param data |
||||
* 待解密字符串 |
||||
* @param key |
||||
* 解密私钥,长度不能够小于8位 |
||||
* @return 解密后的字节数组 |
||||
* @throws Exception |
||||
* 异常 |
||||
*/ |
||||
public static byte[] decode(String key, byte[] data) throws Exception { |
||||
try { |
||||
// SecureRandom sr = new SecureRandom();
|
||||
DESKeySpec dks = new DESKeySpec(key.getBytes()); |
||||
SecretKeyFactory keyFactory = SecretKeyFactory.getInstance("DES"); |
||||
// key的长度不能够小于8位字节
|
||||
Key secretKey = keyFactory.generateSecret(dks); |
||||
Cipher cipher = Cipher.getInstance(ALGORITHM_DES); |
||||
IvParameterSpec iv = new IvParameterSpec(key.getBytes()); |
||||
AlgorithmParameterSpec paramSpec = iv; |
||||
cipher.init(Cipher.DECRYPT_MODE, secretKey, paramSpec); |
||||
return cipher.doFinal(data); |
||||
} catch (Exception e) { |
||||
throw new Exception(e); |
||||
} |
||||
} |
||||
|
||||
/** |
||||
* 获取编码后的值 |
||||
* |
||||
* @param key |
||||
* @param data |
||||
* @return |
||||
* @throws Exception |
||||
*/ |
||||
public static String decode(String key, String data) { |
||||
byte[] datas; |
||||
String value = null; |
||||
try { |
||||
|
||||
datas = decode(key, new Base64Encoder().decode(data)); |
||||
|
||||
value = new String(datas); |
||||
} catch (Exception e) { |
||||
value = ""; |
||||
} |
||||
return value; |
||||
} |
||||
|
||||
public static String byte2HexStr(byte[] b) { |
||||
String hs = ""; |
||||
String stmp = ""; |
||||
for (int n = 0; n < b.length; n++) { |
||||
stmp = (Integer.toHexString(b[n] & 0XFF)); |
||||
if (stmp.length() == 1) |
||||
hs = hs + "0" + stmp; |
||||
else |
||||
hs = hs + stmp; |
||||
// if (n<b.length-1) hs=hs+":";
|
||||
} |
||||
return hs; |
||||
} |
||||
|
||||
public static String triDesEncrypt(String input, String desKey, String desIv) { |
||||
Cipher cipher = null; |
||||
try { |
||||
SecureRandom sr = new SecureRandom(); |
||||
DESedeKeySpec dks = new DESedeKeySpec(desKey.getBytes("UTF-8")); |
||||
SecretKeyFactory keyFactory = SecretKeyFactory |
||||
.getInstance("DESede"); |
||||
Key key = keyFactory.generateSecret(dks); |
||||
IvParameterSpec iv = new IvParameterSpec(desIv.getBytes("UTF-8")); |
||||
cipher = Cipher.getInstance("DESede/CBC/PKCS5Padding"); |
||||
cipher.init(1, key, iv, sr); |
||||
byte[] array = cipher.doFinal(input.getBytes("UTF-8")); |
||||
|
||||
return Base64.encodeBase64String(array); |
||||
} catch (Exception e) { |
||||
e.printStackTrace(); |
||||
} |
||||
return null; |
||||
} |
||||
|
||||
public static String triDesDecrypt(String input, String desKey, String desIv) { |
||||
Cipher cipher = null; |
||||
try { |
||||
SecureRandom sr = new SecureRandom(); |
||||
DESedeKeySpec dks = new DESedeKeySpec(desKey.getBytes("UTF-8")); |
||||
SecretKeyFactory keyFactory = SecretKeyFactory |
||||
.getInstance("DESede"); |
||||
SecretKey key = keyFactory.generateSecret(dks); |
||||
IvParameterSpec iv = new IvParameterSpec(desIv.getBytes("UTF-8")); |
||||
cipher = Cipher.getInstance("DESede/CBC/PKCS5Padding"); |
||||
cipher.init(2, key, iv, sr); |
||||
byte[] decoded = Base64.decodeBase64(input); |
||||
byte[] array = cipher.doFinal(decoded); |
||||
return new String(array, "UTF-8"); |
||||
} catch (Exception e) { |
||||
e.printStackTrace(); |
||||
} |
||||
return null; |
||||
} |
||||
|
||||
public static byte[] encode(String key, String data, Charset charset) |
||||
throws Exception { |
||||
return encode(key, data.getBytes(charset)); |
||||
} |
||||
|
||||
public static String decode(String key, String data, Charset charset) { |
||||
byte[] datas; |
||||
String value = null; |
||||
try { |
||||
|
||||
datas = decode(key, new Base64Encoder().decode(data)); |
||||
|
||||
value = new String(datas, charset); |
||||
} catch (Exception e) { |
||||
value = ""; |
||||
} |
||||
return value; |
||||
} |
||||
|
||||
/** |
||||
* des加密,返回string类型,指定编码集 |
||||
* |
||||
* @time 2018-11-6 16:08:27 |
||||
* @author Lxm |
||||
* @param data |
||||
* @param charset |
||||
* @param key |
||||
* @return |
||||
* @throws Exception |
||||
*/ |
||||
public static String encode(String data, Charset charset, String key) |
||||
throws Exception { |
||||
|
||||
byte[] bt = encode(key, data.getBytes(charset)); |
||||
|
||||
// return new String(bt, charset);
|
||||
String rs = new Base64Encoder().encode(bt); |
||||
return rs; |
||||
} |
||||
|
||||
/* public static String quickDecode(String key, String data) throws Exception { |
||||
return quickDecode(key, data, "UTF-8"); |
||||
} |
||||
|
||||
public static String quickDecode(String key, String data, String charSet) |
||||
throws Exception { |
||||
return new String(DesUtil.decode(key, HexUtil.hexStr2Bytes(data)), |
||||
charSet); |
||||
}*/ |
||||
|
||||
|
||||
public static void main(String[] args) throws Exception { |
||||
// String ke = RandomStringUtils
|
||||
// .random(8,
|
||||
// "qwertyuiopasdfghjklzxcvbnmQWERTYUIOPASDFGHJKLZXCVBNM1234567890!@#$%^&*");
|
||||
// System.out.println(ke);
|
||||
// String key = "abcdefgh";
|
||||
// String data =
|
||||
// "<?xml version=\"1.0\"?><RequestData><cardNo>8800030132003900701</cardNo><jourId>1231204573498573945</jourId><mercCode>000001</mercCode><terminalId>8800030165000100040</terminalId><terminalTime>2016-07-28 13:20:50</terminalTime><tranAmt>150</tranAmt></RequestData>";
|
||||
// String s = HexUtil.byte2HexStr(DesUtil.encode(key, data));
|
||||
// System.err.println(s);
|
||||
// System.err.println(DesUtil.encode(key, data.getBytes()).length);
|
||||
// String s2 = new String(DesUtil.decode(key, HexUtil.hexStr2Bytes(s)));
|
||||
// System.err.println(s2);
|
||||
|
||||
// String s = encode("123", Charset.forName("GBK"), "d0fb65e5");
|
||||
// System.out.println(s);
|
||||
//
|
||||
// String e = decode("d0fb65e5", "258946d6143e30f9",
|
||||
// Charset.forName("UTF-8"));
|
||||
// System.out.println(e);
|
||||
|
||||
/*String s = encode("中国", Charset.forName("UTF-8"),"12345678"); |
||||
System.out.println(s);*/ |
||||
String data = "T3xbPEKEXV9+CbBw8D1B+N2jk8xwa55s0Bde48c49YDwYfnUdBVz6Kj4HS2oCA1TTiqJkCUIYa5ckMhJeByBCAMsqu21LmFjb/hdW0y1Tt0Wk5PqmO8FAg=="; |
||||
/* { |
||||
"success": true, |
||||
"message": "ok", |
||||
"cards": [{ |
||||
"cardNo": "8800030115015107746" |
||||
}, { |
||||
"cardNo": "8800030115015119428" |
||||
}, { |
||||
"cardNo": "8800030128003170055" |
||||
}, { |
||||
"cardNo": "8800030132003656709" |
||||
}, { |
||||
"cardNo": "8800030132004014510" |
||||
}, { |
||||
"cardNo": "8800031104000000248" |
||||
}] |
||||
}*/ |
||||
|
||||
String a = decode("F8E91A3C", data,Charset.forName("UTF-8")); |
||||
System.out.println(a); |
||||
System.out.println("完成"); |
||||
} |
||||
} |
Loading…
Reference in new issue