java des 16进制_java和C#对16位的十六进制MAckey进行加密解密。 java解密C#用DES加密的字符串 | 学步园...

java代码:

package c.m;

import java.security.spec.KeySpec;

import javax.crypto.Cipher;

import javax.crypto.SecretKey;

import javax.crypto.SecretKeyFactory;

import javax.crypto.spec.DESKeySpec;

public class DES{

public static void main(String[] args) throws Exception {

//DES加密 16进制字符串

ENCRYPTMethod("5C656FF73140F705","1111111111111111");

//DES解密---使用ECB模式

test2("A104E4BA4E121B4E","1111111111111111","DES/ECB/NoPadding");

}

/**

* DES加密

* @param HexString 字符串(16位16进制字符串)

* @param keyStr 密钥16个1

* @throws Exception

*/

public static void ENCRYPTMethod(String HexString,String keyStr) throws Exception{

try {

byte[] theKey = null;

byte[] theMsg = null;

theMsg = hexToBytes(HexString);

theKey = hexToBytes(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);

byte[] theCph = cf.doFinal(theMsg);

System.out.println("*************DES加密****************");

System.out.println("密钥 : "+bytesToHex(theKey));

System.out.println("字符串: "+bytesToHex(theMsg));

System.out.println("加密后: "+bytesToHex(theCph));

} catch (Exception e) {

e.printStackTrace();

return;

}

}

/**

* DES解密

*

* @param hexStr 16位十六进制字符串

* @param keyStr 密钥16个1

* @param modeStr 解密模式:ECB

* @throws Exception

*/

public static void test2(String hexStr,String keyStr,String modeStr) throws Exception{

String algorithm = modeStr;

try {

byte[] theKey = null;

byte[] theMsg = null;

theMsg = hexToBytes(hexStr);

theKey = hexToBytes(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);

byte[] theCph = cf.doFinal(theMsg);

System.out.println("*************DES解密****************");

System.out.println("密钥 : "+bytesToHex(theKey));

System.out.println("字符串: "+bytesToHex(theMsg));

System.out.println("解密后: "+bytesToHex(theCph));

} catch (Exception e) {

e.printStackTrace();

return;

}

}

public static byte[] hexToBytes(String str) {

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;

}

}

public static String bytesToHex(byte[] data) {

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

}

}

}

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)

{

throw e;

}

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值