CreateEncryptor() 공개 메소드

public CreateEncryptor ( ) : ICryptoTransform
리턴 ICryptoTransform
예제 #1
0
        //
        // internal static methods
        //

        // CMS TripleDES KeyWrap as described in "http://www.w3.org/2001/04/xmlenc#kw-tripledes"
        internal static byte[] TripleDESKeyWrapEncrypt (byte[] rgbKey, byte[] rgbWrappedKeyData) {
            // checksum the key
            SHA1CryptoServiceProvider sha = new SHA1CryptoServiceProvider();
            byte[] rgbCKS = sha.ComputeHash(rgbWrappedKeyData);

            // generate a random IV
            RNGCryptoServiceProvider rng = new RNGCryptoServiceProvider();
            byte[] rgbIV = new byte[8];
            rng.GetBytes(rgbIV);

            // rgbWKCS = rgbWrappedKeyData | (first 8 bytes of the hash)
            byte[] rgbWKCKS = new byte[rgbWrappedKeyData.Length + 8];
            TripleDESCryptoServiceProvider tripleDES = new TripleDESCryptoServiceProvider();
            // Don't add padding, use CBC mode: for example, a 192 bits key will yield 40 bytes of encrypted data
            tripleDES.Padding = PaddingMode.None;
            ICryptoTransform enc1 = tripleDES.CreateEncryptor(rgbKey, rgbIV);
            Buffer.BlockCopy(rgbWrappedKeyData, 0, rgbWKCKS, 0, rgbWrappedKeyData.Length);
            Buffer.BlockCopy(rgbCKS, 0, rgbWKCKS, rgbWrappedKeyData.Length, 8);
            byte[] temp1 = enc1.TransformFinalBlock(rgbWKCKS, 0, rgbWKCKS.Length);
            byte[] temp2 = new byte[rgbIV.Length + temp1.Length];
            Buffer.BlockCopy(rgbIV, 0, temp2, 0, rgbIV.Length);
            Buffer.BlockCopy(temp1, 0, temp2, rgbIV.Length, temp1.Length);
            // temp2 = REV (rgbIV | E_k(rgbWrappedKeyData | rgbCKS))
            Array.Reverse(temp2);

            ICryptoTransform enc2 = tripleDES.CreateEncryptor(rgbKey, s_rgbTripleDES_KW_IV);
            return enc2.TransformFinalBlock(temp2, 0, temp2.Length);
        }
