我看了一下,主要是这个:
StringBuilder ret = new StringBuilder();
foreach(byte b in ms.ToArray())
   {
ret.Append((char)b);
   }
建立字符串,但是没有解码,如果有Encoding.GetDecoder方法,就可以把字节解码为字符,而且可以加上参数gb2312,就可以得出中文了,不过怎么用?

解决方案 »

  1.   

    using System;
    using System.Security.Cryptography;
    using System.IO;
    namespace HenryFan.Library
    {
    /// <summary>
    /// Class1 的摘要说明。
    /// </summary>
    public class HenryFanCipher
    {
    private static byte[] PublicKey = {214,46,220,83,160,73,40,39,201,155,19,202,3,11,191,178,56,
       74,90,36,248,103,18,144,170,163,145,87,54,61,34,220,222,
       207,137,149,173,14,92,120,206,222,158,28,40,24,30,16,175,
       108,128,35,230,118,40,121,113,125,216,130,11,24,90,48,194,
       240,105,44,76,34,57,249,228,125,80,38,9,136,29,117,207,139,
       168,181,85,137,126,10,126,242,120,247,121,8,100,12,201,171,
       38,226,193,180,190,117,177,87,143,242,213,11,44,180,113,93,
       106,99,179,68,175,211,164,116,64,148,226,254,172,147};
    private byte[] Exponent;
    public HenryFanCipher()
    {
    //
    // TODO: 在此处添加构造函数逻辑
    //
    }
    public byte[] PExponent
    {
    set
    {
    Exponent=value;
    }
    get
    {
    return Exponent;
    }
    }
    public string WriteCipher(string cipher)
    {
    byte[] EncryptedSymmetricKey;
    byte[] EncryptedSymmetricIV;
    RijndaelManaged RM = new RijndaelManaged();
    EncryptedSymmetricKey = (new MD5CryptoServiceProvider()).ComputeHash(PublicKey);
    EncryptedSymmetricIV =(new MD5CryptoServiceProvider()).ComputeHash(Exponent);
    System.IO.MemoryStream stream=new MemoryStream();
    StreamWriter SWriter = new StreamWriter(stream);
    SWriter.Write(cipher);
    SWriter.Flush();
    stream.Position=0;
    CryptoStream CryptStream = new CryptoStream(stream, 
    RM.CreateEncryptor(EncryptedSymmetricKey, EncryptedSymmetricIV),   
    CryptoStreamMode.Read);
    byte[] b=new byte[1000] ;
    MemoryStream m=new MemoryStream();
    int readLength=0;
    while ((readLength=CryptStream.Read(b,0,1000))>0)
    {
    m.Write(b,0,readLength);
    readLength=0;
    }
    byte[] mb=new byte[m.Length];
    m.Position=0;
    m.Read(mb,0,mb.Length);
    m.Close();
    CryptStream.Close();
    stream.Close();
    return System.Text.Encoding.Unicode.GetString(mb);

    }
    public string ReaderCipher(string Cipher)
    {
    byte[] EncryptedSymmetricKey;
    byte[] EncryptedSymmetricIV;
    RijndaelManaged RM = new RijndaelManaged();
    EncryptedSymmetricKey = (new MD5CryptoServiceProvider()).ComputeHash(PublicKey);
    EncryptedSymmetricIV =(new MD5CryptoServiceProvider()).ComputeHash(Exponent);
    MemoryStream stream=new MemoryStream();
    byte[] mb=System.Text.Encoding.Unicode.GetBytes(Cipher);
    stream.Write(mb,0,mb.Length);
    stream.Position=0;
    CryptoStream CryptStream = new CryptoStream(stream, 
    RM.CreateDecryptor(EncryptedSymmetricKey, EncryptedSymmetricIV),   
    CryptoStreamMode.Read);
    StreamReader SReader = new StreamReader(CryptStream);
    string readerPass=SReader.ReadToEnd();
    SReader.Close();
    CryptStream.Close();
    stream.Close();
    return readerPass; }
    public string WritePassWord(string PassWord)
    {
    System.IO.MemoryStream stream=new System.IO.MemoryStream();
    RijndaelManaged RMCrypto = new RijndaelManaged();
    byte[] Key = {0x01, 0x01, 0x03, 0x04, 0x05, 0x06, 0x06, 0x08, 0x09, 0x10, 0x10, 0x12, 0x13, 0x14, 0x15, 0x16};
    byte[] IV = {0x01, 0x01, 0x03, 0x04, 0x05, 0x06, 0x06, 0x08, 0x09, 0x10, 0x10, 0x12, 0x13, 0x14, 0x15, 0x16};
    StreamWriter SWriter = new StreamWriter(stream);
    SWriter.Write(PassWord);
    SWriter.Flush();
    stream.Position=0;
    CryptoStream CryptStream = new CryptoStream(stream, 
    RMCrypto.CreateEncryptor(Key, IV),   
    CryptoStreamMode.Read);
    byte[] b=new byte[100] ;
    MemoryStream m=new MemoryStream();
    int readLength=0;
    while ((readLength=CryptStream.Read(b,0,100))>0)
    {
    m.Write(b,0,readLength);
    readLength=0;
    }
    byte[] mb=new byte[m.Length];
    m.Position=0;
    m.Read(mb,0,mb.Length);
    string WritePass=System.Text.Encoding.Unicode.GetString(mb,0,mb.Length);
    stream.Close();
    CryptStream.Close();
    return WritePass;
    }
    public string ReaderPassWord(string PassWord)
    {

    System.IO.MemoryStream stream=new System.IO.MemoryStream();
    RijndaelManaged RMCrypto = new RijndaelManaged();
    byte[] Key = {0x01, 0x01, 0x03, 0x04, 0x05, 0x06, 0x06, 0x08, 0x09, 0x10, 0x10, 0x12, 0x13, 0x14, 0x15, 0x16};
    byte[] IV = {0x01, 0x01, 0x03, 0x04, 0x05, 0x06, 0x06, 0x08, 0x09, 0x10, 0x10, 0x12, 0x13, 0x14, 0x15, 0x16};
    byte[] mb=System.Text.Encoding.Unicode.GetBytes(PassWord);
    stream.Write(mb,0,mb.Length);
    stream.Position=0;
    CryptoStream CryptStream = new CryptoStream(stream, 
    RMCrypto.CreateDecryptor(Key, IV),   
    CryptoStreamMode.Read);
    StreamReader SReader = new StreamReader(CryptStream);
    string readerPass=SReader.ReadToEnd();
    stream.Close();
    CryptStream.Close();
    return readerPass; } }
    }
    以上是我做的事例,对中文加密解密成功
      

  2.   

    ReaderCipher同WriteCipher方法;
    使用前设PExponent是一个BYTE[]
      

  3.   

    我已经改写成功:public  string Encrypt(string pToEncrypt, string sKey)
    {
    DESCryptoServiceProvider des = new DESCryptoServiceProvider(); //把字符串放到byte数组中
    //原来使用的UTF8编码,我改成Unicode编码了,不行
    byte[] inputByteArray = Encoding.Default.GetBytes(pToEncrypt);
    //byte[] inputByteArray=Encoding.Unicode.GetBytes(pToEncrypt); //建立加密对象的密钥和偏移量
    //原文使用ASCIIEncoding.ASCII方法的GetBytes方法
    //使得输入密码必须输入英文文本
    des.Key = ASCIIEncoding.ASCII.GetBytes(sKey);
    des.IV = ASCIIEncoding.ASCII.GetBytes(sKey);
    MemoryStream ms = new MemoryStream();
    CryptoStream cs = new CryptoStream(ms, des.CreateEncryptor(),
    CryptoStreamMode.Write);
    //Write the byte array into the crypto stream
    //(It will end up in the memory stream)
    cs.Write(inputByteArray, 0, inputByteArray.Length);
    cs.FlushFinalBlock(); //Get the data back from the memory stream, and into a string
    StringBuilder ret = new StringBuilder();
    foreach(byte b in ms.ToArray())
    {
    //Format as hex
    ret.AppendFormat("{0:X2}", b);
    }
    ret.ToString();
    return ret.ToString();
    }
    public  string Decrypt(string pToDecrypt, string sKey)
    {
    DESCryptoServiceProvider des = new DESCryptoServiceProvider(); //Put the input string into the byte array
    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;
    } //建立加密对象的密钥和偏移量,此值重要,不能修改
    des.Key = ASCIIEncoding.ASCII.GetBytes(sKey);
    des.IV = ASCIIEncoding.ASCII.GetBytes(sKey);
    MemoryStream ms = new MemoryStream();
    CryptoStream cs = new CryptoStream(ms, des.CreateDecryptor(),CryptoStreamMode.Write);
    //Flush the data through the crypto stream into the memory stream
    cs.Write(inputByteArray, 0, inputByteArray.Length);
    cs.FlushFinalBlock(); //Get the decrypted data back from the memory stream
    //建立StringBuild对象,CreateDecrypt使用的是流对象,必须把解密后的文本变成流对象
    StringBuilder ret = new StringBuilder();

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

  4.   

    谢谢啦,TheAres(班门斧):
    原来还是编码的问题,我还以为是算法错了呢~~
    谢谢~~~
    给分!
      

  5.   

    我这里是这么写的.通过了中文的测试.一切OKEncrypt.csusing System;
    using System.Security.Cryptography;
    using System.IO;public class Encrypt{
    public static void Main(String[] args) {
    byte[] deskey = {198,125,20,111,77,71,228,9};
    byte[] desiv = {223,20,19,27,226,119,149,85};
    EncryptData("./jm.txt","./jm.des",deskey,desiv);
    }
    private static void EncryptData(String inName, String outName, byte[] desKey, byte[] desIV) {    
    //Create the file streams to handle the input and output files.
    FileStream fin = new FileStream(inName, FileMode.Open, FileAccess.Read);
    FileStream fout = new FileStream(outName, FileMode.OpenOrCreate, FileAccess.Write);
    fout.SetLength(0); //Create variables to help with read and write.
    byte[] bin = new byte[100]; //This is intermediate storage for the encryption.
    long rdlen = 0;              //This is the total number of bytes written.
    long totlen = fin.Length;    //This is the total length of the input file.
    int len;                     //This is the number of bytes to be written at a time. DES des = new DESCryptoServiceProvider();          
    CryptoStream encStream = new CryptoStream(fout, des.CreateEncryptor(desKey, desIV), CryptoStreamMode.Write);

    Console.WriteLine("Encrypting..."); //Read from the input file, then encrypt and write to the output file.
    while(rdlen < totlen)
    {
     len = fin.Read(bin, 0, 100);
     encStream.Write(bin, 0, len);
     rdlen = rdlen + len;
     Console.WriteLine("{0} bytes processed", rdlen);
    } encStream.Close();  
    fout.Close();
    fin.Close();  
    }
    }Decrypt.csusing System;
    using System.Security.Cryptography;
    using System.IO;public class Decrypt{
    public static void Main(String[] args) {
    byte[] deskey = {198,125,20,111,77,71,228,9};
    byte[] desiv = {223,20,19,27,226,119,149,85};
    EncryptData("./jm.des","./jm1.txt",deskey,desiv);
    }
    private static void EncryptData(String inName, String outName, byte[] desKey, byte[] desIV) {    
    //Create the file streams to handle the input and output files.
    FileStream fin = new FileStream(inName, FileMode.Open, FileAccess.Read);
    FileStream fout = new FileStream(outName, FileMode.OpenOrCreate, FileAccess.Write);
    fout.SetLength(0); //Create variables to help with read and write.
    byte[] bin = new byte[100]; //This is intermediate storage for the encryption.
    long rdlen = 0;              //This is the total number of bytes written.
    long totlen = fin.Length;    //This is the total length of the input file.
    int len;                     //This is the number of bytes to be written at a time. DES des = new DESCryptoServiceProvider();          
    CryptoStream encStream = new CryptoStream(fout, des.CreateDecryptor(desKey, desIV), CryptoStreamMode.Write);

    Console.WriteLine("Encrypting..."); //Read from the input file, then encrypt and write to the output file.
    while(rdlen < totlen)
    {
     len = fin.Read(bin, 0, 100);
     encStream.Write(bin, 0, len);
     rdlen = rdlen + len;
     Console.WriteLine("{0} bytes processed", rdlen);
    } encStream.Close();  
    fout.Close();
    fin.Close();  
    }
    }你可以在jm.txt写入中英文.jm.des是加密文件.jm1.txt是解密后的结果.你可以与jm.txt比较下.
      

  6.   

    在WEB中调试出现:怎么办
    Specified key is not a valid size for this algorithm.