package com.guowei.des;
import java.security.InvalidKeyException;
import java.security.Key;
import java.security.NoSuchAlgorithmException;
import javax.crypto.BadPaddingException;
import javax.crypto.Cipher;
import javax.crypto.IllegalBlockSizeException;
import javax.crypto.NoSuchPaddingException;
import javax.crypto.spec.SecretKeySpec;
public class Test {
/**
* 自定义一个key
*/
public Key getKey(byte[] keyByte) {
Key key = null;
// 创建一个空的八位数组,默认情况下为0
byte[] byteTemp = new byte[8];
// 将用户指定的规则转换成八位数组
for (int i = 0; i < byteTemp.length && i < keyByte.length; i++) {
byteTemp[i] = keyByte[i];
}
key = new SecretKeySpec(byteTemp, "DES");
return key;
}
/**
* 第二种产生key的方法
* @return
*/
public Key getKey2(){
Key key = null;
//创建一个可信任的随机数源,DES算法需要
SecureRandom sr = new SecureRandom();
try {
//用DES算法创建一个KeyGenerator对象
KeyGenerator kg = KeyGenerator.getInstance("DES");
// 初始化此密钥生成器,使其具有确定的密钥长度
kg.init(sr);
//生成密匙
key = kg.generateKey();
} catch (NoSuchAlgorithmException e) {
e.printStackTrace();
}
return key;
}
/**
* @param key
* 加密使用的密钥!
* @return 生成密文的字符串表示形式
*/
public String getEncriptCode(Key key, String srcCode) {
StringBuffer sb = null;
try {
/*
* Cipher类无构造方法,调用getInstance()方法将所请求转换的名称传递给它 参数为 转换的名称,例如
* DES/CBC/PKCS5Padding,这里我们使用DES转换。
*/
Cipher encriptCipher = Cipher.getInstance("DES");
// 用密钥初始化此 Cipher
encriptCipher.init(Cipher.ENCRYPT_MODE, key);
// 按单部分操作加密数据
byte[] desCode = encriptCipher.doFinal(srcCode.getBytes());
// 将加密后的数据转换成16进制的字符串返回
sb = new StringBuffer(desCode.length * 2);
for (int i = 0; i < desCode.length; i++) {
int temp = desCode[i];
// 把负数转换为正数
if (temp < 0) {
temp = temp + 256;// byte的最小值为-256,最大值为255
}
// 小于 0F 的数需要在前面补0
if (temp < 16) {
sb.append("0");
}
sb.append(Integer.toString(temp, 16));
}
} catch (NoSuchAlgorithmException e) {
e.printStackTrace();
} catch (NoSuchPaddingException e) {
e.printStackTrace();
} catch (InvalidKeyException e) {
e.printStackTrace();
} catch (IllegalBlockSizeException e) {
e.printStackTrace();
} catch (BadPaddingException e) {
e.printStackTrace();
}
return sb.toString();
}
//解密算法
public String getDecriptCode(String encriptCode, Key key) {
Cipher decriptCipher = null;
String decriptString = null;
byte[] encriptByte = encriptCode.getBytes();
byte[] decriptByte = new byte[encriptByte.length / 2];
for (int i = 0; i < encriptByte.length; i+=2) {
String strTmp = new String(encriptByte, i, 2);
decriptByte[i / 2] = (byte) Integer.parseInt(strTmp, 16);
}
try {
decriptCipher = Cipher.getInstance("DES");
decriptCipher.init(Cipher.DECRYPT_MODE, key);
byte[] outByte = decriptCipher.doFinal(decriptByte);
decriptString = new String(outByte);
} catch (NoSuchAlgorithmException e) {
e.printStackTrace();
} catch (NoSuchPaddingException e) {
e.printStackTrace();
} catch (InvalidKeyException e) {
e.printStackTrace();
} catch (IllegalBlockSizeException e) {
e.printStackTrace();
} catch (BadPaddingException e) {
e.printStackTrace();
}
return decriptString;
}
public static void main(String[] args) {
// TODO Auto-generated method stub
Test2 test = new Test2();
Key key = null;
// 要加密的原文
String src = "encript des coding~";
// 自己指定的密钥
String keyRule = "guoweiadsfjkieopaiwerklajsdf";
key = test.getKey(keyRule.getBytes());
String encriptCode = test.getEncriptCode(key, src);
System.out.println("原文:"+src);
System.out.println("密文是:"+encriptCode);
System.out.println("解密后:"+test.getDecriptCode(test.getEncriptCode(key, src), key));
}
}
执行后的效果如下:
