在C#中复制Java代码DES加密 [英] Replicate a java code DES encryption in C#

查看:64
本文介绍了在C#中复制Java代码DES加密的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

伙计们,我在尝试使用C#中的以下示例复制C#中的加密算法时遇到了麻烦.这里的plainData是一个16字符的十六进制值,作为要加密的字符串(ex:048054CC6DED88AD),而keyString是一个16字符的十六进制密钥,其格式为字符串(例如:07D3251ACE1FF48A.我什至已经将Hex和BigInteger类迁移到C#.我需要加密的输出成为字符串中的16个字符的十六进制值.请帮助...

/////////java:开始/////////////

Folks, I am in a dead trouble trying to replicate an encryption algorithm in C# same as the below in java. Here plainData is 16 char hex value as a string (ex:048054CC6DED88AD) to be encrypted and keyString is a 16 char hex key in string format (ex: 07D3251ACE1FF48A. I even have migrated Hex and BigInteger classes to C#. I need the encrypted output to be a 16 char hex value in string. Please Help......

///////// java: start ///////////////

String strEncryptedData;
//Add Cryptix provider
Security.addProvider(new cryptix.provider.Cryptix());
// The DES algorithm requires a trusted source of random bits
SecureRandom sr = new SecureRandom();
// Create a DES key object specification from the raw data
DESKeySpec dks = new DESKeySpec(Hex.fromString(keyString));
// Create a key factory and use it to turn the DESKeySpec into
// a SecretKey object
SecretKeyFactory keyFactory = SecretKeyFactory.getInstance("DES");
SecretKey key = keyFactory.generateSecret( dks );
Cipher cipher = Cipher.getInstance("DES/ECB/NoPadding");
// Initialize the cipher with the key
cipher.init( Cipher.ENCRYPT_MODE, key, sr );
byte[] data = Hex.fromString(plainData);
// The actual encryption step
byte encryptedData[] = cipher.doFinal(data);


//Convert the encrypted bytes big int
BigInteger bCiph = new BigInteger(encryptedData);
//Dump the byte array to output string
strEncryptedData = Hex.dumpString(getMagnitude(bCiph));
strEnryptedData = encryptedData.trim();

//DES operation in DECRYPT mode: for decryption
dks = new DESKeySpec(Hex.fromString(keyString));
key = keyFactory.generateSecret(dks);
cipher.init( Cipher.DECRYPT_MODE, key, sr );
byte encryptedData1[] = cipher.doFinal(encryptedData);



/////////java:结束//////////////


我已经对下面的C#代码进行了相当不错的尝试,以照原样复制上面的Java代码,但是我没有得到想要的结果.

/////////c#:开始//////////////



///////// java: end ///////////////


I have done quite a hit and trial on the below C# code to replicate the above java code as it is but I am not getting it as desired.

///////// c#: start ///////////////

static TripleDES CreateDES(string key)
{
	System.Security.Cryptography.MD5 md5 = new MD5CryptoServiceProvider();
	TripleDES des = new TripleDESCryptoServiceProvider();
	des.Padding = PaddingMode.None;
	des.Mode = CipherMode.ECB;
	//des.Key = md5.ComputeHash(Encoding.Unicode.GetBytes(key));
	//des.Key = md5.ComputeHash(Convert.FromBase64String(key));
	//des.Key = md5.ComputeHash(Encoding.UTF8.GetBytes(key));
	des.Key = md5.ComputeHash(Encoding.ASCII.GetBytes(key));
	des.IV = new byte[des.BlockSize / 8];
	return des;
}
public byte[] Encryption(string PlainText, string key)
{
	//TripleDES des = CreateDES(key);
	TripleDES des = CreateDES(key);
	LogManager.logDebug("in TripleDESUtil des = " + des.ToString(), "COMMAND_DEBUG");
	ICryptoTransform ct = des.CreateEncryptor();
	//byte[] input = Encoding.Unicode.GetBytes(PlainText);
	//byte[] input = Convert.FromBase64String(PlainText);
	//byte[] input = Encoding.UTF8.GetBytes(PlainText);
	byte[] input = Encoding.ASCII.GetBytes(PlainText);
	return ct.TransformFinalBlock(input, 0, input.Length);
}



/////////c#:结束//////////////

问候,
Sadique



///////// c#: end///////////////

regards,
Sadique

推荐答案

尝试查看此内容,简单加密和在C#中解密数据 [ ^ ]
Try looking at this, Simple encrypting and decrypting data in C#[^]


检查此答案 http://stackoverflow.com/q/1400830/1225337 [^ ]


这篇关于在C#中复制Java代码DES加密的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