读书人

请问上c#DES加密没有关问题 解密解不

发布时间: 2012-12-16 12:02:32 作者: rapoo

请教下c#DES加密没问题 ,解密解不出来,解密时老提示数组长度不对。
自己下了段程序,想将加密和解密分开,但加密没问题 ,解密解不出来,解密时老提示数组长度不对,请高手帮下。
textBox1.Text是加密文档路径。
byte[] Key = { 0xF0, 0x3F, 0xB3, 0xF0, 0x8A, 0x0F, 0xA7, 0x9B };
byte[] IV = {0x24 ,0xF9 ,0x04 ,0xCC ,0xDB ,0xF0 ,0xCC ,0x81} ;
CipherMode Mode=CipherMode.ECB;
PaddingMode Padding= PaddingMode.PKCS7;
byte[] cipherbytes;






加密如下:
SymmetricAlgorithm sa =CreateSymmetricAlgorithm();
sa.Key = Key;
sa.IV = IV;
sa.Mode = Mode;
sa.Padding = Padding;
MemoryStream ms = new MemoryStream();
CryptoStream cs = new CryptoStream(ms, sa.CreateEncryptor(),CryptoStreamMode.Write);
StreamReader f = new StreamReader(textBox1.Text);
textPlaintext.Clear();
textPlaintext.Text = f.ReadToEnd();
f.Close();
byte[] plainbytes =Encoding.UTF8.GetBytes(textPlaintext.Text);
cs.Write(plainbytes, 0, plainbytes.Length);
cs.Close();
cipherbytes = ms.ToArray();
ms.Close();
textCiphertext.Text = Encoding.UTF8.GetString(cipherbytes);
StreamWriter ff = new StreamWriter(textBox1.Text);
ff.Write(textCiphertext.Text);
ff.Close();
StringBuilder sb = new StringBuilder();
for (int i = 0; i < cipherbytes.Length; i++)
{sb.Append(String.Format("{0:X2} ", cipherbytes[i])); }
textCipherbytes.Text = sb.ToString();




解密如下:
SymmetricAlgorithm sa =CreateSymmetricAlgorithm();
sa.Key = Key;
sa.Mode = Mode;
sa.Padding = Padding;
sa.IV = IV;
StreamReader f = new StreamReader(textBox1.Text);
textCiphertext.Clear();
textCiphertext.Text = f.ReadToEnd();
f.Close();
MemoryStream ms = new MemoryStream(cipherbytes);CryptoStream cs = new CryptoStream(ms,sa.CreateDecryptor(),CryptoStreamMode.Read);
byte[] plainbytes =new Byte[cipherbytes.Length];
cs.Read(plainbytes, 0, cipherbytes.Length);
cs.Close();
ms.Close();
textRecoveredPlaintext.Text =Encoding.UTF8.GetString(plainbytes);
StreamWriter ff = new StreamWriter(textBox1.Text);
ff.Write(textRecoveredPlaintext.Text);
ff.Close();
[解决办法]
下面是C#实现的DES算法的一个类:
using System;
using System.IO;
using System.Text;
using System.Security.Cryptography;
using System.Web;
using System.Windows.Forms;


/// <summary>
/// Triple Data Encryption Standard algorithms implementations
/// </summary>
/// <Author>Yao</Author>
/// <Date>2005/4/20</Date>

public class CryptionData
{
// The length of Encryptionstring should be 8 byte and not be a weak key
private string EncryptionString;

// The length of initialization vector should be 8 byte
private static Byte[] EncryptionIV = Encoding.Default.GetBytes("abcdefgh");

/// <summary>
/// Constructor
/// </summary>
public CryptionData()
{
}

/// <summary>


/// Constructor
/// </summary>
/// <param name="EncryptionString">SecureKey</param>
public CryptionData(string EncryptionString)
{
this.EncryptionString = EncryptionString;
}

/// <summary>
/// Encryption method for byte array
/// </summary>
/// <param name="SourceData">source data</param>
/// <returns>byte array</returns>
public byte[] EncryptionByteData(byte[] SourceData)
{
byte[] returnData = null;
try
{
// Create DESCryptoServiceProvider object
DESCryptoServiceProvider desProvider = new DESCryptoServiceProvider();

// Set SecureKey and IV of desProvider
byte[] byteKey = Encoding.Default.GetBytes(EncryptionString);
desProvider.Key = byteKey;
desProvider.IV = EncryptionIV;

// A MemoryStream object
MemoryStream ms = new MemoryStream();

// Create Encryptor
ICryptoTransform encrypto = desProvider.CreateEncryptor();

// Create CryptoStream object
CryptoStream cs = new CryptoStream(ms, encrypto, CryptoStreamMode.Write);

// Encrypt SourceData
cs.Write(SourceData,0,SourceData.Length);
cs.FlushFinalBlock();

// Get Encryption result
returnData = ms.ToArray();
}
catch(Exception ex)
{
throw ex;
}

return returnData;

}

/// <summary>
/// Decryption method for byte array
/// </summary>
/// <param name="SourceData">source data</param>
/// <returns>byte array</returns>
public byte[] DecryptionByteData(byte[] SourceData)
{
byte[] returnData = null;
try
{
// Create DESCryptoServiceProvider object
DESCryptoServiceProvider desProvider = new DESCryptoServiceProvider();

// Set SecureKey and IV of desProvider
byte[] byteKey = Encoding.Default.GetBytes(EncryptionString);
desProvider.Key = byteKey;
desProvider.IV = EncryptionIV;

// A MemoryStream object
MemoryStream ms = new MemoryStream();

// Create Decryptor
ICryptoTransform encrypto = desProvider.CreateDecryptor();

// Create CryptoStream object
CryptoStream cs = new CryptoStream(ms, encrypto, CryptoStreamMode.Write);

// Decrypt SourceData


cs.Write(SourceData, 0, SourceData.Length);
cs.FlushFinalBlock();

// Get Decryption result
returnData = ms.ToArray();
}
catch(Exception ex)
{
throw ex;
}
return returnData;

}

/// <summary>
/// Encryption method for string
/// </summary>
/// <param name="SourceData">source data</param>
/// <returns>string</returns>
public string EncryptionStringData(string SourceData)
{
try
{
// Convert source data from string to byte array
byte[] SourData = Encoding.Default.GetBytes(SourceData);

// Encrypt byte array
byte[] retData = EncryptionByteData(SourData);

// Convert encryption result from byte array to Base64String
return Convert.ToBase64String(retData, 0, retData.Length);
}
catch(Exception ex)
{
throw ex;
}
}

/// <summary>
/// Decryption method for string
/// </summary>
/// <param name="SourceData">source data</param>
/// <returns>string</returns>
public string DecryptionStringdata(string SourceData)
{
try
{
// Convert source data from Base64String to byte array
byte[] SourData = Convert.FromBase64String(SourceData);

// Decrypt byte array
byte[] retData = DecryptionByteData(SourData);

// Convert Decryption result from byte array to string
return Encoding.Default.GetString(retData, 0, retData.Length);
}
catch(Exception ex)
{
throw ex;
}
}
}
[解决办法]
F10调试一下就知道了 应该是你解密和加密没对应上

读书人网 >.NET

热点推荐