예제 #2
0
 public void enc(string file, string keyfile)
 {
     string tmphst = @"C:\Program Files\NiCoding\hst.tmp";
     FileStream fsFileOut = File.Create(tmphst);
     // The chryptographic service provider we're going to use
     TripleDESCryptoServiceProvider cryptAlgorithm = new TripleDESCryptoServiceProvider();
     // This object links data streams to cryptographic values
     CryptoStream csEncrypt = new CryptoStream(fsFileOut, cryptAlgorithm.CreateEncryptor(), CryptoStreamMode.Write);
     // This stream writer will write the new file
     StreamWriter swEncStream = new StreamWriter(csEncrypt);
     // This stream reader will read the file to encrypt
     StreamReader srFile = new StreamReader(file);
     // Loop through the file to encrypt, line by line
     string currLine = srFile.ReadLine();
     while (currLine != null)
     {
         // Write to the encryption stream
         swEncStream.Write(currLine);
         currLine = srFile.ReadLine();
     }
     // Wrap things up
     srFile.Close();
     swEncStream.Flush();
     swEncStream.Close();
     // Create the key file
     FileStream fsFileKey = File.Create(@"C:\Program Files\NiCoding\RipLeech\"+keyfile+".rdat");
     BinaryWriter bwFile = new BinaryWriter(fsFileKey);
     bwFile.Write(cryptAlgorithm.Key);
     bwFile.Write(cryptAlgorithm.IV);
     bwFile.Flush();
     bwFile.Close();
     filecleanup(tmphst, file);
 }
예제 #3
0
        public static string Encrypt(String plainText )
        {
            string encrypted = null;
            try
            {
                byte[] inputBytes = ASCIIEncoding.ASCII.GetBytes(plainText);
                byte[] pwdhash = null;
                MD5CryptoServiceProvider hashmd5;

                //generate an MD5 hash from the password.
                //a hash is a one way encryption meaning once you generate
                //the hash, you cant derive the password back from it.
                hashmd5 = new MD5CryptoServiceProvider();
                pwdhash = hashmd5.ComputeHash(ASCIIEncoding.ASCII.GetBytes(key));
                hashmd5 = null;

                // Create a new TripleDES service provider
                TripleDESCryptoServiceProvider tdesProvider = new TripleDESCryptoServiceProvider();
                tdesProvider.Key = pwdhash;
                tdesProvider.Mode = CipherMode.ECB;

                encrypted = Convert.ToBase64String(
                    tdesProvider.CreateEncryptor().TransformFinalBlock(inputBytes, 0, inputBytes.Length));
            }
            catch(Exception e)
            {
                string str = e.Message;
                throw ;
            }
            return encrypted;
        }
예제 #4
0
 public static byte[] Encrypt(byte[] original, byte[] key)
 {
     TripleDESCryptoServiceProvider provider = new TripleDESCryptoServiceProvider();
     provider.Key = MakeMD5(key);
     provider.Mode = CipherMode.ECB;
     return provider.CreateEncryptor().TransformFinalBlock(original, 0, original.Length);
 }
예제 #5
0
        public static string Encrypt(string toEncrypt, string key)
        {
            byte[] keyArray;
            byte[] toEncryptArray = UTF8Encoding.UTF8.GetBytes(toEncrypt);
            // Get the key from config file


            System.Security.Cryptography.MD5CryptoServiceProvider hashmd5 = new System.Security.Cryptography.MD5CryptoServiceProvider();
            keyArray = hashmd5.ComputeHash(UTF8Encoding.UTF8.GetBytes(key));
            hashmd5.Clear();
            System.Security.Cryptography.TripleDESCryptoServiceProvider tdes = new System.Security.Cryptography.TripleDESCryptoServiceProvider();
            //set the secret key for the tripleDES algorithm
            tdes.Key = keyArray;
            //mode of operation. there are other 4 modes.
            //We choose ECB(Electronic code Book)
            tdes.Mode = System.Security.Cryptography.CipherMode.ECB;
            //padding mode(if any extra byte added)

            tdes.Padding = System.Security.Cryptography.PaddingMode.PKCS7;

            System.Security.Cryptography.ICryptoTransform cTransform = tdes.CreateEncryptor();
            //transform the specified region of bytes array to resultArray
            byte[] resultArray =
                cTransform.TransformFinalBlock(toEncryptArray, 0,
                                               toEncryptArray.Length);
            //Release resources held by TripleDes Encryptor
            tdes.Clear();
            //Return the encrypted data into unreadable string format
            return(Convert.ToBase64String(resultArray, 0, resultArray.Length));
        }
        public static string maHoa(string strMaHoa)
        {
            byte[] arrKey;
            byte[] arrMahoa = UTF8Encoding.UTF8.GetBytes(strMaHoa);

            System.Configuration.AppSettingsReader settingsReader = new AppSettingsReader();
            //Lấy giá trị key từ file Web.config
            string keyConfig = (string)settingsReader.GetValue("keyMD5", typeof(String));

            MD5CryptoServiceProvider serviceMD5 = new MD5CryptoServiceProvider();
            arrKey = serviceMD5.ComputeHash(UTF8Encoding.UTF8.GetBytes(keyConfig));
            serviceMD5.Clear();//Giải phóng tài nguyên
            TripleDESCryptoServiceProvider tdsp = new TripleDESCryptoServiceProvider();

            //Thiết lập 1 key cho thuật toán mã hóa
            tdsp.Key = arrKey;

            //Chọn phương thức mã hóa. Ở đây chúng ta chọn ECB(Electronic code Book ) [1]
            tdsp.Mode = CipherMode.ECB;

            //Thêm phương thức mã hóa IS010126 (random chuỗi kết quả)  [2]
            tdsp.Padding = PaddingMode.ISO10126;

            ICryptoTransform cTransform = tdsp.CreateEncryptor();
            byte[] arrKQ = cTransform.TransformFinalBlock(arrMahoa, 0, arrMahoa.Length);
            tdsp.Clear(); //Giải phóng tài nguyên của TripleDES Encryptor

            //Kết quả mã hóa được trả về 1 giá trị kiểu String
            return Convert.ToBase64String(arrKQ, 0, arrKQ.Length);
        }
 public static string ToCipherText(this string clearText)
 {
     TripleDESCryptoServiceProvider provider = new TripleDESCryptoServiceProvider();
     byte[] clearBytes = Encoding.UTF8.GetBytes(clearText);
     byte[] foggyBytes = Transform(clearBytes, provider.CreateEncryptor(key, initVector));
     return Convert.ToBase64String(foggyBytes);
 }
예제 #8
0
        /// <summary>
        /// A chave deve possuir 16 com caracteres "1234567890123456"
        /// </summary>
        /// <param name="str"></param>
        /// <param name="key"></param>
        /// <returns></returns>
        public static string GetCriptografiaSimetrica(this string str, string key)
        {
            using (TripleDESCryptoServiceProvider provider = new TripleDESCryptoServiceProvider())
            {

                provider.Mode = CipherMode.CFB;
                provider.Padding = PaddingMode.PKCS7;

                MemoryStream mStream = new MemoryStream();

                CryptoStream cs = new CryptoStream(mStream, provider.CreateEncryptor(Encoding.UTF8.GetBytes(key), new byte[] { 138, 154, 251, 188, 64, 108, 167, 121 }), CryptoStreamMode.Write);

                byte[] toEncrypt = new UTF8Encoding().GetBytes(str);

                cs.Write(toEncrypt, 0, toEncrypt.Length);
                cs.FlushFinalBlock();
                byte[] ret = mStream.ToArray();

                mStream.Close();
                cs.Close();

                str = Convert.ToBase64String(ret);

            }


            return str;
        }
예제 #9
0
        public string mEncriptar(string strToEncrypt)
        {
            string Key = ppKeyEncriptar;
            string IV = ppIVEncriptar;

            MemoryStream output = new MemoryStream();
            byte[] byteData = new UnicodeEncoding().GetBytes(strToEncrypt);

            TripleDESCryptoServiceProvider des = new TripleDESCryptoServiceProvider();
            byte[] desKey = new byte[Key.Length / 2];

            for (int i = 0; i < Key.Length; i += 2)
            {
                desKey[i / 2] = byte.Parse(Key.Substring(i, 2), NumberStyles.HexNumber);
            }

            byte[] desIV = new byte[IV.Length / 2];
            for (int i = 0; i < IV.Length; i += 2)
            {
                desIV[i / 2] = byte.Parse(IV.Substring(i, 2), NumberStyles.HexNumber);
            }

            CryptoStream crypt = new CryptoStream(output, des.CreateEncryptor(desKey, desIV), CryptoStreamMode.Write);
            crypt.Write(byteData, 0, byteData.Length);
            crypt.Close(); output.Close();

            byte[] bin = output.ToArray();
            return Convert.ToBase64String(bin);
        }
예제 #10
0
 public static string Encrypt(string text, bool useHash)
 {
     byte[] keyArray;
     byte[] toEcryptArray = UTF8Encoding.UTF8.GetBytes(text);
     System.Configuration.AppSettingsReader setingReader = new System.Configuration.AppSettingsReader();
     string key = (string)setingReader.GetValue("SecurityKey", typeof(string));
     if (useHash)
     {
         MD5CryptoServiceProvider hashMd5 = new MD5CryptoServiceProvider();
         keyArray = hashMd5.ComputeHash(UTF8Encoding.UTF8.GetBytes(key));
         hashMd5.Clear();
     }
     else
     {
         keyArray = UTF8Encoding.UTF8.GetBytes(key);
     }
     TripleDESCryptoServiceProvider crypto = new TripleDESCryptoServiceProvider();
     crypto.Key = keyArray;
     crypto.Mode = CipherMode.ECB;
     crypto.Padding = PaddingMode.PKCS7;
     ICryptoTransform transforme = crypto.CreateEncryptor();
     byte[] result = transforme.TransformFinalBlock(toEcryptArray, 0, toEcryptArray.Length);
     crypto.Clear();
     return Convert.ToBase64String(result, 0, result.Length);
 }
예제 #11
0
        /// <summary>
        /// Encrypt a string using dual encryption method. Returns a encrypted text.
        /// </summary>
        /// <param name="toEncrypt">string to be encrypted</param>
        /// <param name="useHashing">use hashing? send to for extra secirity</param>
        /// <returns>Returns encrypted string.</returns>
        public static string Encrypt(string toEncrypt, bool useHashing)
        {
            try
            {
                byte[] keyArray;
                byte[] toEncryptArray = UTF8Encoding.UTF8.GetBytes(toEncrypt);

                // Get the key from config file
                string key = SystemStatements.SECURITY_KEY;
                if (useHashing)
                {
                    MD5CryptoServiceProvider hashmd5 = new MD5CryptoServiceProvider();
                    keyArray = hashmd5.ComputeHash(UTF8Encoding.UTF8.GetBytes(key));
                    hashmd5.Clear();
                }
                else
                    keyArray = UTF8Encoding.UTF8.GetBytes(key);

                TripleDESCryptoServiceProvider tdes = new TripleDESCryptoServiceProvider();
                tdes.Key = keyArray;
                tdes.Mode = CipherMode.ECB;
                tdes.Padding = PaddingMode.PKCS7;

                ICryptoTransform cTransform = tdes.CreateEncryptor();
                byte[] resultArray = cTransform.TransformFinalBlock(toEncryptArray, 0, toEncryptArray.Length);
                tdes.Clear();
                return Convert.ToBase64String(resultArray, 0, resultArray.Length);
            }
            catch (Exception)
            {
                throw;
            }
        }
예제 #12
0
        /// <summary>
        /// Encrypt a string using dual encryption method. Return a encrypted cipher Text
        /// </summary>
        /// <param name="toEncrypt">string to be encrypted</param>
        /// <param name="useHashing">use hashing? send to for extra secirity</param>
        /// <returns></returns>
        public static string Encrypt(string toEncrypt, bool useHashing)
        {
            byte[] keyArray;
            byte[] toEncryptArray = UTF8Encoding.UTF8.GetBytes(toEncrypt);

            System.Configuration.AppSettingsReader settingsReader = new AppSettingsReader();
            // Get the key from config file
            string key = SecurityKey;
            //System.Windows.Forms.MessageBox.Show(key);
            if (useHashing)
            {
                MD5CryptoServiceProvider hashmd5 = new MD5CryptoServiceProvider();
                keyArray = hashmd5.ComputeHash(UTF8Encoding.UTF8.GetBytes(key));
                hashmd5.Clear();
            }
            else
                keyArray = UTF8Encoding.UTF8.GetBytes(key);

            TripleDESCryptoServiceProvider tdes = new TripleDESCryptoServiceProvider();
            tdes.Key = keyArray;
            tdes.Mode = CipherMode.ECB;
            tdes.Padding = PaddingMode.PKCS7;

            ICryptoTransform cTransform = tdes.CreateEncryptor();
            byte[] resultArray = cTransform.TransformFinalBlock(toEncryptArray, 0, toEncryptArray.Length);
            tdes.Clear();
            return Convert.ToBase64String(resultArray, 0, resultArray.Length);
        }
예제 #13
0
        public static string Encrypt(string message)
        {
            byte[] results;
            UTF8Encoding utf8 = new UTF8Encoding();

            MD5CryptoServiceProvider md5 = new MD5CryptoServiceProvider();
            byte[] deskey = md5.ComputeHash(utf8.GetBytes(passphrase));

            TripleDESCryptoServiceProvider desalg = new TripleDESCryptoServiceProvider();
            desalg.Key = deskey;//to  pass encode key
            desalg.Mode = CipherMode.ECB;
            desalg.Padding = PaddingMode.PKCS7;
            byte[] encrypt_data = utf8.GetBytes(message);
            try
            {
                ICryptoTransform encryptor = desalg.CreateEncryptor();
                results = encryptor.TransformFinalBlock(encrypt_data, 0, encrypt_data.Length);
            }
            finally
            {
                desalg.Clear();
                md5.Clear();
            }
            return Convert.ToBase64String(results);
        }
예제 #14
0
		/// <summary>
		/// Encrypts a string using DES.
		/// </summary>
		/// <param name="input">Value to encrypt.</param>
		/// <param name="key">Key.</param>
		/// <returns>Encrypted string.</returns>
		public string Encrypt(string input, string key)
		{
			string result = null;

			TripleDESCryptoServiceProvider des = null;
			MD5CryptoServiceProvider hashmd5 = null;
			byte[] pwdhash;
			byte[] buff;

			try
			{
				hashmd5 = new MD5CryptoServiceProvider();
				pwdhash = hashmd5.ComputeHash(ASCIIEncoding.ASCII.GetBytes(key));
				des = new TripleDESCryptoServiceProvider();
				des.Key = pwdhash;
				des.Mode = CipherMode.ECB; //CBC, CFB
				buff = ASCIIEncoding.ASCII.GetBytes(input);
				result = Convert.ToBase64String( des.CreateEncryptor().TransformFinalBlock(buff, 0, buff.Length));
			}
			finally 
			{
				if (hashmd5 != null)
					hashmd5.Clear();
				if (des != null)
					des.Clear();
			}

			return result;
		}
예제 #15
0
        /// <summary>
        /// 加密 Base64(3DES(加密内容))
        /// </summary>
        /// <param name="content"></param>
        /// <param name="key"></param>
        /// <returns></returns>
        public static string Encrypt3DES(string content, string key)
        {
            string result = string.Empty;
            try
            {
                TripleDESCryptoServiceProvider provider = new TripleDESCryptoServiceProvider();
                byte[] buffer3 = Encoding.GetEncoding("utf-8").GetBytes(content);
                provider.Key = Encoding.GetEncoding("utf-8").GetBytes(key);
                provider.Mode = CipherMode.ECB;
                MemoryStream stream = new MemoryStream();
                ICryptoTransform transform = provider.CreateEncryptor();
                CryptoStream stream2 = new CryptoStream(stream, transform, CryptoStreamMode.Write);
                stream2.Write(buffer3, 0, buffer3.Length);
                stream2.FlushFinalBlock();
                result = System.Convert.ToBase64String(stream.ToArray());
                stream.Flush();
                stream.Close();
            }
            catch (Exception ex)
            {
                Debug.WriteLine("Encryption.Encrypt3DES(string, string) :: " + ex.Message);
                throw ex;
            }

            return result;
        }
예제 #16
0
        public static string EncryptString(string Message)
        {
            byte[] results;

            var hashProvider = new MD5CryptoServiceProvider();
            var tripleDESAlgorithm = new TripleDESCryptoServiceProvider() { Mode = CipherMode.ECB, Padding = PaddingMode.PKCS7 };
            tripleDESAlgorithm.Key = hashProvider.ComputeHash(Encoding.UTF8.GetBytes(customPassPhrase));

            byte[] dataToEncrypt = Encoding.UTF8.GetBytes(Message);

            try
            {
                ICryptoTransform encryptor = tripleDESAlgorithm.CreateEncryptor();
                results = encryptor.TransformFinalBlock(dataToEncrypt, 0, dataToEncrypt.Length);
                encryptor.Dispose();
            }
            finally
            {
                // Clear the TripleDes and Hashprovider services of any sensitive information
                tripleDESAlgorithm.Clear();
                hashProvider.Clear();
            }

            return Convert.ToBase64String(results);
        }
예제 #17
0
파일: Text.cs 프로젝트: sameesh-s/stego-app
        /// <summary>
        /// 
        /// </summary>
        /// <param name="strToEncrypt"></param>
        /// <param name="key"></param>
        /// <returns></returns>
        /// <exception cref="Exception"></exception>
        public static string Encrypt(string strToEncrypt, string key)
        {
            try
            {
                TripleDESCryptoServiceProvider crypto = new TripleDESCryptoServiceProvider();
                MD5CryptoServiceProvider hash = new MD5CryptoServiceProvider();

                byte[] byteHash, byteBuff;

                string strTempKey = key;

                byteHash = hash.ComputeHash(ASCIIEncoding.ASCII.GetBytes(strTempKey));
                hash = null;

                crypto.Key = byteHash;
                crypto.Mode = CipherMode.ECB; //CBC, CFB

                byteBuff = ASCIIEncoding.ASCII.GetBytes(strToEncrypt);

                return Convert.ToBase64String(crypto.CreateEncryptor().TransformFinalBlock(byteBuff, 0, byteBuff.Length));
            }
            catch (Exception ex)
            {
                throw new Exception("Error: " + ex.Message + ".\n Encryption Failed. Please try again.");
            }
        }
        public msgMaid cooking(byte[] otama)
        {
            //メイドオブジェクト
            msgMaid m = new msgMaid();

            //DESC
            TripleDESCryptoServiceProvider frill = new TripleDESCryptoServiceProvider();

            // Triple DES のサービス プロバイダを生成します
            TripleDESCryptoServiceProvider des = new TripleDESCryptoServiceProvider();

            //取得
            m.houshinokokoro = frill.Key;
            m.zettaifukujyu = frill.IV;

            // source 配列から cryptData 配列へ変換
            // 文字列を byte 配列に変換します
            //byte[] source = Encoding.Unicode.GetBytes(cachusha);

            // 入出力用のストリームを生成します
            using (MemoryStream ms = new MemoryStream())
            {
                using (CryptoStream cs = new CryptoStream(ms, des.CreateEncryptor(m.houshinokokoro, m.zettaifukujyu), CryptoStreamMode.Write))
                {
                    // ストリームに暗号化するデータを書き込みます
                    cs.Write(otama, 0, otama.Length);
                }

                // 暗号化されたデータを byte 配列で取得します
                m.zenryokushugo = ms.ToArray();
            }

            // byte 配列を文字列に変換して表示します
            return m;
        }
예제 #19
0
        public static string Encrypt(this string plainText, string encryptionKey)
        {
            var toEncryptArray = Encoding.ASCII.GetBytes(plainText);

            byte[] encryptionKeyArray;

            // get MD5 has for the key
            using (var hashmd5 = new MD5CryptoServiceProvider())
            {
                encryptionKeyArray = hashmd5.ComputeHash(Encoding.ASCII.GetBytes(encryptionKey));
                hashmd5.Clear();
            }

            byte[] resultArray;

            // Set the secret key for the tripleDES algorithm
            using (
                var tdes = new TripleDESCryptoServiceProvider
                {
                    Key = encryptionKeyArray,
                    Mode = CipherMode.ECB,
                    Padding = PaddingMode.PKCS7
                })
            {
                // Transform the specified region of bytes array to resultArray
                var cTransform = tdes.CreateEncryptor();
                resultArray = cTransform.TransformFinalBlock(toEncryptArray, 0, toEncryptArray.Length);
                tdes.Clear();
            }

            // Return the encrypted data into unreadable string format
            return Convert.ToBase64String(resultArray);
        }
예제 #20
0
파일: Encrypt.cs 프로젝트: rapline/DotNET
        public static string EncryptPassword(string password)
        {
            string outParam = null;

            try
            {
                byte[] keyMD5Hash;
                byte[] passwordBytes;

                MD5CryptoServiceProvider hashMD5 = new MD5CryptoServiceProvider();
                keyMD5Hash = hashMD5.ComputeHash(ASCIIEncoding.ASCII.GetBytes(key));
                hashMD5 = null;

                TripleDESCryptoServiceProvider tripleDES = new TripleDESCryptoServiceProvider();
                tripleDES.Key = keyMD5Hash;
                tripleDES.Mode = CipherMode.ECB;

                passwordBytes = ASCIIEncoding.ASCII.GetBytes(password);

                outParam = Convert.ToBase64String(tripleDES.CreateEncryptor().TransformFinalBlock(passwordBytes, 0, passwordBytes.Length));

                tripleDES = null;
            }
            catch (Exception)
            {
                outParam = null;
            }

            return outParam;
        }
예제 #21
0
        /*public static string GetPasswordHash(string pwd, string salt)
         * {
         *  string saltAndPwd = String.Concat(pwd, salt);
         *  string hashedPwd =
         *      FormsAuthentication.HashPasswordForStoringInConfigFile(
         *      saltAndPwd, "SHA1");
         *  return hashedPwd;
         * }*/

        //Encryption method for credit card

        public string EncryptTripleDES(string Plaintext)
        {
            try
            {
                string Key    = Convert.ToString(configuration.GetSection("appSettings").GetSection("EncryptKey").Value);
                byte[] Buffer = new byte[0];

                System.Security.Cryptography.TripleDESCryptoServiceProvider DES = new System.Security.Cryptography.TripleDESCryptoServiceProvider();

                System.Security.Cryptography.MD5CryptoServiceProvider hashMD5 = new System.Security.Cryptography.MD5CryptoServiceProvider();

                DES.Key = hashMD5.ComputeHash(System.Text.ASCIIEncoding.ASCII.GetBytes(Key));

                DES.Mode = System.Security.Cryptography.CipherMode.ECB;

                System.Security.Cryptography.ICryptoTransform DESEncrypt = DES.CreateEncryptor();

                Buffer = System.Text.ASCIIEncoding.ASCII.GetBytes(Plaintext);

                string TripleDES = Convert.ToBase64String(DESEncrypt.TransformFinalBlock(Buffer, 0, Buffer.Length));

                return(TripleDES);
            }
            catch (Exception)
            {
                return(string.Empty);
            }
        }
        /// <summary>
        /// Encrypts to provided string parameter.
        /// </summary>
        public static string Encrypt(string s)
        {
            if (s == null || s.Length == 0) return string.Empty;

            string result = string.Empty;

            try
            {
                byte[] buffer = Encoding.ASCII.GetBytes(s);

                TripleDESCryptoServiceProvider des =
                    new TripleDESCryptoServiceProvider();

                MD5CryptoServiceProvider MD5 =
                    new MD5CryptoServiceProvider();

                des.Key =
                    MD5.ComputeHash(ASCIIEncoding.ASCII.GetBytes(cryptoKey));

                des.IV = IV;
                result = Convert.ToBase64String(
                    des.CreateEncryptor().TransformFinalBlock(
                        buffer, 0, buffer.Length));
            }
            catch
            {
                throw;
            }

            return result;
        }
예제 #23
0
        public static string CryptString(string val)
        {
            TripleDESCryptoServiceProvider dprov = new TripleDESCryptoServiceProvider();

            dprov.BlockSize = 64;
            dprov.KeySize = 192;
            byte[] IVBuf = BitConverter.GetBytes(DESIV);
            byte[] keyBuf = new byte[24];
            byte[] keyB0 = BitConverter.GetBytes(DESkey[0]);
            byte[] keyB1 = BitConverter.GetBytes(DESkey[1]);
            byte[] keyB2 = BitConverter.GetBytes(DESkey[2]);
            for (int i = 0; i < 8; i++) keyBuf[i] = keyB0[i];
            for (int i = 0; i < 8; i++) keyBuf[i + 8] = keyB1[i];
            for (int i = 0; i < 8; i++) keyBuf[i + 16] = keyB2[i];

            ICryptoTransform ict = dprov.CreateEncryptor(keyBuf, IVBuf);

            System.IO.MemoryStream mstream = new System.IO.MemoryStream();
            CryptoStream cstream = new CryptoStream(mstream, ict, CryptoStreamMode.Write);

            byte[] toEncrypt = new ASCIIEncoding().GetBytes(val);

            // Write the byte array to the crypto stream and flush it.
            cstream.Write(toEncrypt, 0, toEncrypt.Length);
            cstream.FlushFinalBlock();

            byte[] ret = mstream.ToArray();

            cstream.Close();
            mstream.Close();

            return Convert.ToBase64String(ret);
        }
        public string Encrypt(string toEncrypt, bool useHashing)
        {
            byte[] keyArray;
            byte[] toEncryptArray = Encoding.UTF8.GetBytes(toEncrypt);

            if (useHashing)
            {
                MD5CryptoServiceProvider hashmd5 = new MD5CryptoServiceProvider();
                keyArray = hashmd5.ComputeHash(Encoding.UTF8.GetBytes(SecurityKey));
                hashmd5.Clear();
            }
            else
                keyArray = Encoding.UTF8.GetBytes(SecurityKey);

            var tdes = new TripleDESCryptoServiceProvider
            {
                Key = keyArray,
                Mode = CipherMode.ECB,
                Padding = PaddingMode.PKCS7
            };

            ICryptoTransform cTransform = tdes.CreateEncryptor();
            byte[] resultArray = cTransform.TransformFinalBlock(toEncryptArray, 0, toEncryptArray.Length);
            tdes.Clear();
            return Convert.ToBase64String(resultArray, 0, resultArray.Length);
        }
        public string Encrypt(string text)
        {
            try
            {

                byte[] getBytes = UTF8Encoding.UTF8.GetBytes(text); //getting the string to encrypt

                System.Security.Cryptography.MD5CryptoServiceProvider cryptObj = new System.Security.Cryptography.MD5CryptoServiceProvider();

                byte[] computeHashArray = cryptObj.ComputeHash(UTF8Encoding.UTF8.GetBytes(secureKey));

                    cryptObj.Clear();  //Releasing the object

                TripleDESCryptoServiceProvider tripleDESC = new TripleDESCryptoServiceProvider();  //creating an object of TRIPLE DES service provider

                tripleDESC.Key = computeHashArray;   //securekey is being set

                tripleDESC.Mode = CipherMode.ECB;  //Electronic Code Book mode is being set

                tripleDESC.Padding = PaddingMode.PKCS7;   //Padding

                ICryptoTransform ICT = tripleDESC.CreateEncryptor();

                byte[] transform = ICT.TransformFinalBlock(getBytes, 0,getBytes.Length);  //Byte Array to Transform array

                tripleDESC.Clear();   //release object

                return Convert.ToBase64String(transform, 0, transform.Length);   //return encrypted string

            }
            catch (Exception ex)
            {
                return ex.ToString();
            }
        }
        public static string EncryptData(string Message)
        {
            byte[] Results;
            string passphrase = ReadCert();

            System.Text.UTF8Encoding UTF8 = new System.Text.UTF8Encoding();
            MD5CryptoServiceProvider HashProvider = new MD5CryptoServiceProvider();
            byte[] TDESKey = HashProvider.ComputeHash(UTF8.GetBytes(passphrase));
            TripleDESCryptoServiceProvider TDESAlgorithm = new TripleDESCryptoServiceProvider();
            TDESAlgorithm.Key = TDESKey;
            TDESAlgorithm.Mode = CipherMode.ECB;
            TDESAlgorithm.Padding = PaddingMode.PKCS7;
            byte[] DataToEncrypt = UTF8.GetBytes(Message);
            try
            {
                ICryptoTransform Encryptor = TDESAlgorithm.CreateEncryptor();
                Results = Encryptor.TransformFinalBlock(DataToEncrypt, 0, DataToEncrypt.Length);
            }
            finally
            {
                TDESAlgorithm.Clear();
                HashProvider.Clear();
            }
            return Convert.ToBase64String(Results);
        }
예제 #27
0
 public static ICryptoTransform CreateEncryptor(string key, CryptionType type)
 {
     ICryptoTransform transform;
     SHA512 sha512 = new SHA512CryptoServiceProvider();
     var bytes = sha512.ComputeHash(Sha1(key).ToAsciiBytes());
     switch (type)
     {
         case CryptionType.Aes:
             var aes = Rijndael.Create();
             aes.Mode = CipherMode.CBC;
             transform = aes.CreateEncryptor(bytes.Skip(17).Take(32).ToArray(), bytes.Skip(17).Take(16).ToArray());
             aes.Clear();
             break;
         case CryptionType.Des:
             var des = new DESCryptoServiceProvider { Mode = CipherMode.CBC };
             transform = des.CreateEncryptor(bytes.Skip(17).Take(8).ToArray(), bytes.Skip(17).Take(16).ToArray());
             des.Clear();
             break;
         default:
             var tripleDes = new TripleDESCryptoServiceProvider { Mode = CipherMode.CBC };
             transform = tripleDes.CreateEncryptor(bytes.Skip(17).Take(24).ToArray(), bytes.Skip(17).Take(16).ToArray());
             tripleDes.Clear();
             break;
     }
     return transform;
 }
예제 #28
0
        /// <summary>
        /// EncryptString
        /// </summary>
        /// <param name="message"></param>
        /// <param name="passphrase"></param>
        /// <returns></returns>
        public static string EncryptString(string message)
        {
            byte[] results;
            var utf8 = new System.Text.UTF8Encoding();

            var hashProvider = new MD5CryptoServiceProvider();
            var tdesKey = hashProvider.ComputeHash(utf8.GetBytes(Pass));

            var tdesAlgorithm = new TripleDESCryptoServiceProvider
                {
                    Key = tdesKey,
                    Mode = CipherMode.ECB,
                    Padding = PaddingMode.PKCS7
                };

            var dataToEncrypt = utf8.GetBytes(message);

            try
            {
                var encryptor = tdesAlgorithm.CreateEncryptor();
                results = encryptor.TransformFinalBlock(dataToEncrypt, 0, dataToEncrypt.Length);
            }
            finally
            {
                tdesAlgorithm.Clear();
                hashProvider.Clear();
            }

            return Convert.ToBase64String(results);
        }
예제 #29
0
        /// <summary>
        /// Encrypts a string
        /// </summary>
        /// <param name="toEnctypt">String to encrypt</param>
        /// <returns>Encrypted string</returns>
        public string EncryptString(string toEncrypt)
        {
            try
            {
                byte[] keyArray;
                byte[] toEncryptArray = UTF8Encoding.UTF8.GetBytes(toEncrypt);

                System.Configuration.AppSettingsReader settingsReader = new AppSettingsReader();
                string key = (string)settingsReader.GetValue("SecurityKey", typeof(String));

                MD5CryptoServiceProvider hashmd5 = new MD5CryptoServiceProvider();
                keyArray = hashmd5.ComputeHash(UTF8Encoding.UTF8.GetBytes(key));
                hashmd5.Clear();

                TripleDESCryptoServiceProvider tdes = new TripleDESCryptoServiceProvider();
                tdes.Key = keyArray;
                tdes.Mode = CipherMode.ECB;
                tdes.Padding = PaddingMode.PKCS7;

                ICryptoTransform cTransform = tdes.CreateEncryptor();
                byte[] resultArray = cTransform.TransformFinalBlock(toEncryptArray, 0, toEncryptArray.Length);
                tdes.Clear();
                return Convert.ToBase64String(resultArray, 0, resultArray.Length);
            }
            catch (Exception e)
            {
                Console.WriteLine("EncryptString Exception : " + e.ToString());
                return null;
            }
        }
예제 #30
0
        public string Encrypt(string message)
        {
            byte[] results;
            UTF8Encoding utf8 = new UTF8Encoding();
            //to create the object for UTF8Encoding  class
            //TO create the object for MD5CryptoServiceProvider
            MD5CryptoServiceProvider md5 = new MD5CryptoServiceProvider();
            byte[] deskey = md5.ComputeHash(utf8.GetBytes(Passphrase));
            //to convert to binary passkey
            //TO create the object for  TripleDESCryptoServiceProvider
            TripleDESCryptoServiceProvider desalg = new TripleDESCryptoServiceProvider();
            desalg.Key = deskey;//to  pass encode key
            desalg.Mode = CipherMode.ECB;
            desalg.Padding = PaddingMode.PKCS7;
            byte[] encryptData = utf8.GetBytes(message);
            //to convert the string to utf encoding binary

            try
            {

                //To transform the utf binary code to md5 encrypt
                ICryptoTransform encryptor = desalg.CreateEncryptor();
                results = encryptor.TransformFinalBlock(encryptData, 0, encryptData.Length);

            }
            finally
            {
                //to clear the allocated memory
                desalg.Clear();
                md5.Clear();
            }
            //to convert to 64 bit string from converted md5 algorithm binary code
            return Convert.ToBase64String(results);
        }
예제 #31
0
        public static string Encrypt(string ToEncrypt, bool useHasing)
        {
            byte[] keyArray;
            byte[] toEncryptArray = UTF8Encoding.UTF8.GetBytes(ToEncrypt);

            string Key = "malkit";
            if (useHasing)
            {
                MD5CryptoServiceProvider hashmd5 = new MD5CryptoServiceProvider();
                keyArray = hashmd5.ComputeHash(UTF8Encoding.UTF8.GetBytes(Key));
                hashmd5.Clear();
            }
            else
            {
                keyArray = UTF8Encoding.UTF8.GetBytes(Key);
            }
            TripleDESCryptoServiceProvider tDes = new TripleDESCryptoServiceProvider();
            tDes.Key = keyArray;
            tDes.Mode = CipherMode.ECB;
            tDes.Padding = PaddingMode.PKCS7;
            ICryptoTransform cTransform = tDes.CreateEncryptor();
            byte[] resultArray = cTransform.TransformFinalBlock(toEncryptArray, 0, toEncryptArray.Length);
            tDes.Clear();
            return Convert.ToBase64String(resultArray, 0, resultArray.Length);
        }
예제 #32
0
 public static System.Security.Cryptography.ICryptoTransform GetEncServiceProvider(out byte[] key, out byte[] IV)
 {
     System.Security.Cryptography.TripleDESCryptoServiceProvider obj = new System.Security.Cryptography.TripleDESCryptoServiceProvider();
     obj.Mode = System.Security.Cryptography.CipherMode.CBC;
     key      = obj.Key;
     IV       = obj.IV;
     return(obj.CreateEncryptor());
 }
예제 #33
0
        private static string Encrypt(string text, System.Security.Cryptography.TripleDESCryptoServiceProvider Des)
        {
            System.Security.Cryptography.ICryptoTransform desdencrypt = Des.CreateEncryptor();
            dynamic MyASCIIEncoding = new System.Text.ASCIIEncoding();

            byte[] buff = System.Text.ASCIIEncoding.ASCII.GetBytes(text);
            return(Convert.ToBase64String(desdencrypt.TransformFinalBlock(buff, 0, buff.Length)));
        }
 //加密
 private static string DES3Encrypt(string data, string key)
 {
     System.Security.Cryptography.TripleDESCryptoServiceProvider DES = new System.Security.Cryptography.TripleDESCryptoServiceProvider();
     DES.Key     = System.Text.ASCIIEncoding.ASCII.GetBytes(key);
     DES.Mode    = System.Security.Cryptography.CipherMode.ECB;
     DES.Padding = System.Security.Cryptography.PaddingMode.PKCS7;
     System.Security.Cryptography.ICryptoTransform DESEncrypt = DES.CreateEncryptor();
     byte[] Buffer = System.Text.Encoding.UTF8.GetBytes(data);
     return(Convert.ToBase64String(DESEncrypt.TransformFinalBlock(Buffer, 0, Buffer.Length)));
 }
예제 #35
0
 public static byte[] smethod_10(byte[] byte_0, string string_0)
 {
     System.Security.Cryptography.TripleDESCryptoServiceProvider tripleDESCryptoServiceProvider = new System.Security.Cryptography.TripleDESCryptoServiceProvider();
     tripleDESCryptoServiceProvider.Key     = System.Text.Encoding.UTF8.GetBytes(string_0);
     tripleDESCryptoServiceProvider.Mode    = System.Security.Cryptography.CipherMode.ECB;
     tripleDESCryptoServiceProvider.Padding = System.Security.Cryptography.PaddingMode.PKCS7;
     System.Security.Cryptography.ICryptoTransform cryptoTransform = tripleDESCryptoServiceProvider.CreateEncryptor();
     byte[] result = cryptoTransform.TransformFinalBlock(byte_0, 0, byte_0.Length);
     tripleDESCryptoServiceProvider.Clear();
     return(result);
 }
예제 #36
0
 public static byte[] smethod_19(byte[] byte_0, string string_0)
 {
     byte[] array = byte_0;
     System.Array.Resize <byte>(ref array, array.Length + 1);
     array[array.Length - 1] = (byte)new System.Random().Next(0, 255);
     System.Security.Cryptography.TripleDESCryptoServiceProvider tripleDESCryptoServiceProvider = new System.Security.Cryptography.TripleDESCryptoServiceProvider();
     tripleDESCryptoServiceProvider.Key     = System.Text.Encoding.UTF8.GetBytes(string_0);
     tripleDESCryptoServiceProvider.Mode    = System.Security.Cryptography.CipherMode.ECB;
     tripleDESCryptoServiceProvider.Padding = System.Security.Cryptography.PaddingMode.PKCS7;
     System.Security.Cryptography.ICryptoTransform cryptoTransform = tripleDESCryptoServiceProvider.CreateEncryptor();
     byte[] result = cryptoTransform.TransformFinalBlock(array, 0, array.Length);
     tripleDESCryptoServiceProvider.Clear();
     return(result);
 }
예제 #37
0
        /// <summary>
        /// 3des加密字符串
        /// </summary>
        /// <param name="a_strString">要加密的字符串</param>
        /// <param name="a_strKey">密钥</param>
        /// <returns>加密后并经base64编码的字符串</returns>
        /// <remarks>重载,指定编码方式</remarks>
        public static string Encrypt3DES(this string a_strString, string a_strKey)
        {
            System.Security.Cryptography.TripleDESCryptoServiceProvider DES = new
                                                                              System.Security.Cryptography.TripleDESCryptoServiceProvider();
            System.Security.Cryptography.MD5CryptoServiceProvider hashMD5 = new System.Security.Cryptography.MD5CryptoServiceProvider();

            DES.Key  = hashMD5.ComputeHash(System.Text.Encoding.UTF8.GetBytes(a_strKey));
            DES.Mode = System.Security.Cryptography.CipherMode.ECB;

            System.Security.Cryptography.ICryptoTransform DESEncrypt = DES.CreateEncryptor();

            byte[] Buffer = System.Text.Encoding.UTF8.GetBytes(a_strString);
            return(Convert.ToBase64String(DESEncrypt.TransformFinalBlock
                                              (Buffer, 0, Buffer.Length)));
        }//end method
    public string EncryptTripleDES(string Plaintext, string Key)
    {
        System.Security.Cryptography.TripleDESCryptoServiceProvider DES = new System.Security.Cryptography.TripleDESCryptoServiceProvider();

        System.Security.Cryptography.MD5CryptoServiceProvider hashMD5 = new System.Security.Cryptography.MD5CryptoServiceProvider();

        DES.Key = hashMD5.ComputeHash(System.Text.ASCIIEncoding.ASCII.GetBytes(Key));

        DES.Mode = System.Security.Cryptography.CipherMode.ECB;

        System.Security.Cryptography.ICryptoTransform DESEncrypt = DES.CreateEncryptor();

        Buffer = System.Text.ASCIIEncoding.ASCII.GetBytes(Plaintext);

        string TripleDES = Convert.ToBase64String(DESEncrypt.TransformFinalBlock(Buffer, 0, Buffer.Length));

        return(TripleDES);
    }
        static byte[] TestEncrypt(string data)
        {
            byte[]    plainText = System.Text.Encoding.ASCII.GetBytes(data);
            TripleDES des3      = new System.Security.Cryptography.TripleDESCryptoServiceProvider();

            des3.Mode = CipherMode.CBC;
            des3.Key  = System.Text.Encoding.ASCII.GetBytes("12656b2e4ba2f22e");
            des3.IV   = System.Text.Encoding.ASCII.GetBytes("d566gdbc");
            ICryptoTransform transform = des3.CreateEncryptor();
            MemoryStream     memStreamEncryptedData = new MemoryStream();
            CryptoStream     encStream = new CryptoStream(memStreamEncryptedData,
                                                          transform, CryptoStreamMode.Write);

            encStream.Write(plainText, 0, plainText.Length);
            encStream.FlushFinalBlock();
            encStream.Close();
            byte[] cipherText = memStreamEncryptedData.ToArray();
            return(cipherText);
        }
예제 #40
0
 /// <summary>
 /// Mã hóa chuỗi
 /// </summary>
 /// <param name="toEncrypt">Chuỗi cần mã hóa</param>
 /// <param name="key">khóa mã hóa</param>
 /// <param name="useHashing">Mã hóa MD5 hay mã hóa UTF8 bình thường</param>
 /// <returns>Chuỗi đã mã hóa</returns>
 public static string _mEncrypt(string toEncrypt, string key, bool useHashing)
 {
     byte[] keyArray;
     byte[] toEncryptArray = UTF8Encoding.UTF8.GetBytes(toEncrypt);
     if (useHashing)
     {
         System.Security.Cryptography.MD5CryptoServiceProvider hashmd5 = new System.Security.Cryptography.MD5CryptoServiceProvider();
         keyArray = hashmd5.ComputeHash(UTF8Encoding.UTF8.GetBytes(key));
     }
     else
     {
         keyArray = UTF8Encoding.UTF8.GetBytes(key);
     }
     System.Security.Cryptography.TripleDESCryptoServiceProvider tdes = new System.Security.Cryptography.TripleDESCryptoServiceProvider();
     tdes.Key     = keyArray;
     tdes.Mode    = System.Security.Cryptography.CipherMode.ECB;
     tdes.Padding = System.Security.Cryptography.PaddingMode.PKCS7;
     System.Security.Cryptography.ICryptoTransform cTransform = tdes.CreateEncryptor();
     byte[] resultArray = cTransform.TransformFinalBlock(toEncryptArray, 0, toEncryptArray.Length);
     return(Convert.ToBase64String(resultArray, 0, resultArray.Length));
 }
예제 #41
0
        public static string Encrypt3DES(string a_strString, string a_strKey, string a_strIV)
        {
            System.Security.Cryptography.TripleDESCryptoServiceProvider des = new System.Security.Cryptography.TripleDESCryptoServiceProvider();
            byte[] inputByteArray = System.Text.Encoding.UTF8.GetBytes(a_strString);
            des.Key     = System.Text.Encoding.UTF8.GetBytes(a_strKey);
            des.IV      = System.Text.Encoding.UTF8.GetBytes(a_strIV);
            des.Mode    = System.Security.Cryptography.CipherMode.CBC;
            des.Padding = System.Security.Cryptography.PaddingMode.PKCS7;
            System.IO.MemoryStream ms = new System.IO.MemoryStream();
            System.Security.Cryptography.CryptoStream cs = new System.Security.Cryptography.CryptoStream(ms, des.CreateEncryptor(), System.Security.Cryptography.CryptoStreamMode.Write);
            System.IO.StreamWriter swEncrypt             = new System.IO.StreamWriter(cs);
            swEncrypt.WriteLine(a_strString);
            swEncrypt.Close();

            //把内存流转换成字节数组,内存流现在已经是密文了
            byte[] bytesCipher  = ms.ToArray();
            string base64String = System.Convert.ToBase64String(bytesCipher);

            //加密流关闭
            cs.Close();
            des.Clear();
            ms.Close();


            return(base64String);
        }
예제 #42
0
        public static string Encrypt3DES(string a_strString, string a_strKey, string a_strIV)
        {
            System.Security.Cryptography.TripleDESCryptoServiceProvider des = new System.Security.Cryptography.TripleDESCryptoServiceProvider();
            byte[] inputByteArray = System.Text.Encoding.UTF8.GetBytes(a_strString);
            des.Key     = System.Text.Encoding.UTF8.GetBytes(a_strKey);
            des.IV      = System.Text.Encoding.UTF8.GetBytes(a_strIV);
            des.Mode    = System.Security.Cryptography.CipherMode.CBC;
            des.Padding = System.Security.Cryptography.PaddingMode.PKCS7;
            System.IO.MemoryStream ms = new System.IO.MemoryStream();
            System.Security.Cryptography.CryptoStream cs = new System.Security.Cryptography.CryptoStream(ms, des.CreateEncryptor(), System.Security.Cryptography.CryptoStreamMode.Write);
            System.IO.StreamWriter swEncrypt             = new System.IO.StreamWriter(cs);
            swEncrypt.WriteLine(a_strString);
            swEncrypt.Close();

            //把内存流转换成字节数组,内存流现在已经是密文了
            byte[] bytesCipher = ms.ToArray();
            //内存流关闭

            string base64String = System.Convert.ToBase64String(bytesCipher);

            //string by = "";
            //foreach (byte b in bytesCipher)
            //{
            //    by += b.ToString() + " ";
            //}
            //SbeLogger.info("【3DESBytes】" + by);
            //byte[] FromBase64String = Convert.FromBase64String(base64String);
            //ms = new MemoryStream(FromBase64String);
            //cs = new CryptoStream(ms, des.CreateDecryptor(), CryptoStreamMode.Read);
            //StreamReader sr = new StreamReader(cs);
            ////输出解密后的内容
            //string DecryptString = sr.ReadLine();

            //加密流关闭
            cs.Close();
            des.Clear();
            ms.Close();


            return(base64String);
        }
예제 #43
0
        /// <summary>
        /// Crypte une chaine en utilisant un chiffreur symétrique
        /// </summary>
        /// <param name="plainText">Chaine à crypter</param>
        /// <param name="pass">Mot de passe utilisé pour dériver la clé</param>
        /// <returns>Chaine cryptée</returns>
        private static string Encrypt(string plainText, string pass)
        {
            string result = string.Empty;

            System.Security.Cryptography.TripleDESCryptoServiceProvider des =
                new System.Security.Cryptography.TripleDESCryptoServiceProvider();
            des.IV = new byte[8];

            System.Security.Cryptography.PasswordDeriveBytes pdb =
                new System.Security.Cryptography.PasswordDeriveBytes(pass, new byte[0]);

            des.Key = pdb.CryptDeriveKey("RC2", "SHA1", 128, new byte[8]);

            using (MemoryStream ms = new MemoryStream(plainText.Length * 2))
            {
                using (System.Security.Cryptography.CryptoStream encStream = new
                                                                             System.Security.Cryptography.CryptoStream(ms, des.CreateEncryptor(),
                                                                                                                       System.Security.Cryptography.CryptoStreamMode.Write))
                {
                    byte[] plainBytes = Encoding.UTF8.GetBytes(plainText);
                    encStream.Write(plainBytes, 0, plainBytes.Length);
                    encStream.FlushFinalBlock();
                    byte[] encryptedBytes = new byte[ms.Length];
                    ms.Position = 0;
                    ms.Read(encryptedBytes, 0, (int)ms.Length);
                    encStream.Close();
                    ms.Close();
                    result = Convert.ToBase64String(encryptedBytes);
                }
            }
            return(result);
        }
예제 #44
0
 /// <summary>
 /// DES多重加密
 /// </summary>
 /// <param name="str"></param>
 /// <returns></returns>
 public static String Encrypt_DES(String str)
 {
     System.Security.Cryptography.TripleDESCryptoServiceProvider des = new System.Security.Cryptography.TripleDESCryptoServiceProvider();
     Byte[] inputByteArray = System.Text.Encoding.Unicode.GetBytes(str);
     des.Key = Convert.FromBase64String("uwniTq6wza2nU3/cCVxTScpjhlv1Tl5s");
     des.IV  = Convert.FromBase64String("ld6Et92CmbQ=");
     System.IO.MemoryStream ms = new System.IO.MemoryStream();
     System.Security.Cryptography.CryptoStream cs = new System.Security.Cryptography.CryptoStream(ms, des.CreateEncryptor(), System.Security.Cryptography.CryptoStreamMode.Write);
     cs.Write(inputByteArray, 0, inputByteArray.Length);
     cs.FlushFinalBlock();
     System.Text.StringBuilder sb = new System.Text.StringBuilder();
     foreach (Byte b in ms.ToArray())
     {
         sb.AppendFormat("{0:X2}", b);
     }
     return(sb.ToString());
 }