小弟我要用DES算法解密配置文件,然后把解密后的内容写入内存中,请问怎么做,最好给出代码

解决方案 »

  1.   

    你是要写内存的代码还是DES算法的代码?
    DES算法就是调用方法返回值就行了c#是一般不能直接操作内存的
      

  2.   

    private static readonly string DESKey = "ABCDEFGH";//这是加密算法的密钥,对于Key的要求是:长度只能是八个字节.
            private static readonly string DESIV = "HGFEDCBA";//这是初始化向量,对于IV的要求是:长度也只能是八个字节.        /// <summary>
            /// 传递要加密的文本,返回加密之后的文本,即密文
            /// </summary>
            /// <param name="aBeforeString"></param>
            /// <returns></returns>
            public static string Encrypt(string aBeforeString)
            {
                byte[] arrDESKey;
                byte[] arrDESIV;
                MemoryStream ms = new MemoryStream();//这是采用内存流的方式加密文本,加密后直接以字节流的方式返回密文.
                DESCryptoServiceProvider objDES;
                ICryptoTransform objEncryptor;
                CryptoStream objCryptoStream;
                arrDESKey = Convert2ByteArray(DESKey);
                arrDESIV = Convert2ByteArray(DESIV);
                objDES = new DESCryptoServiceProvider();
                objEncryptor = objDES.CreateEncryptor(arrDESKey, arrDESIV);
                objCryptoStream = new CryptoStream(ms, objEncryptor, CryptoStreamMode.Write);
                StreamWriter sw = new StreamWriter(objCryptoStream);
                sw.Write(aBeforeString);
                // Close the StreamWriter and CryptoStream.
                sw.Close();
                objCryptoStream.Close();
                // Get an array of bytes that represents
                // the memory stream.
                byte[] buffer = ms.ToArray();
                // Close the memory stream.
                ms.Close();
                string aString = "";
                for (int i = 0; i < buffer.Length; i++)
                {
                    aString = aString + BitConverter.ToChar(buffer, i).ToString();//在这里对于密文//做了进一步的处理,将相临的两个字节组合成一个16位的Unicode字符
                    i = i + 1;
                }
                return aString;
            }        public static string Decrypt(string aAferterString)//解密密文,返回原文本.
            {
                byte[] arrDESKey;
                byte[] arrDESIV;
                DESCryptoServiceProvider objDES;
                ICryptoTransform objDecryptor;
                CryptoStream objCryptoStream;
                MemoryStream ms = new MemoryStream(ConvertStringTOByte(aAferterString));
                arrDESKey = Convert2ByteArray(DESKey);
                arrDESIV = Convert2ByteArray(DESIV);
                objDES = new DESCryptoServiceProvider();
                objDecryptor = objDES.CreateDecryptor(arrDESKey, arrDESIV);
                objCryptoStream = new CryptoStream(ms, objDecryptor, CryptoStreamMode.Read);//解密是采取和加密相反的方式进行解密.
                StreamReader sr = new StreamReader(objCryptoStream);
                // Read the stream as a string.
                string val = sr.ReadToEnd();
                // Close the streams.
                sr.Close();
                objCryptoStream.Close();
                ms.Close();
                //objFileStream.Close();
                return val;
            }
      

  3.   

    估计要调用windows API 中 WriteProcessMemory来处理了。