备忘DES带向量的加密和解密与DES简单加密与解密

时间:2022-12-13 15:05:26

package com.ego.util;

import java.security.Key;

import java.security.SecureRandom;

import java.security.spec.AlgorithmParameterSpec;

import javax.crypto.Cipher;

import javax.crypto.SecretKey;

import javax.crypto.SecretKeyFactory;

import javax.crypto.spec.DESKeySpec;

import javax.crypto.spec.IvParameterSpec;

public class DESCryption {

private static AlgorithmParameterSpec iv = null;// 加密算法的参数接口,IvParameterSpec是它的一个实现

private static Key key = null;

// 带向量的DES加密方法
/**
*
* @param data 原文
* @param DESkey key
* @param DESIV 向量
* @return 加密后转换成hex形式的密文字符串
* @throws Exception
*/
public static String encode(String data, byte[] DESkey, byte[] DESIV)
throws Exception {
DESKeySpec keySpec = new DESKeySpec(DESkey);// 设置密钥参数
iv = new IvParameterSpec(DESIV);// 设置向量
SecretKeyFactory keyFactory = SecretKeyFactory.getInstance("DES");// 获得密钥工厂
key = keyFactory.generateSecret(keySpec);// 得到密钥对象
Cipher enCipher = Cipher.getInstance("DES/CBC/PKCS5Padding");// 得到加密对象Cipher
enCipher.init(Cipher.ENCRYPT_MODE, key, iv);// 设置工作模式为加密模式,给出密钥和向量
byte[] pasByte = enCipher.doFinal(data.getBytes("utf-8"));
return byte2hex(pasByte);
} // 带向量的DES解密方法
/**
*
* @param data 加密后转换成hex形式的密文字符串
* @param DESkey key
* @param DESIV 向量
* @return 原文
* @throws Exception
*/
public static String decode(String data, byte[] DESkey, byte[] DESIV)
throws Exception {
DESKeySpec keySpec = new DESKeySpec(DESkey);// 设置密钥参数
iv = new IvParameterSpec(DESIV);// 设置向量
SecretKeyFactory keyFactory = SecretKeyFactory.getInstance("DES");// 获得密钥工厂
key = keyFactory.generateSecret(keySpec);// 得到密钥对象
Cipher deCipher = Cipher.getInstance("DES/CBC/PKCS5Padding");
deCipher.init(Cipher.DECRYPT_MODE, key, iv);
byte[] buf = hex2byte(data.getBytes());
byte[] pasByte = deCipher.doFinal(buf);
return new String(pasByte);
} //不带向量的DES加密方法
/**
*
* @param dataString 原文
* @param keyString key
* @return 加密后转换成hex形式的密文字符串
* @throws Exception
*/
public static String encrypt(String dataString, String keyString) throws Exception {
byte[] data = dataString.getBytes();
byte[] key =keyString.getBytes();
// 生成一个可信任的随机数源
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(Cipher.ENCRYPT_MODE, securekey, sr);
byte[] bt=cipher.doFinal(data);
String strs=byte2hex(bt);
return strs;
}
//不带向量的DES解密方法
/**
*
* @param dataString 加密后转换成hex形式的密文字符串
* @param keyString key
* @return 原文
* @throws Exception
*/
public static String decrypt(String dataString, String keyString)throws Exception{
if (dataString == null)
return null;
byte[] dataHex = dataString.getBytes();
byte[] key =keyString.getBytes();
byte[] data = hex2byte(dataHex);
// 生成一个可信任的随机数源
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(Cipher.DECRYPT_MODE, securekey, sr);
byte[] bt = cipher.doFinal(data);
return new String(bt);
}
/**
* 十六进制字符转换成byte数组
* @param string.getByte
* @return
*/
public static byte[] hex2byte(byte[] b) {
if ((b.length % 2) != 0)
throw new IllegalArgumentException("长度不是偶数");
byte[] b2 = new byte[b.length / 2];
for (int n = 0; n < b.length; n += 2) {
String item = new String(b, n, 2);
b2[n / 2] = (byte) Integer.parseInt(item, 16);
}
return b2;
}
/**
* byte数组转换成16进制字符串
* @param b
* @return
*/
public static String byte2hex(byte[] b) {
String hs = "";
String stmp = ""; for (int n = 0; n < b.length; n++) {
stmp = (java.lang.Integer.toHexString(b[n] & 0XFF));
if (stmp.length() == 1)
hs = hs + "0" + stmp;
else
hs = hs + stmp;
}
return hs.toUpperCase();
}
// 测试
public static void main(String[] args) throws Exception {

// String result=DESCryption.decode("59782316F0DA48F6C2CD6F4E0BE6580A37A98DF27B3E623B2D07D651FA9ED1204AA6ECC637A45725126D6F177F3ACA1EF0FB5AE27B230664","20161108".getBytes(),"20161108".getBytes());

// String result=DESCryption.decode("5C77BDA7EE8096AF5E8DE236DAD59038DB7B8C7A856B09E5703191843F1A4AA0AC36BB835057C661F61F40E945DBE786513ACA1BFDC7AB876FC49E0879C6D07B","20161108".getBytes(),"20161108".getBytes());

String result=DESCryption.encode("{'uid':'admin','pwd':'21232f297a57a5a743894a0e4a801fc3'}","20161108".getBytes(),"20161108".getBytes());

System.out.println(result);

}

}