C#的算法返回的是十六进制码,也就是像C2B3E11ABCD....之类的,其中每个字代表一个16进制数,即半个字节,2个字就是1个字节,16个字其实效力只有8字节
有没有c#加密解密代码啊,结果和我贴的图片上结果一致。
@猪头猪脑: 用这个试试
///<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;
}
}
@az235: 这是解密方法 ,加密方法呢?求教。。。
@猪头猪脑: 试试这个
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();
}
@az235:不行啊。
学习。。。