對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

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

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);

}

}

C#代碼如下:

//對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;

}

}

Logo

北京人形旗下天工造物具身智能开源社区,聚焦具身天工与慧思开物两大平台

更多推荐