积极答复者
求解DES加密解密,解密没问题,就是解密总是提示“要解密的数据的长度无效。”

问题
-
using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Linq; using System.Text; using System.Windows.Forms; using System.IO; using System.Security.Cryptography; namespace DESSampleForm { public partial class Form1 : Form { public Form1() { InitializeComponent(); } static byte[] EncryptStringToBytes(string plainText, byte[] Key, byte[] IV) { // Check arguments. if (plainText == null || plainText.Length <= 0) throw new ArgumentNullException("plainText"); if (Key == null || Key.Length <= 0) throw new ArgumentNullException("Key"); if (IV == null || IV.Length <= 0) throw new ArgumentNullException("Key"); byte[] encrypted; // Create an TripleDESCryptoServiceProvider object // with the specified key and IV. using (TripleDESCryptoServiceProvider tdsAlg = new TripleDESCryptoServiceProvider()) { tdsAlg.Key = Key; tdsAlg.IV = IV; // Create a decrytor to perform the stream transform. ICryptoTransform encryptor = tdsAlg.CreateEncryptor(tdsAlg.Key, tdsAlg.IV); // Create the streams used for encryption. using (MemoryStream msEncrypt = new MemoryStream()) { using (CryptoStream csEncrypt = new CryptoStream(msEncrypt, encryptor, CryptoStreamMode.Write)) { using (StreamWriter swEncrypt = new StreamWriter(csEncrypt)) { //Write all data to the stream. swEncrypt.Write(plainText); } encrypted = msEncrypt.ToArray(); } } } // Return the encrypted bytes from the memory stream. return encrypted; } static string DecryptStringFromBytes(byte[] cipherText, byte[] Key, byte[] IV) { // Check arguments. if (cipherText == null || cipherText.Length <= 0) throw new ArgumentNullException("cipherText"); if (Key == null || Key.Length <= 0) throw new ArgumentNullException("Key"); if (IV == null || IV.Length <= 0) throw new ArgumentNullException("Key"); // Declare the string used to hold // the decrypted text. string plaintext = null; // Create an TripleDESCryptoServiceProvider object // with the specified key and IV. using (TripleDESCryptoServiceProvider tdsAlg = new TripleDESCryptoServiceProvider()) { tdsAlg.Key = Key; tdsAlg.IV = IV; // Create a decrytor to perform the stream transform. ICryptoTransform decryptor = tdsAlg.CreateDecryptor(tdsAlg.Key, tdsAlg.IV); // Create the streams used for decryption. using (MemoryStream msDecrypt = new MemoryStream(cipherText)) { using (CryptoStream csDecrypt = new CryptoStream(msDecrypt, decryptor, CryptoStreamMode.Read)) { using (StreamReader srDecrypt = new StreamReader(csDecrypt)) { // Read the decrypted bytes from the decrypting stream // and place them in a string. plaintext = srDecrypt.ReadToEnd(); } } } } return plaintext; } private void button1_Click(object sender, EventArgs e) { TripleDESCryptoServiceProvider myTripleDES = new TripleDESCryptoServiceProvider(); byte[] encrypted = EncryptStringToBytes(textBox1.Text.Trim(), myTripleDES.Key, myTripleDES.IV); textBox2.Text =Convert.ToBase64String(encrypted.ToArray()); } private void button2_Click(object sender, EventArgs e) { TripleDESCryptoServiceProvider myTripleDES = new TripleDESCryptoServiceProvider(); byte[] temp = System.Text.Encoding.Default.GetBytes(textBox2.Text.Trim()); textBox3.Text = DecryptStringFromBytes(temp, myTripleDES.Key, myTripleDES.IV); } } }
Raymond
- 已移动 ThankfulHeartModerator 2014年9月8日 0:52
答案
-
你好:
我对这一块也不是太熟,但是查了一下MSDN文档:
TripleDESCryptoServiceProvider Class
文档里面已经提供了完整的加解密的方法,你的代码应该是有问题的, 因为在加密和解密的方法里面各自使用了自己的key和IV
以下是MSDN中在内存里面进行加解密的示例代码,你可以参考着修改一下你自己的代码:
using System; using System.Security.Cryptography; using System.Text; using System.IO; class TrippleDESCSPSample { static void Main() { try { // Create a new TripleDESCryptoServiceProvider object // to generate a key and initialization vector (IV). TripleDESCryptoServiceProvider tDESalg = new TripleDESCryptoServiceProvider(); // Create a string to encrypt. string sData = "Here is some data to encrypt."; // Encrypt the string to an in-memory buffer. byte[] Data = EncryptTextToMemory(sData, tDESalg.Key, tDESalg.IV); // Decrypt the buffer back to a string. string Final = DecryptTextFromMemory(Data, tDESalg.Key, tDESalg.IV); // Display the decrypted string to the console. Console.WriteLine(Final); } catch (Exception e) { Console.WriteLine(e.Message); } } public static byte[] EncryptTextToMemory(string Data, byte[] Key, byte[] IV) { try { // Create a MemoryStream. MemoryStream mStream = new MemoryStream(); // Create a CryptoStream using the MemoryStream // and the passed key and initialization vector (IV). CryptoStream cStream = new CryptoStream(mStream, new TripleDESCryptoServiceProvider().CreateEncryptor(Key, IV), CryptoStreamMode.Write); // Convert the passed string to a byte array. byte[] toEncrypt = new ASCIIEncoding().GetBytes(Data); // Write the byte array to the crypto stream and flush it. cStream.Write(toEncrypt, 0, toEncrypt.Length); cStream.FlushFinalBlock(); // Get an array of bytes from the // MemoryStream that holds the // encrypted data. byte[] ret = mStream.ToArray(); // Close the streams. cStream.Close(); mStream.Close(); // Return the encrypted buffer. return ret; } catch(CryptographicException e) { Console.WriteLine("A Cryptographic error occurred: {0}", e.Message); return null; } } public static string DecryptTextFromMemory(byte[] Data, byte[] Key, byte[] IV) { try { // Create a new MemoryStream using the passed // array of encrypted data. MemoryStream msDecrypt = new MemoryStream(Data); // Create a CryptoStream using the MemoryStream // and the passed key and initialization vector (IV). CryptoStream csDecrypt = new CryptoStream(msDecrypt, new TripleDESCryptoServiceProvider().CreateDecryptor(Key, IV), CryptoStreamMode.Read); // Create buffer to hold the decrypted data. byte[] fromEncrypt = new byte[Data.Length]; // Read the decrypted data out of the crypto stream // and place it into the temporary buffer. csDecrypt.Read(fromEncrypt, 0, fromEncrypt.Length); //Convert the buffer into a string and return it. return new ASCIIEncoding().GetString(fromEncrypt); } catch(CryptographicException e) { Console.WriteLine("A Cryptographic error occurred: {0}", e.Message); return null; } } }
We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
Click HERE to participate the survey.- 已编辑 CaillenModerator 2014年9月9日 3:15
- 已建议为答案 ThankfulHeartModerator 2014年9月16日 8:45
- 已标记为答案 CaillenModerator 2014年9月16日 9:41
全部回复
-
你好:
我对这一块也不是太熟,但是查了一下MSDN文档:
TripleDESCryptoServiceProvider Class
文档里面已经提供了完整的加解密的方法,你的代码应该是有问题的, 因为在加密和解密的方法里面各自使用了自己的key和IV
以下是MSDN中在内存里面进行加解密的示例代码,你可以参考着修改一下你自己的代码:
using System; using System.Security.Cryptography; using System.Text; using System.IO; class TrippleDESCSPSample { static void Main() { try { // Create a new TripleDESCryptoServiceProvider object // to generate a key and initialization vector (IV). TripleDESCryptoServiceProvider tDESalg = new TripleDESCryptoServiceProvider(); // Create a string to encrypt. string sData = "Here is some data to encrypt."; // Encrypt the string to an in-memory buffer. byte[] Data = EncryptTextToMemory(sData, tDESalg.Key, tDESalg.IV); // Decrypt the buffer back to a string. string Final = DecryptTextFromMemory(Data, tDESalg.Key, tDESalg.IV); // Display the decrypted string to the console. Console.WriteLine(Final); } catch (Exception e) { Console.WriteLine(e.Message); } } public static byte[] EncryptTextToMemory(string Data, byte[] Key, byte[] IV) { try { // Create a MemoryStream. MemoryStream mStream = new MemoryStream(); // Create a CryptoStream using the MemoryStream // and the passed key and initialization vector (IV). CryptoStream cStream = new CryptoStream(mStream, new TripleDESCryptoServiceProvider().CreateEncryptor(Key, IV), CryptoStreamMode.Write); // Convert the passed string to a byte array. byte[] toEncrypt = new ASCIIEncoding().GetBytes(Data); // Write the byte array to the crypto stream and flush it. cStream.Write(toEncrypt, 0, toEncrypt.Length); cStream.FlushFinalBlock(); // Get an array of bytes from the // MemoryStream that holds the // encrypted data. byte[] ret = mStream.ToArray(); // Close the streams. cStream.Close(); mStream.Close(); // Return the encrypted buffer. return ret; } catch(CryptographicException e) { Console.WriteLine("A Cryptographic error occurred: {0}", e.Message); return null; } } public static string DecryptTextFromMemory(byte[] Data, byte[] Key, byte[] IV) { try { // Create a new MemoryStream using the passed // array of encrypted data. MemoryStream msDecrypt = new MemoryStream(Data); // Create a CryptoStream using the MemoryStream // and the passed key and initialization vector (IV). CryptoStream csDecrypt = new CryptoStream(msDecrypt, new TripleDESCryptoServiceProvider().CreateDecryptor(Key, IV), CryptoStreamMode.Read); // Create buffer to hold the decrypted data. byte[] fromEncrypt = new byte[Data.Length]; // Read the decrypted data out of the crypto stream // and place it into the temporary buffer. csDecrypt.Read(fromEncrypt, 0, fromEncrypt.Length); //Convert the buffer into a string and return it. return new ASCIIEncoding().GetString(fromEncrypt); } catch(CryptographicException e) { Console.WriteLine("A Cryptographic error occurred: {0}", e.Message); return null; } } }
We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
Click HERE to participate the survey.- 已编辑 CaillenModerator 2014年9月9日 3:15
- 已建议为答案 ThankfulHeartModerator 2014年9月16日 8:45
- 已标记为答案 CaillenModerator 2014年9月16日 9:41