private void button1_Click(object sender, System.EventArgs e)
{
string strPwd = "2D3FAFB85B04266E";
string strkey = "BA9FDB88260C5046";
Decrypt(strPwd, strkey);
}
/// <summary>
/// 进行DES解密。
/// </summary>
/// <param name="pToDecrypt">要解密的以Base64</param>
/// <param name="sKey">密钥,且必须为8位。</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;
}
}报错为:指定键的大小对于此算法无效。是在des.Key = ASCIIEncoding.ASCII.GetBytes(sKey);这句报得错请高手帮忙。在线等。
{
string strPwd = "2D3FAFB85B04266E";
string strkey = "BA9FDB88260C5046";
Decrypt(strPwd, strkey);
}
/// <summary>
/// 进行DES解密。
/// </summary>
/// <param name="pToDecrypt">要解密的以Base64</param>
/// <param name="sKey">密钥,且必须为8位。</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;
}
}报错为:指定键的大小对于此算法无效。是在des.Key = ASCIIEncoding.ASCII.GetBytes(sKey);这句报得错请高手帮忙。在线等。
如果此属性在使用时为空引用(在 Visual Basic 中为 Nothing),则将调用 GenerateKey 创建新的随机值。
密钥长度必须等于 BlockSizeValue。
报错为:指定键的大小对于此算法无效。引发此异常的原因如下:
试图设置长度不等于 BlockSizeValue 的密钥。
msdn提供的DES解密加密算法示例如下// This sample demonstrates using a key based on the cryptographic service provider (CSP) version
// of the Data Encryption Standard (DES)algorithm to encrypt a string to a byte array, and then
// to decrypt the byte array back to a string.using System;
using System.IO;
using System.Text;
using System.Security.Cryptography;class CryptoMemoryStream
{
// Main method.
public static void Main()
{
// Create a new DES key.
DESCryptoServiceProvider key = new DESCryptoServiceProvider(); // Encrypt a string to a byte array.
byte[] buffer = Encrypt("This is some plaintext!", key); // Decrypt the byte array back to a string.
string plaintext = Decrypt(buffer, key); // Display the plaintext value to the console.
Console.WriteLine(plaintext);
}
// Encrypt the string.
public static byte[] Encrypt(string PlainText, SymmetricAlgorithm key)
{
// Create a memory stream.
MemoryStream ms = new MemoryStream(); // Create a CryptoStream using the memory stream and the
// CSP DES key.
CryptoStream encStream = new CryptoStream(ms, key.CreateEncryptor(), CryptoStreamMode.Write); // Create a StreamWriter to write a string
// to the stream.
StreamWriter sw = new StreamWriter(encStream); // Write the plaintext to the stream.
sw.WriteLine(PlainText); // Close the StreamWriter and CryptoStream.
sw.Close();
encStream.Close(); // Get an array of bytes that represents
// the memory stream.
byte[] buffer = ms.ToArray(); // Close the memory stream.
ms.Close(); // Return the encrypted byte array.
return buffer;
} // Decrypt the byte array.
public static string Decrypt(byte[] CypherText, SymmetricAlgorithm key)
{
// Create a memory stream to the passed buffer.
MemoryStream ms = new MemoryStream(CypherText); // Create a CryptoStream using the memory stream and the
// CSP DES key.
CryptoStream encStream = new CryptoStream(ms, key.CreateDecryptor(), CryptoStreamMode.Read); // Create a StreamReader for reading the stream.
StreamReader sr = new StreamReader(encStream); // Read the stream as a string.
string val = sr.ReadLine(); // Close the streams.
sr.Close();
encStream.Close();
ms.Close();
return val;
}
}