首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > .NET > C# >

DES解密出现-“不正确的数据”和“要解密的数据长度无效”-有关问题,高手来解决!

2012-01-09 
DES解密出现-“不正确的数据”和“要解密的数据长度无效”-问题,高手来解决!在线等!源码如下:///summary///

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);
//密匙和偏移量
byte[] kkk = new byte[] { 0x16, 0x09, 0x14, 0x15, 0x07, 0x01, 0x05, 0x08 };


byte[] iivv = new byte[] { 0x08, 0x05, 0x01, 0x07, 0x15, 0x14, 0x09, 0x16 };
des.Key = kkk;
des.IV = iivv;

MemoryStream ms = new MemoryStream();
CryptoStream cs = new CryptoStream(ms, des.CreateEncryptor(), CryptoStreamMode.Write);
cs.Write(inputByteArray, 0, inputByteArray.Length);
cs.FlushFinalBlock();
StringBuilder ret = new StringBuilder();
foreach (byte b in ms.ToArray())
{
ret.AppendFormat( "{0:X2} ", b);
}
ret.ToString();
return ret.ToString();
}
catch
{
return " ";
}

}

private void button2_Click(object sender, EventArgs e)
{
this.textBox3.Text = DESDecrypt( " " + this.textBox2.Text.ToString().Trim() + " ");
}
//解密
public string DESDecrypt(string pToDecrypt)
{
DESCryptoServiceProvider des = new DESCryptoServiceProvider();
try
{
byte[] inputByteArray = new byte[pToDecrypt.Length / 2];
for (int x = 0; x < pToDecrypt.Length / 2; x++)
{
int i = (Convert.ToInt32(pToDecrypt.Substring(x * 2, 2), 16));
inputByteArray[x] = (byte)i;
}

byte[] kkk = new byte[] { 0x16, 0x09, 0x14, 0x15, 0x07, 0x01, 0x05, 0x08 };
byte[] iivv = new byte[] { 0x08, 0x05, 0x01, 0x07, 0x15, 0x14, 0x09, 0x16 };
des.Key = kkk;
des.IV = iivv;

MemoryStream ms = new MemoryStream();
CryptoStream cs = new CryptoStream(ms, des.CreateDecryptor(), CryptoStreamMode.Write);
cs.Write(inputByteArray, 0, inputByteArray.Length);
cs.FlushFinalBlock();

StringBuilder ret = new StringBuilder();

return System.Text.Encoding.Default.GetString(ms.ToArray());
}
catch
{
return " ";
}
}




}
}

热点排行