124 lines
3.5 KiB
Plaintext
124 lines
3.5 KiB
Plaintext
package com.nationalelectric.greenH5.utils;
|
|
|
|
import java.nio.charset.Charset;
|
|
|
|
import javax.crypto.Cipher;
|
|
import javax.crypto.SecretKey;
|
|
import javax.crypto.SecretKeyFactory;
|
|
import javax.crypto.spec.DESKeySpec;
|
|
import javax.crypto.spec.IvParameterSpec;
|
|
|
|
import Decoder.BASE64Decoder;
|
|
import Decoder.BASE64Encoder;
|
|
|
|
public class DesUtil {
|
|
|
|
private static Charset CS = Charset.forName("utf8");
|
|
|
|
public static String encrypt(String srcStr, String sKey, String sIV) throws Exception {
|
|
return encrypt(srcStr, CS, sKey, sIV);
|
|
}
|
|
|
|
public static String decrypt(String base64Str, String sKey, String sIV) throws Exception {
|
|
return decrypt(base64Str, CS, sKey, sIV);
|
|
}
|
|
|
|
/**
|
|
* 对给定的字符串以指定的编码方式和密钥进行加密
|
|
*
|
|
* @param srcStr
|
|
* 待加密的字符串
|
|
* @param sKey
|
|
* 密钥
|
|
*/
|
|
public static String encrypt(String srcStr, Charset charset, String sKey, String sIV) throws Exception {
|
|
byte[] src = srcStr.getBytes();
|
|
byte[] buf = encrypt(src, sKey, sIV);
|
|
BASE64Encoder encoder = new BASE64Encoder();
|
|
return encoder.encode(buf);
|
|
}
|
|
|
|
/**
|
|
* 对给定的密文以指定的编码方式和密钥进行解密
|
|
*
|
|
* @param hexStr
|
|
* 需要解密的密文
|
|
* @param charset
|
|
* 字符集
|
|
* @param sKey
|
|
* 密钥
|
|
* @return 解密后的原文
|
|
* @throws Exception
|
|
*/
|
|
public static String decrypt(String base64Str, Charset charset, String sKey, String sIV) throws Exception {
|
|
BASE64Decoder decoder = new BASE64Decoder();
|
|
byte[] src = decoder.decodeBuffer(base64Str);
|
|
byte[] buf = decrypt(src, sKey, sIV);
|
|
return new String(buf, charset);
|
|
|
|
}
|
|
|
|
public static byte[] encrypt(byte[] data, String sKey, String sIV) {
|
|
try {
|
|
byte[] key = sKey.getBytes();
|
|
byte[] bIV = sIV.getBytes();
|
|
|
|
IvParameterSpec iv = new IvParameterSpec(bIV);
|
|
DESKeySpec desKey = new DESKeySpec(key);
|
|
|
|
SecretKeyFactory keyFactory = SecretKeyFactory.getInstance("DES");
|
|
SecretKey securekey = keyFactory.generateSecret(desKey);
|
|
|
|
Cipher cipher = Cipher.getInstance("DES/CBC/PKCS5Padding");
|
|
|
|
cipher.init(Cipher.ENCRYPT_MODE, securekey, iv);
|
|
|
|
return cipher.doFinal(data);
|
|
} catch (Throwable e) {
|
|
e.printStackTrace();
|
|
}
|
|
return null;
|
|
}
|
|
|
|
/**
|
|
* 解密
|
|
*
|
|
* @param src
|
|
* @param sKey
|
|
* @return
|
|
* @throws Exception
|
|
*/
|
|
public static byte[] decrypt(byte[] src, String sKey, String sIV) throws Exception {
|
|
byte[] key = sKey.getBytes();
|
|
byte[] bIV = sIV.getBytes();
|
|
// 初始化向量
|
|
IvParameterSpec iv = new IvParameterSpec(bIV);
|
|
// 创建一个DESKeySpec对象
|
|
DESKeySpec desKey = new DESKeySpec(key);
|
|
// 创建一个密匙工厂
|
|
SecretKeyFactory keyFactory = SecretKeyFactory.getInstance("DES");
|
|
// 将DESKeySpec对象转换成SecretKey对象
|
|
SecretKey securekey = keyFactory.generateSecret(desKey);
|
|
// Cipher对象实际完成解密操作
|
|
Cipher cipher = Cipher.getInstance("DES/CBC/PKCS5Padding");
|
|
// 用密匙初始化Cipher对象
|
|
cipher.init(Cipher.DECRYPT_MODE, securekey, iv);
|
|
// 真正开始解密操作
|
|
return cipher.doFinal(src);
|
|
}
|
|
|
|
public static void main(String[] args) throws Exception {
|
|
String data = "0004774159";
|
|
String skey = "YWtoaWw6";
|
|
String iv = "L+toj8%]";
|
|
Charset cs = Charset.forName("utf8");
|
|
String ss = encrypt(data, cs, skey, iv);
|
|
System.out.println("===明文:" + data);
|
|
System.out.println("===密钥:" + skey);
|
|
System.out.println("===向量:" + iv);
|
|
System.out.println("===密文:" + ss);
|
|
String kk = decrypt(ss, cs, skey, iv);
|
|
System.out.println("===解密:" + kk);
|
|
}
|
|
}
|