对16位十六进制字符串进行DES加密,解密。-使用DES加密工具验证成功。
java和C#代码。互相加密解密、
java代码:(两种方法)
第一种:
package com.fhk.Decrypt;
import java.security.spec.KeySpec;
import javax.crypto.Cipher;
import javax.crypto.SecretKey;
import javax.crypto.SecretKeyFactory;
import javax.crypto.spec.DESKeySpec;
import com.fhk.util.ConvertUtil;
/**
* DES加密、解密
* @ClassName DES
* @author 张月
* @date 2013年8月8日
*/
public class DES{
/**
* DES加密
* @param HexString 字符串(16位16进制字符串)
* @param keyStr 密钥16个1
* @throws Exception
*/
public static byte[] SEncrypt_DES(byte[] HexString,byte[] keyStr) throws Exception{
try {
byte[] theCph = new byte[8];
try {
byte[] theKey = null;
byte[] theMsg = null;
theMsg = HexString;
theKey = keyStr;
KeySpec ks = new DESKeySpec(theKey);
SecretKeyFactory kf = SecretKeyFactory.getInstance("DES");
SecretKey ky = kf.generateSecret(ks);
Cipher cf = Cipher.getInstance("DES/ECB/NoPadding");
cf.init(Cipher.ENCRYPT_MODE,ky);
theCph = cf.doFinal(theMsg);
// System.out.println("*************DES加密****************");
// System.out.println("密钥 : "+bytesToHex(keyStr));
// System.out.println("字符串: "+bytesToHex(theMsg));
// System.out.println("加密后: "+bytesToHex(theCph));
} catch (Exception e) {
e.printStackTrace();
}
return theCph;
} catch (Exception e) {
throw e;
}
}
//进行ECB模式的DES加密,已验证成功
public static void main(String[] args) throws Exception {
byte[] s = SEncrypt_DES(PBOCDESConvertUtil.hexStringToByte("0123456789ABCDEF"),PBOCDESConvertUtil.hexStringToByte("1111111111111111"));
System.out.println(s.length);
}
/**
* DES解密
*
* @param hexStr 16位十六进制字符串
* @param keyStr 密钥16个1
* @param modeStr 解密模式:ECB
* @throws Exception
*/
public static byte[] SDecrypt_DES(byte[] hexStr,byte[] keyStr) throws Exception{
try {
String algorithm = "DES/ECB/NoPadding";
byte[] theCph = new byte[8];
byte[] theKey = null;
byte[] theMsg = null;
theMsg = hexStr;
theKey = keyStr;
KeySpec ks = new DESKeySpec(theKey);
SecretKeyFactory kf
= SecretKeyFactory.getInstance("DES");
SecretKey ky = kf.generateSecret(ks);
Cipher cf = Cipher.getInstance(algorithm);
cf.init(Cipher.DECRYPT_MODE,ky);
theCph = cf.doFinal(theMsg);
// System.out.println("*************DES解密****************");
// System.out.println("密钥 : "+bytesToHex(theKey));
// System.out.println("字符串: "+bytesToHex(theMsg));
// System.out.println("解密后: "+bytesToHex(theCph));
return theCph;
} catch (Exception e) {
throw e;
}
}
public static byte[] hexToBytes(String str) {
try {
if (str==null) {
return null;
} else if (str.length() < 2) {
return null;
} else {
int len = str.length() / 2;
byte[] buffer = new byte[len];
for (int i=0; i<len; i++) {
buffer[i] = (byte) Integer.parseInt(
str.substring(i*2,i*2+2),16);
}
return buffer;
}
} catch (Exception e) {
throw e;
}
}
public static String bytesToHex(byte[] data) {
try {
if (data==null) {
return null;
} else {
int len = data.length;
String str = "";
for (int i=0; i<len; i++) {
if ((data[i]&0xFF)<16) str = str + "0"
+ java.lang.Integer.toHexString(data[i]&0xFF);
else str = str
+ java.lang.Integer.toHexString(data[i]&0xFF);
}
return str.toUpperCase();
}
} catch (Exception e) {
throw e;
}
}
}
java:Des算法,第二种代码:
package com.fhk.Decrypt;
import java.security.Key;
import javax.crypto.Cipher;
import javax.crypto.spec.SecretKeySpec;
import com.fhk.util.ConvertUtil;
//ECB模式 DES加密、解密算法
public class DesECBencrypt {
/**
* 加密数据
* @param encryptString 注意:这里的数据长度只能为8的倍数
* @param encryptKey
* @return
* @throws Exception
*/
public static byte[] encryptDES(byte[] encryptbyte, byte[] encryptKey) throws Exception {
SecretKeySpec key = new SecretKeySpec(getKey(encryptKey), "DES");
Cipher cipher = Cipher.getInstance("DES/ECB/NoPadding");
cipher.init(Cipher.ENCRYPT_MODE, key);
byte[] encryptedData = cipher.doFinal(encryptbyte);
return encryptedData;
}
//进行ECB模式的DES加密,已验证成功
public static void main(String[] args) throws Exception {
byte[] s = encryptDES(PBOCDESConvertUtil.hexStringToByte("0123456789ABCDEF"),PBOCDESConvertUtil.hexStringToByte("1111111111111111"));
System.out.println(DES.bytesToHex(s));
}
/**
* 自定义一个key
* @param string
*/
public static byte[] getKey(byte[] keyRule) {
Key key = null;
byte[] keyByte = keyRule;
// 创建一个空的八位数组,默认情况下为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.getEncoded();
}
/***
* 解密数据
* @param decryptString
* @param decryptKey
* @return
* @throws Exception
*/
public static String decryptDES(String decryptString, byte[] decryptKey) throws Exception {
SecretKeySpec key = new SecretKeySpec(getKey(decryptKey), "DES");
Cipher cipher = Cipher.getInstance("DES/ECB/NoPadding");
cipher.init(Cipher.DECRYPT_MODE, key);
byte decryptedData[] = cipher.doFinal(PBOCDESConvertUtil.hexStringToByte(decryptString));
return new String(decryptedData);
}
}
//对MAC进行DES加密
public static string Encrypt_DES16(string str_in_data, string str_DES_KEY) //数据为十六进制
{
try
{
byte[] shuju = new byte[8];
byte[] keys = new byte[8];
for (int i = 0; i < 8; i++)
{
shuju[i] = Convert.ToByte(str_in_data.Substring(i * 2, 2), 16);
keys[i] = Convert.ToByte(str_DES_KEY.Substring(i * 2, 2), 16);
}
DES desEncrypt = new DESCryptoServiceProvider();
desEncrypt.Mode = CipherMode.ECB;
//desEncrypt.Key = ASCIIEncoding.ASCII.GetBytes(str_DES_KEY);
desEncrypt.Key = keys;
byte[] Buffer;
Buffer = shuju;//ASCIIEncoding.ASCII.GetBytes(str_in_data);
ICryptoTransform transForm = desEncrypt.CreateEncryptor();
byte[] R;
R = transForm.TransformFinalBlock(Buffer, 0, Buffer.Length);
string return_str = "";
foreach (byte b in R)
{
return_str += b.ToString("X2");
}
return_str = return_str.Substring(0, 16);
return return_str;
}
catch (Exception e)
{
ScaleConvertion.WriteLog("PBOCDESMAC.cs中Encrypt_DES16()方法出现异常:" + e.Message);
throw e;
}
}