DES解密出现-“不正确的数据”和“要解密的数据长度无效”-问题,高手来解决!在线等!
源码如下:
/// <summary>
/// 解密给定的字符串
/// </summary>
/// <param name= 'str '> 要解密的字符 </param>
/// <returns> </returns>
public string DecryptString(string str)
{
byte[] ivb=Encoding.ASCII.GetBytes(this.iv);
byte[] keyb=Encoding.ASCII.GetBytes(this.EncryptKey);
byte[] toDecrypt=this.EncodingMode.GetBytes(str);
byte[] deCrypted=new byte[toDecrypt.Length];
ICryptoTransform deCryptor=des.CreateDecryptor(keyb,ivb);
MemoryStream msDecrypt=new MemoryStream(toDecrypt);
CryptoStream csDecrypt=new CryptoStream(msDecrypt,deCryptor,CryptoStreamMode.Read);
try
{
csDecrypt.Read(deCrypted,0,deCrypted.Length);
}
catch(Exception err)
{
throw new ApplicationException(err.Message);
}
finally
{
try
{
msDecrypt.Close();
csDecrypt.Close();
}
catch{;}
}
return this.EncodingMode.GetString(deCrypted);
}
为什么会出现这个问题?
------解决方案--------------------using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Security.Cryptography;
using System.IO;
namespace DES加密解密
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
this.textBox2.Text = DESEncrypt( " " + this.textBox1.Text.ToString().Trim() + " ");
}
//加密
public string DESEncrypt(string pToEncrypt)
{
DESCryptoServiceProvider des = new DESCryptoServiceProvider();
try
{
byte[] inputByteArray = Encoding.Default.GetBytes(pToEncrypt)