Skip to content

Instantly share code, notes, and snippets.

@JunqiangYang
Last active July 10, 2017 08:24
Show Gist options
  • Save JunqiangYang/8df918b0639ccd79f69902a2d87d8283 to your computer and use it in GitHub Desktop.
Save JunqiangYang/8df918b0639ccd79f69902a2d87d8283 to your computer and use it in GitHub Desktop.
import java.io.IOException;
import java.security.SecureRandom;
import javax.crypto.Cipher;
import javax.crypto.SecretKey;
import javax.crypto.SecretKeyFactory;
import javax.crypto.spec.DESKeySpec;
public class DESUtil {
/**
* 根据键值进行加密
*/
public static String encrypt(String data, String key) throws Exception {
byte[] bt = encrypt(data.getBytes(), key.getBytes());
String strs = HexUtil.bytes2HexStr( bt );
return strs;
}
/**
* 根据键值进行解密
*/
public static String decrypt(String data, String key) throws IOException, Exception {
if (data == null)
{
return null;
}
byte[] buf = HexUtil.hexStr2Bytes( data );
byte[] bt = decrypt(buf, key.getBytes());
return new String(bt);
}
/**
* 根据键值进行加密
*/
private static byte[] encrypt(byte[] data, byte[] key) throws Exception {
Cipher cipher = cipherInit(data, key, Cipher.ENCRYPT_MODE);
return cipher.doFinal(data);
}
/**
* 根据键值进行解密
*/
private static byte[] decrypt(byte[] data, byte[] key) throws Exception {
Cipher cipher = cipherInit(data, key, Cipher.DECRYPT_MODE);
return cipher.doFinal(data);
}
private static Cipher cipherInit(byte[] data, byte[] key,int cipherValue) throws Exception {
/** 生成一个可信任的随机数源**/
SecureRandom sr = new SecureRandom();
/** 从原始密钥数据创建DESKeySpec对象**/
DESKeySpec dks = new DESKeySpec(key);
/** 创建一个密钥工厂,然后用它把DESKeySpec转换成SecretKey对象**/
SecretKeyFactory keyFactory = SecretKeyFactory.getInstance("DES");
SecretKey securekey = keyFactory.generateSecret(dks);
/** Cipher对象实际完成加密或解密操作**/
Cipher cipher = Cipher.getInstance("DES");
/**用密钥初始化Cipher对象**/
cipher.init(cipherValue, securekey, sr);
return cipher;
}
/*
* 加密
*/
static final String key = "nykjxnlc";
public static String encryptHex( String data ){
String outstr ="";
try {
outstr = encrypt(data, key);
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return outstr;
}
/*
* 解密
*/
public static String decryptHex( String data ){
String outstr ="";
try {
outstr = decrypt( data, key);
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return outstr;
}
public static void main(String[] args) throws Exception {
String data="6107470767063040|13521746902|145613450355";
String str = encryptHex( data );
System.out.println( data +" : "+ str );
String dstr = decryptHex( str );
System.out.println( str +" : "+dstr );
/*
String data = "13264034035";
String key = "38156403";
System.err.println(encrypt(data, key));
System.err.println( decrypt(encrypt(data, key), key) );
System.out.println( HexUtil.bytes2HexStr(encrypt(data, key).getBytes()) );
String str=new String ( HexUtil.hexStr2Bytes( HexUtil.bytes2HexStr(encrypt(data, key).getBytes()) ));
System.out.println( str );
System.out.println( decrypt( str, key) );
*/
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment