[英]TripleDES algorithm in C#
I'm currently working on TripleDES encryption in C# for which I've received the code sample from JAVA.我目前正在使用 C# 进行 TripleDES 加密,我已经收到了来自 JAVA 的代码示例。
I have created an encryption function in C# with below code sample :我使用以下代码示例在 C# 中创建了一个加密函数:
Inputs : key/ekay = "15ce89cd1a2a838f4f6d49d60438251915ce89cd1a2a838f"输入:key/ekay = "15ce89cd1a2a838f4f6d49d60438251915ce89cd1a2a838f"
text/data = "0000000000000000"文本/数据 = "0000000000000000"
public static string encryptionMethod(string Text, string key)
{
string encryptedText = string.Empty;
try
{
MD5CryptoServiceProvider md5Hash = new MD5CryptoServiceProvider();
byte[] md5Bytes = md5Hash.ComputeHash(Encoding.UTF8.GetBytes(key));
md5Hash.Clear();
byte[] clearBytes = Encoding.UTF8.GetBytes(Text);
TripleDESCryptoServiceProvider des = new TripleDESCryptoServiceProvider();
des.KeySize = 128;
des.Mode = CipherMode.CBC;
des.Padding = PaddingMode.None;
des.Key = md5Bytes; //Passing key in byte array
//des.BlockSize = 64;
byte[] ivBytes = new byte[8] {0, 0, 0, 0, 0, 0, 0, 0 };
des.IV = ivBytes;
ICryptoTransform ct = des.CreateEncryptor(); //Interface with some result
byte[] resultArray = ct.TransformFinalBlock(clearBytes, 0, clearBytes.Length);
encryptedText = ByteArrayToHexString(resultArray);
}
catch (Exception exception)
{
return "";
}
return encryptedText;
}
public static string ByteArrayToHexString(byte[] ba)
{
StringBuilder hex = new StringBuilder(ba.Length * 2);
foreach (byte b in ba)
hex.AppendFormat("{0:x2}", b);
return hex.ToString();
}
but after comparing the C# output with JAVA output, I got the different results.但是在将 C# 输出与 JAVA 输出进行比较后,我得到了不同的结果。
JAVA code JAVA代码
public static String encrypt(String data, String ekey) {
String encrypteddata = null;
try{
String key = ekey;
byte[] encryptKey = ISOUtil.hex2byte(key);
DESedeKeySpec spec = new DESedeKeySpec(encryptKey);
SecretKeyFactory keyFactory = SecretKeyFactory.getInstance("DESede");
SecretKey theKey = keyFactory.generateSecret(spec);
Cipher cipher = Cipher.getInstance("DESede/CBC/NoPadding");
IvParameterSpec IvParameters = new IvParameterSpec( new byte[] { 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 });
cipher.init(Cipher.ENCRYPT_MODE, theKey, IvParameters);
String plain = data;
byte[] plaintext = ISOUtil.hex2byte(plain);
byte[] encrypted = cipher.doFinal(plaintext);
encrypteddata= ISOUtil.byte2hex(encrypted);
}
catch(Exception e){
}
return encrypteddata;
}
output :输出 :
C# : eca27a1e639900f3298a5090cc34dd29 C#:eca27a1e639900f3298a5090cc34dd29
JAVA : c0a946402dd20f5e爪哇:c0a946402dd20f5e
Any help would be appreciated?任何帮助,将不胜感激?
Thanks.谢谢。
Here is the modified code, that has solved my issue.这是修改后的代码,它解决了我的问题。
public static string encryptionMethod(string Text, string key)
{
string encryptedText = string.Empty;
try
{
byte[] clearBytes = StringToByteArray(Text); ;//Encoding.UTF8.GetBytes(Text);
TripleDESCryptoServiceProvider des = new TripleDESCryptoServiceProvider();
des.KeySize = 128;
des.Mode = CipherMode.CBC;
des.Padding = PaddingMode.None;
des.Key = StringToByteArray(key); //Passing key in byte array
//des.BlockSize = 64;
byte[] ivBytes = new byte[8] { 0, 0, 0, 0, 0, 0, 0, 0 };
des.IV = ivBytes;
ICryptoTransform ct = des.CreateEncryptor(); //Interface with some result
byte[] resultArray = ct.TransformFinalBlock(clearBytes, 0, clearBytes.Length);
encryptedText = ByteArrayToHexString(resultArray);
}
catch (Exception exception)
{
return "";
}
return encryptedText;
}
public static byte[] StringToByteArray(string hex)
{
return Enumerable.Range(0, hex.Length)
.Where(x => x % 2 == 0)
.Select(x => Convert.ToByte(hex.Substring(x, 2), 16))
.ToArray();
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.