C# C语言中的TripleDES算法#
我目前正在C#中进行TripleDES加密,我收到了来自JAVA的代码示例 我在C#中创建了一个加密函数,代码示例如下: 投入: key/ekay=“15ce89cd1a2a838f4f6d49d60438251915ce89cd1a2a838f” text/data=“0000000000000000” 但是在比较了C#输出和JAVA输出之后,我得到了不同的结果 JAVA代码C# C语言中的TripleDES算法#,c#,encryption,cryptography,tripledes,C#,Encryption,Cryptography,Tripledes,我目前正在C#中进行TripleDES加密,我收到了来自JAVA的代码示例 我在C#中创建了一个加密函数,代码示例如下: 投入: key/ekay=“15ce89cd1a2a838f4f6d49d60438251915ce89cd1a2a838f” text/data=“0000000000000000” 但是在比较了C#输出和JAVA输出之后,我得到了不同的结果 JAVA代码 public static String encrypt(String data, String ekey) {
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;
}
输出:
C#:eca27a1e639900f3298a5090cc34dd29
JAVA:c0a946402dd20f5e
如果有任何帮助,我们将不胜感激
谢谢。这是修改后的代码,解决了我的问题
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();
}
关键世代是不同的。注:c#在IV之前。DESede==TripleDES?@bradbury9是的,DES-EDE意味着DES-encrypt-decrypt-encrypt,描述了triple-DES的加密操作(我认为正式的3-DEA,但triple-DES是标准中认可的别名,每个人都使用它)。嗯,一个有MD5,另一个没有。一个有妖术,另一个没有。你甚至都没试过。复制使用三重DES的代码并不意味着我们将为您编写代码。明白了,谢谢!!
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();
}