首页 新闻 赞助 找找看

求c# des 16位密钥及16位加密数据的加密解密方法。

0
悬赏园豆:100 [已关闭问题] 关闭于 2012-03-27 09:23

这是一个c++的程序,要用c#来实现下图的功能

猪头猪脑的主页 猪头猪脑 | 初学一级 | 园豆:6
提问于:2012-03-26 10:46
< >
分享
所有回答(3)
0
LCM | 园豆:6876 (大侠五级) | 2012-03-26 11:05

那个是8位的。。我要的是16位的。谢谢

支持(0) 反对(0) 猪头猪脑 | 园豆:6 (初学一级) | 2012-03-26 11:08
0
C#的算法返回的是十六进制码,也就是像C2B3E11ABCD....之类的,其中每个字代表一个16进制数,即半个字节,2个字就是1个字节,16个字其实效力只有8字节
az235 | 园豆:8483 (大侠五级) | 2012-03-26 11:08

有没有c#加密解密代码啊,结果和我贴的图片上结果一致。

支持(0) 反对(0) 猪头猪脑 | 园豆:6 (初学一级) | 2012-03-26 13:25

@猪头猪脑: 用这个试试

///<summary>
/// 进行DES解密。
///</summary>
///<param name="pToDecrypt">要解密的以Base64</param>
///<returns>已解密的字符串。</returns>
public string Decrypt(string pToDecrypt,string sKey)
{
byte[] inputByteArray = Convert.FromBase64String(pToDecrypt);
using(DESCryptoServiceProvider des = new DESCryptoServiceProvider())
{
des.Key=ASCIIEncoding.ASCII.GetBytes(sKey);
des.IV=ASCIIEncoding.ASCII.GetBytes(sKey);
System.IO.MemoryStream ms = new System.IO.MemoryStream();
using(CryptoStream cs = new CryptoStream(ms,des.CreateDecryptor(),CryptoStreamMode.Write))
{
cs.Write(inputByteArray,0,inputByteArray.Length);
cs.FlushFinalBlock();
cs.Close();
}
string str = Encoding.UTF8.GetString(ms.ToArray());
ms.Close();
return str;
}
}
支持(0) 反对(0) az235 | 园豆:8483 (大侠五级) | 2012-03-26 13:38

@az235: 这是解密方法 ,加密方法呢?求教。。。

支持(0) 反对(0) 猪头猪脑 | 园豆:6 (初学一级) | 2012-03-26 13:49

@猪头猪脑: 试试这个

private static void EncryptData(String inName, String outName, byte[] rijnKey, byte[] rijnIV)
{
//Create the file streams to handle the input and output files.
FileStream fin = new FileStream(inName, FileMode.Open, FileAccess.Read);
FileStream fout = new FileStream(outName, FileMode.OpenOrCreate, FileAccess.Write);
fout.SetLength(0);

//Create variables to help with read and write.
byte[] bin = new byte[100]; //This is intermediate storage for the encryption.
long rdlen = 0; //This is the total number of bytes written.
long totlen = fin.Length; //This is the total length of the input file.
int len; //This is the number of bytes to be written at a time.

SymmetricAlgorithm rijn = SymmetricAlgorithm.Create(); //Creates the default implementation, which is RijndaelManaged.
CryptoStream encStream = new CryptoStream(fout, rijn.CreateEncryptor(rijnKey, rijnIV), CryptoStreamMode.Write);

Console.WriteLine("Encrypting...");

//Read from the input file, then encrypt and write to the output file.
while(rdlen < totlen)
{
len = fin.Read(bin, 0, 100);
encStream.Write(bin, 0, len);
rdlen = rdlen + len;
Console.WriteLine("{0} bytes processed", rdlen);
}

encStream.Close();
fout.Close();
fin.Close();
}
支持(0) 反对(0) az235 | 园豆:8483 (大侠五级) | 2012-03-26 14:22

@az235:不行啊。

支持(0) 反对(0) 猪头猪脑 | 园豆:6 (初学一级) | 2012-03-26 15:09
0

学习。。。

KivenRo | 园豆:1734 (小虾三级) | 2012-03-26 12:59
清除回答草稿
   您需要登录以后才能回答,未注册用户请先注册