论坛的各位大神,帮个忙。我想通过DES加密来存储我的各种密码~我想用一个固定的Key来加密字符。问题来了
        public string GenerateKey()
        {
            DESCryptoServiceProvider desCrypto = (DESCryptoServiceProvider)DESCryptoServiceProvider.Create();
            return ASCIIEncoding.ASCII.GetString(desCrypto.Key);
        }
每次的Key都是变化的,怎么把我的口令(例如 hack+=3? )转换为Key?我试过强制转换,但是不知怎么操作。大神们,求解!!!求具体做法!!

解决方案 »

  1.   

    using System;
    using System.IO;
    using System.Text;
    using System.Security.Cryptography;class CryptoMemoryStream
    {
    // Main method.
        public static void Main()
        {
            // Create a new DES key.
            DESCryptoServiceProvider key = new DESCryptoServiceProvider();        // Encrypt a string to a byte array.
            byte[] buffer = Encrypt("This is some plaintext!", key);        // Decrypt the byte array back to a string.
            string plaintext =  Decrypt(buffer, key);        // Display the plaintext value to the console.
            Console.WriteLine(plaintext);
        }
        
    // Encrypt the string.
        public static byte[] Encrypt(string PlainText, SymmetricAlgorithm key)
        {
            // Create a memory stream.
            MemoryStream ms = new MemoryStream();        // Create a CryptoStream using the memory stream and the 
            // CSP DES key.  
            CryptoStream encStream = new CryptoStream(ms, key.CreateEncryptor(), CryptoStreamMode.Write);        // Create a StreamWriter to write a string
            // to the stream.
            StreamWriter sw = new StreamWriter(encStream);        // Write the plaintext to the stream.
            sw.WriteLine(PlainText);        // Close the StreamWriter and CryptoStream.
            sw.Close();
            encStream.Close();        // Get an array of bytes that represents
            // the memory stream.
            byte[] buffer = ms.ToArray();        // Close the memory stream.
            ms.Close();        // Return the encrypted byte array.
            return buffer;
        }   // Decrypt the byte array.
        public static string Decrypt(byte[] CypherText, SymmetricAlgorithm key)
        {
            // Create a memory stream to the passed buffer.
            MemoryStream ms = new MemoryStream(CypherText);        // Create a CryptoStream using the memory stream and the 
            // CSP DES key. 
            CryptoStream encStream = new CryptoStream(ms, key.CreateDecryptor(), CryptoStreamMode.Read);        // Create a StreamReader for reading the stream.
            StreamReader sr = new StreamReader(encStream);        // Read the stream as a string.
            string val = sr.ReadLine();        // Close the streams.
            sr.Close();
            encStream.Close();
            ms.Close();
                
            return val;
        }
    }
      

  2.   

     byte[] key = { 0x12, 0x34, 0x56, 0x78, 0x90, 0xAB, 0xCD, 0xEF }
     这种格式
      

  3.   


    这样Key就可以用来加密string了吗?byte[] 里的数组可以自定义吧?
      

  4.   

       /// <summary> 
            /// 加密字符串 
            /// 注意:密钥必须为8位 
            /// </summary> 
            /// <param name="strText">字符串</param> 
            /// <param name="encryptKey">密钥</param>
            /// <return>加密后字符串</return>
            public static string DesEncrypt(string strText) 
            {
                string outString="";
                byte[] byKey=null; 
                byte[] IV= {0x12, 0x34, 0x56, 0x78, 0x90, 0xAB, 0xCD, 0xEF}; 
                try 
                {
                    byKey = System.Text.Encoding.UTF8.GetBytes(encryptKey.Substring(0, encryptKey.Length)); 
                    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();
                    outString = Convert.ToBase64String(ms.ToArray()); 
                } 
                catch(System.Exception) 
                {
                    outString = ""; 
                }            return outString;
            }         /// <summary> 
            /// 解密字符串 
            /// </summary> 
            /// <param name="strText">加了密的字符串</param> 
            /// <param name="decryptKey">密钥</param> 
            public static string DesDecrypt(string strText) 
            {
                string outString = "";
                byte[] byKey = null; 
                byte[] IV= {0x12, 0x34, 0x56, 0x78, 0x90, 0xAB, 0xCD, 0xEF};
                byte[] inputByteArray = new Byte[strText.Length]; 
                try 
                {
                    byKey = System.Text.Encoding.UTF8.GetBytes(decryptKey.Substring(0, decryptKey.Length)); 
                    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(); 
                    System.Text.Encoding encoding = new System.Text.UTF8Encoding(); 
                    outString = encoding.GetString(ms.ToArray()); 
                } 
                catch(System.Exception) 
                {
                    outString = ""; 
                }            return outString;
            }         #endregion