示例#1
0
文件: Cryptor.cs 项目: pngwei/TEST
        /// <summary>
        /// 解密数据
        /// </summary>
        /// <param name="toDecryptText">待解密的文本</param>
        /// <returns>解密的文本</returns>
        public static string Decrypt(string toDecryptText)
        {
            UnicodeEncoding textConverter = new UnicodeEncoding();

            byte[] fromEncrypt;
            byte[] encrypted;
            //Get a decryptor that uses the same key and IV as the encryptor.
            ICryptoTransform decryptor = rc2CSP.CreateDecryptor(key, IV);

            //encrypted = textConverter.GetBytes(toDecryptText);
            encrypted = TextService.BufferFromHexStr(toDecryptText, false);
            if (encrypted == null)
            {
                return(toDecryptText);
            }
            //Now decrypt the previously encrypted message using the decryptor
            // obtained in the above step.
            MemoryStream msDecrypt = new MemoryStream(encrypted);
            CryptoStream csDecrypt = new CryptoStream(msDecrypt, decryptor, CryptoStreamMode.Read);

            fromEncrypt = new byte[encrypted.Length];
            try
            {
                //Read the data oGX of the crypto stream.
                int count = csDecrypt.Read(fromEncrypt, 0, fromEncrypt.Length);
                //Convert the byte array back into a string.
                return(textConverter.GetString(fromEncrypt, 0, count));
            }
            catch
            {
                return(toDecryptText);
            }
        }
示例#2
0
文件: Cryptor.cs 项目: pngwei/TEST
        /// <summary>
        /// 加密数据
        /// </summary>
        /// <param name="toEncryptText">待加密的文本</param>
        /// <returns>经过加密的文本</returns>
        public static string Encrypt(string toEncryptText)
        {
            UnicodeEncoding textConverter = new UnicodeEncoding();

            byte[] encrypted;
            byte[] toEncrypt;
            //Get an encryptor.
            ICryptoTransform encryptor = rc2CSP.CreateEncryptor(key, IV);

            //Encrypt the data.
            MemoryStream msEncrypt = new MemoryStream();
            CryptoStream csEncrypt = new CryptoStream(msEncrypt, encryptor, CryptoStreamMode.Write);

            //Convert the data to a byte array.
            toEncrypt = textConverter.GetBytes(toEncryptText);

            //Write all data to the crypto stream and flush it.
            csEncrypt.Write(toEncrypt, 0, toEncrypt.Length);
            csEncrypt.FlushFinalBlock();

            //Get encrypted array of bytes.
            encrypted = msEncrypt.ToArray();

            //return textConverter.GetString(encrypted);
            return(TextService.BufferToHexStr(encrypted, 0, encrypted.Length));
        }