对照一下啦//字符串加密
private  string DesEncrypt(string strText, string strEncrKey)
{
byte[] byKey=null;   
byte[] IV= {0xE2, 0x3F, 0xC6, 0xA8, 0x9D, 0x1B, 0xC9, 0xE0};
try
{
byKey = Encoding.UTF8.GetBytes(strEncrKey.Substring(0,8));
DESCryptoServiceProvider des = new DESCryptoServiceProvider();
byte[] inputByteArray = Encoding.UTF8.GetBytes(strText);
MemoryStream ms = new  MemoryStream();
CryptoStream cs = new  CryptoStream(ms, des.CreateEncryptor(byKey, IV), CryptoStreamMode.Write) ;
cs.Write(inputByteArray, 0, inputByteArray.Length);
cs.FlushFinalBlock();
return Convert.ToBase64String(ms.ToArray());  
}
catch
{
return "99999";
}
} //字符串解密
private string DesDecrypt(string strText,string sDecrKey)
{
byte[] byKey = null; 
byte[] IV= {0xE2, 0x3F, 0xC6, 0xA8, 0x9D, 0x1B, 0xC9, 0xE0}; 
byte[] inputByteArray = new Byte[strText.Length];
try
{
byKey = Encoding.UTF8.GetBytes(sDecrKey.Substring(0,8));
DESCryptoServiceProvider des = new DESCryptoServiceProvider();
inputByteArray = Convert.FromBase64String(strText);
MemoryStream ms = new MemoryStream();
CryptoStream cs = new CryptoStream(ms, des.CreateDecryptor(byKey, IV), CryptoStreamMode.Write);
cs.Write(inputByteArray, 0, inputByteArray.Length);
cs.FlushFinalBlock();
Encoding encode = new System.Text.UTF8Encoding();
return encode.GetString(ms.ToArray());
}
catch
{
return "99999";
}
}

解决方案 »

  1.   

    byte[] IV= {0xE2, 0x3F, 0xC6, 0xA8, 0x9D, 0x1B, 0xC9, 0xE0}; 
    这句什么意思啊。。我转成vb.net 出错了
      

  2.   

    这句转vb是dim iv() as byte = {0xE2, 0x3F, 0xC6, 0xA8, 0x9D, 0x1B, 0xC9, 0xE0}定义key
      

  3.   

    学以下SDK:
    ms-help://MS.NETFrameworkSDKv1.1.CHS/cpguidenf/html/cpconEncryptingDecryptingData.htm[Visual Basic]
    Imports System
    Imports System.IO
    Imports System.Security.Cryptography
    Imports System.Net.SocketsModule Module1
    Sub Main()
       Try
          'Create a TCP connection to a listening TCP process.
          'Use "localhost" to specify the current computer or
          'replace "localhost" with the IP address of the 
          'listening process. 
          Dim TCP As New TcpClient("localhost", 11000)      'Create a network stream from the TCP connection. 
          Dim NetStream As NetworkStream = TCP.GetStream()      'Create a new instance of the RijndaelManaged class
          'and encrypt the stream.
          Dim RMCrypto As New RijndaelManaged()      Dim Key As Byte() = {0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16}
          Dim IV As Byte() = {0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16}      'Create a CryptoStream, pass it the NetworkStream, and encrypt 
          'it with the Rijndael class.
          Dim CryptStream As New CryptoStream(NetStream, RMCrypto.CreateEncryptor(Key, IV), CryptoStreamMode.Write)      'Create a StreamWriter for easy writing to the 
          'network stream.
          Dim SWriter As New StreamWriter(CryptStream)      'Write to the stream.
          SWriter.WriteLine("Hello World!")      'Inform the user that the message was written
          'to the stream.
          Console.WriteLine("The message was sent.")      'Close all the connections.
          SWriter.Close()
          CryptStream.Close()
          NetStream.Close()
          TCP.Close()
       Catch
          'Inform the user that an exception was raised.
          Console.WriteLine("The connection failed.")
       End Try
    End Sub
    End Module[C#]
    using System;
    using System.IO;
    using System.Security.Cryptography;
    using System.Net.Sockets;
     
    public class main
    {
       public static void Main(string[] args)
       {
          try
          {
             //Create a TCP connection to a listening TCP process.
             //Use "localhost" to specify the current computer or
             //replace "localhost" with the IP address of the 
             //listening process.  
             TcpClient TCP = new TcpClient("localhost",11000);
       
             //Create a network stream from the TCP connection. 
             NetworkStream NetStream = TCP.GetStream();         //Create a new instance of the RijndaelManaged class
             // and encrypt the stream.
             RijndaelManaged RMCrypto = new RijndaelManaged();         byte[] Key = {0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16};
             byte[] IV = {0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16};         //Create a CryptoStream, pass it the NetworkStream, and encrypt 
             //it with the Rijndael class.
             CryptoStream CryptStream = new CryptoStream(NetStream, 
             RMCrypto.CreateEncryptor(Key, IV),   
             CryptoStreamMode.Write);         //Create a StreamWriter for easy writing to the 
             //network stream.
             StreamWriter SWriter = new StreamWriter(CryptStream);         //Write to the stream.
             SWriter.WriteLine("Hello World!");         //Inform the user that the message was written
             //to the stream.
             Console.WriteLine("The message was sent.");         //Close all the connections.
             SWriter.Close();
             CryptStream.Close();
             NetStream.Close();
             TCP.Close();
          }
          catch
          {
             //Inform the user that an exception was raised.
             Console.WriteLine("The connection failed.");
          }
       }
    }
      

  4.   

    {0xE2, 0x3F, 0xC6, 0xA8, 0x9D, 0x1B, 0xC9, 0xE0}
    oxe2出错,系统提示需要“}”
      

  5.   

    解密时应该是:
    des.CreateDecryptor
    而不是:
    des.CreateEncryptor