C#DESCryptoServiceProvide类解密问题!!!
发布网友
发布时间:2022-05-06 13:12
我来回答
共1个回答
热心网友
时间:2022-06-30 16:51
你没有给出全部代码,而DES有很多有差别的方法,得出的密文是不一样的,我写通用一点吧
DES_ncbc_encrypt((unsigned char *)src[需要加密的字符串], cipher[加密后存到的地方], len[需加密的字符串的长度], &ks[密码表,由DES_string_to_key(keystring.c_str(), key);
通过key生成], &ivec[用途不知], DES_ENCRYPT[为1加密为0解密] );
所以密钥是KEY
而C#的解密方法如下:
public class DES
{
//默认密钥向量
private static byte[] Keys = { 0x12, 0x34, 0x56, 0x78, 0x90, 0xAB, 0xCD, 0xEF };
/// <summary>
/// DES解密字符串
/// </summary>
/// <param name="decryptString">待解密的字符串</param>
/// <param name="decryptKey">解密密钥,要求为8位,和加密密钥相同</param>
/// <returns>解密成功返回解密后的字符串,失败返源串</returns>
public static string DecryptDES(string decryptString, string decryptKey)
{
try
{
byte[] rgbKey = Encoding.UTF8.GetBytes(decryptKey.Substring(0, 8));
byte[] rgbIV = Keys;
byte[] inputByteArray = Convert.FromBase64String(decryptString);
DESCryptoServiceProvider DCSP = new DESCryptoServiceProvider();
MemoryStream mStream = new MemoryStream();
CryptoStream cStream = new CryptoStream(mStream, DCSP.CreateDecryptor(rgbKey, rgbIV), CryptoStreamMode.Write);
cStream.Write(inputByteArray, 0, inputByteArray.Length);
cStream.FlushFinalBlock();
return Encoding.UTF8.GetString(mStream.ToArray());
}
catch
{
return decryptString;
}
}
}
解密BYTE组的话相应简单修改就行,相信你明白的