当前位置: 首页>>代码示例>>C#>>正文


C# AesCryptoServiceProvider.CreateEncryptor方法代码示例

本文整理汇总了C#中System.Security.Cryptography.AesCryptoServiceProvider.CreateEncryptor方法的典型用法代码示例。如果您正苦于以下问题:C# AesCryptoServiceProvider.CreateEncryptor方法的具体用法?C# AesCryptoServiceProvider.CreateEncryptor怎么用?C# AesCryptoServiceProvider.CreateEncryptor使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在System.Security.Cryptography.AesCryptoServiceProvider的用法示例。


在下文中一共展示了AesCryptoServiceProvider.CreateEncryptor方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。

示例1: SetUp

 public void SetUp()
 {
     using ( AesCryptoServiceProvider aesAlg = new AesCryptoServiceProvider() ) {
         m_Encryptor = aesAlg.CreateEncryptor();
         m_Decryptor = aesAlg.CreateDecryptor();
     }
 }
开发者ID:overmind900,项目名称:Utilities,代码行数:7,代码来源:TestCryptography.cs

示例2: EncryptString

 public static byte[] EncryptString(string toEncrypt, byte[] encryptionKey)
 {
     if (string.IsNullOrEmpty(toEncrypt)) throw new ArgumentException("toEncrypt");
     if (encryptionKey == null || encryptionKey.Length == 0) throw new ArgumentException("encryptionKey");
     var toEncryptBytes = Encoding.UTF8.GetBytes(toEncrypt);
     using (var provider = new AesCryptoServiceProvider())
     {
         provider.Key = encryptionKey;
         provider.Mode = CipherMode.CBC;
         provider.Padding = PaddingMode.PKCS7;
         using (var encryptor = provider.CreateEncryptor(provider.Key, provider.IV))
         {
             using (var ms = new MemoryStream())
             {
                 ms.Write(provider.IV, 0, 16);
                 using (var cs = new CryptoStream(ms, encryptor, CryptoStreamMode.Write))
                 {
                     cs.Write(toEncryptBytes, 0, toEncryptBytes.Length);
                     cs.FlushFinalBlock();
                 }
                 return ms.ToArray();
             }
         }
     }
 }
开发者ID:KLIM8D,项目名称:ucn-4semproject-dm79-group2,代码行数:25,代码来源:Encryption.cs

示例3: AESEncryptor

 public static string AESEncryptor(string plainText, byte[] Key, byte[] IV)
 {
     byte[] data = ASCIIEncoding.ASCII.GetBytes(plainText);
     AesCryptoServiceProvider aes = new AesCryptoServiceProvider();
     string encryptedString = Convert.ToBase64String(aes.CreateEncryptor(Key, IV).TransformFinalBlock(data, 0, data.Length));
     return encryptedString;
 }
开发者ID:chimpinano,项目名称:MVC5Book,代码行数:7,代码来源:PasswordUtility.cs

示例4: EncryptString

        public static string EncryptString(string message, string passphrase = null)
        {
            if (string.IsNullOrEmpty(message))
            {
                throw new ArgumentException("message is empty!", message);
            }

            if (string.IsNullOrEmpty(passphrase))
            {
                passphrase = ConfigurationManager.AppSettings.Get("Common.Cryptography.DefaultPassPhrase");
            }

            // First hash the passphrase to get a 256bit key
            var sha = new SHA256CryptoServiceProvider();
            var passphraseHash = sha.ComputeHash(Encoding.UTF8.GetBytes(passphrase));

            var aes = new AesCryptoServiceProvider { KeySize = 256, Key = passphraseHash };

            var memoryStream = new MemoryStream();

            var cryptoStream = new CryptoStream(memoryStream, aes.CreateEncryptor(), CryptoStreamMode.Write);

            using (var writer = new StreamWriter(cryptoStream, Encoding.UTF8))
            {
                writer.Write(message);
            }

            var results = memoryStream.ToArray();

            var finalArray = new byte[aes.IV.Length + results.Length];
            Array.Copy(aes.IV, 0, finalArray, 0, aes.IV.Length);
            Array.Copy(results, 0, finalArray, aes.IV.Length, results.Length);

            return HttpServerUtility.UrlTokenEncode(finalArray);
        }
开发者ID:mcilis,项目名称:weboideas,代码行数:35,代码来源:AesCryptography.cs

示例5: encriptar

        public static string encriptar(string texto, string key)
        {
            AesCryptoServiceProvider aes = new AesCryptoServiceProvider();
            MD5 md5Hash = MD5.Create();
            string chave = GetMd5Hash(md5Hash, key);
            aes.Key = System.Text.Encoding.UTF8.GetBytes(chave);
            aes.BlockSize = 128;
            aes.IV = System.Text.Encoding.UTF8.GetBytes("6abraswa+hep&#Ac");
            ICryptoTransform encryptor = aes.CreateEncryptor(aes.Key, aes.IV);

            byte[] encrypted;

            // Create the streams used for encryption.
            using (MemoryStream msEncrypt = new MemoryStream())
            {
                using (CryptoStream csEncrypt = new CryptoStream(msEncrypt, encryptor, CryptoStreamMode.Write))
                {
                    using (StreamWriter swEncrypt = new StreamWriter(csEncrypt))
                    {

                        //Write all data to the stream.
                        swEncrypt.Write(texto);
                    }
                    encrypted = msEncrypt.ToArray();
                }
            }

            return Convert.ToBase64String(encrypted);
        }
开发者ID:appcelerator-forks,项目名称:kilikdudu.Vale24h,代码行数:29,代码来源:Crypto.cs

示例6: EncryptAesCbc

        /// <summary>
        /// Encrypt a message using AES in CBC (cipher-block chaining) mode.
        /// </summary>
        /// <param name="plaintext">The message (plaintext) to encrypt</param>
        /// <param name="key">An AES key</param>
        /// <param name="iv">The IV to use or null to use a 0 IV</param>
        /// <param name="addHmac">When set, a SHA256-based HMAC (HMAC256) of 32 bytes using the same key is added to the plaintext
        /// before it is encrypted.</param>
        /// <returns>The ciphertext derived by encrypting the orignal message using AES in CBC mode</returns>
        public static byte[] EncryptAesCbc(byte[] plaintext, byte[] key, byte[] iv = null, bool addHmac = false)
        {
            using (AesCryptoServiceProvider aes = new AesCryptoServiceProvider())
            {
                aes.Key = key;
                if (iv == null)
                    iv = NullIv;
                aes.Mode = CipherMode.CBC;
                aes.IV = iv;

                // Encrypt the message with the key using CBC and InitializationVector=0
                byte[] cipherText;
                using (System.IO.MemoryStream ciphertext = new System.IO.MemoryStream())
                {
                    using (CryptoStream cs = new CryptoStream(ciphertext, aes.CreateEncryptor(), CryptoStreamMode.Write))
                    {
                        cs.Write(plaintext, 0, plaintext.Length);
                        if (addHmac)
                        {
                            byte[] hmac = new HMACSHA256(key).ComputeHash(plaintext);
                            cs.Write(hmac, 0, hmac.Length);
                        }
                        cs.Flush();
                    }
                    cipherText = ciphertext.ToArray();
                }

                return cipherText;
            }
        }
开发者ID:Microsoft,项目名称:StopGuessing,代码行数:39,代码来源:Encryption.cs

示例7: EncryptByBlocks

 protected override string EncryptByBlocks(byte[] key,
     List<byte> plainText)
 {
     var cipherText = new StringBuilder();
     if (plainText.Count % BlockSize == 0)
     {
         AddDummyBlock(plainText);
     }
     using (var aes = new AesCryptoServiceProvider())
     {
         aes.BlockSize = BlockSize * 8;
         aes.KeySize = KeySize * 8;
         aes.Padding = PaddingMode.None;
         aes.Key = key;
         aes.GenerateIV();
         cipherText.Append(BytesToHexString(aes.IV));
         using (var encr = aes.CreateEncryptor())
         {
             while (plainText.Count != 0)
             {
                 var block = DetachBlock(plainText);
                 if (block.Length < BlockSize)
                 {
                     block = CompleteBlock(block);
                 }
                 var encrypted = new byte[BlockSize];
                 encr.TransformBlock(block, 0, BlockSize, encrypted, 0);
                 cipherText.Append(BytesToHexString(encrypted));
                 aes.IV = encrypted;
             }
         }
     }
     return cipherText.ToString();
 }
开发者ID:yurkova,项目名称:cryptography_I,代码行数:34,代码来源:CbcAes.cs

示例8: Encrypt

        /// <summary>
        /// AES encryption
        /// </summary>
        public string Encrypt(string text)
        {
            // AES cryptographic service provider
            AesCryptoServiceProvider aes = new AesCryptoServiceProvider();
            aes.BlockSize = 128;
            aes.KeySize = 128;
           
            aes.Mode = CipherMode.CBC;
            aes.Padding = PaddingMode.PKCS7;

            aes.IV = Encoding.UTF8.GetBytes(AesIV);
            aes.Key = Encoding.UTF8.GetBytes(AesKey);

            // String → byte
            byte[] src = Encoding.Unicode.GetBytes(text);

            //encryption
            using (ICryptoTransform encrypt = aes.CreateEncryptor())
            {
                byte[] dest = encrypt.TransformFinalBlock(src, 0, src.Length);

                // byte→Base64
                return Convert.ToBase64String(dest);
            }
        }
开发者ID:OkaMisato,项目名称:J-s-Communication-Co.-Ltd.,代码行数:28,代码来源:cls_pass.cs

示例9: EncryptAES

        /// <summary>
        /// Encryptes a string using 3DES
        /// </summary>
        /// <param name="data">The data to encrypt</param>
        /// <param name="key">The key to use</param>
        /// <param name="IV">Initialization Vector</param>
        /// <returns>An encrypted, base64 encoded string</returns>
        public static byte[] EncryptAES(string data, byte[] key, byte[] IV)
        {
            string retVal = string.Empty;

            var tsp = new System.Security.Cryptography.AesCryptoServiceProvider();

            // default padding and mode
            tsp.Mode = CipherMode.CBC;
            tsp.Padding = PaddingMode.PKCS7;

            // get a crypt transform interface
            ICryptoTransform ct = tsp.CreateEncryptor(key, IV);

            // setup a memory stream
            MemoryStream ms = new MemoryStream();
            using (CryptoStream cs = new CryptoStream(ms, ct, CryptoStreamMode.Write))
            {
                // write the string through the crypto stream
                byte[] db = System.Text.Encoding.ASCII.GetBytes(data);
                cs.Write(db, 0, db.Length);

                // flush
                cs.FlushFinalBlock();

                // convert the data in the memory stream to base64 text
                ms.Seek(0, SeekOrigin.Begin);

                var edata = ms.ToArray();
                return edata;
            }
        }
开发者ID:akois,项目名称:OracleService,代码行数:38,代码来源:CryptoHelper.cs

示例10: Encrypt

        public static string Encrypt(long id)
        {
            byte[] encrypted;

            using (var aesAlg = new AesCryptoServiceProvider())
            {
                aesAlg.Key = AesCryptoServiceProvider.Key;
                aesAlg.IV = AesCryptoServiceProvider.IV;

                var encryptor = aesAlg.CreateEncryptor(aesAlg.Key, aesAlg.IV);

                using (var msEncrypt = new MemoryStream())
                {
                    using (var csEncrypt = new CryptoStream(msEncrypt, encryptor, CryptoStreamMode.Write))
                    {
                        using (var swEncrypt = new StreamWriter(csEncrypt))
                        {
                            swEncrypt.Write(id);
                        }
                        encrypted = msEncrypt.ToArray();
                    }
                }
            }

            return Convert.ToBase64String(encrypted);
        }
开发者ID:Sp1n89,项目名称:AutoDok,代码行数:26,代码来源:EncryptionProvider.cs

示例11: Encription

        public byte[] Encription(string message, byte[] bobPubKeyBlob)
        {
            byte[] rawData = Encoding.UTF8.GetBytes(message);
            byte[] encryptedData = null;

            using (var aliceAlgorithm = new ECDiffieHellmanCng(privKey))
            using (CngKey bobPubKey = CngKey.Import(bobPubKeyBlob,
                  CngKeyBlobFormat.EccPublicBlob))
            {
                byte[] symmKey = aliceAlgorithm.DeriveKeyMaterial(bobPubKey);
                using (var aes = new AesCryptoServiceProvider())
                {
                    aes.Key = symmKey;
                    aes.GenerateIV();
                    using (ICryptoTransform encryptor = aes.CreateEncryptor())
                    using (MemoryStream ms = new MemoryStream())
                    {
                        // create CryptoStream and encrypt data to send
                        var cs = new CryptoStream(ms, encryptor, CryptoStreamMode.Write);

                        // write initialization vector not encrypted
                        ms.Write(aes.IV, 0, aes.IV.Length);
                        cs.Write(rawData, 0, rawData.Length);
                        cs.Close();
                        encryptedData = ms.ToArray();
                    }
                    aes.Clear();
                }
            }
            return encryptedData;
        }
开发者ID:mohankumarcm,项目名称:sse554-project3,代码行数:31,代码来源:EncriptionDecription.cs

示例12: Encrypt

 /// <summary>
 /// Encrypts a plainText string using the system key retrieved from the Web Service
 /// </summary>
 /// <param name="plainText">String to encrypt</param>
 /// <returns>EncryptedData with encrypted string and IV used</returns>
 public EncryptedData Encrypt(string plainText)
 {
     lock (_locker)
     {
         using (var crypto = new AesCryptoServiceProvider())
         {
             try
             {
                 UnprotectKey();
                 // Create the streams used for encryption. 
                 using (MemoryStream msEncrypt = new MemoryStream())
                 using (var encryptor = crypto.CreateEncryptor(_key, crypto.IV))
                 using (CryptoStream csEncrypt = new CryptoStream(msEncrypt, encryptor, CryptoStreamMode.Write))
                 {
                     using (StreamWriter swEncrypt = new StreamWriter(csEncrypt))
                     {
                         //Write all data to the stream.
                         swEncrypt.Write(plainText);
                     }
                     return new EncryptedData(msEncrypt.ToArray(), crypto.IV);
                 }
             }
             finally
             {
                 ProtectKey();
             }
         }
     }
 }
开发者ID:SharpSeeEr,项目名称:SharpNET.Utilities,代码行数:34,代码来源:EncryptionManager.cs

示例13: EncryptAES

        /// <summary>
        /// uses the AES FIPS-140 compliant algorithm to encrypt a string
        /// </summary>
        /// <param name="plainText">the text to encrypt</param>
        /// <param name="passPhrase">the pass phase to do the encryption</param>
        /// <param name="salt">a salt value to ensure ciphertext using the same text/password is different</param>
        /// <param name="iterations">number of iterations to derive the key (higher is slower but more secure) - optional parameter with a default of 1000</param>
        /// <returns></returns>
        public static String EncryptAES(String plainText, String passPhrase, String salt, int iterations = 1000)
        {
            VerifyAesSettings(passPhrase, salt);

            byte[] saltBytes = Encoding.ASCII.GetBytes(salt);
            var aesProvider = new AesCryptoServiceProvider();
            var derivedBytes = new Rfc2898DeriveBytes(passPhrase, saltBytes, iterations);
            Byte[] derivedKey = derivedBytes.GetBytes(32); // 256 bits
            Byte[] derivedInitVector = derivedBytes.GetBytes(16); // 128 bits
            Byte[] plainTextBytes = Encoding.UTF8.GetBytes(plainText);

            aesProvider.KeySize = 256;
            aesProvider.Padding = PaddingMode.ISO10126;
            aesProvider.Mode = CipherMode.CBC;

            ICryptoTransform encryptor = aesProvider.CreateEncryptor(derivedKey, derivedInitVector);
            var memStream = new MemoryStream();
            var cryptoStream = new CryptoStream(memStream, encryptor, CryptoStreamMode.Write);

            cryptoStream.Write(plainTextBytes, 0, plainTextBytes.Length);
            cryptoStream.FlushFinalBlock();
            Byte[] cipherTextBytes = memStream.ToArray();

            memStream.Close();
            cryptoStream.Close();

            return Convert.ToBase64String(cipherTextBytes);
        }
开发者ID:VegasoftTI,项目名称:Dnn.Platform,代码行数:36,代码来源:FIPSCompliant.cs

示例14: Encrypt

        public static byte[] Encrypt(byte[] input, byte[] key)
        {
            if (key == null || key.Length == 0) throw new Exception("Key can not be empty.");

            using (var md5 = new MD5CryptoServiceProvider())
            {
                key = md5.ComputeHash(key);
            }

            byte[] data = input, encdata = new byte[0];

            try
            {
                using (var ms = new MemoryStream())
                {
                    using (var aesProvider = new AesCryptoServiceProvider() { Key = key })
                    {
                        aesProvider.GenerateIV();

                        using (var cs = new CryptoStream(ms, aesProvider.CreateEncryptor(), CryptoStreamMode.Write))
                        {
                            ms.Write(aesProvider.IV, 0, aesProvider.IV.Length); // write first 16 bytes IV, followed by encrypted message
                            cs.Write(data, 0, data.Length);
                        }
                    }

                    encdata = ms.ToArray();
                }
            }
            catch
            {
            }
            return encdata;
        }
开发者ID:yashodhank,项目名称:QuasarRAT,代码行数:34,代码来源:AES.cs

示例15: Encrypt

        /// <summary>
        /// AES Encryption
        /// </summary>
        public static string Encrypt(string clearText)
        {
            if (clearText == null) return null;
            // AesCryptoServiceProvider
            AesCryptoServiceProvider aes = new AesCryptoServiceProvider();
            aes.BlockSize = 128;
            aes.KeySize = 128;

            aes.GenerateIV();
            aes.Key = Encoding.UTF8.GetBytes(AesKey);
            aes.Mode = CipherMode.CBC;
            aes.Padding = PaddingMode.PKCS7;

            // Convert string to byte array
            byte[] src = Encoding.Unicode.GetBytes(clearText);

            // encryption
            using (ICryptoTransform encrypt = aes.CreateEncryptor())
            {
                byte[] dest = encrypt.TransformFinalBlock(src, 0, src.Length);

                // Convert byte array to Base64 strings
                return Convert.ToBase64String(aes.IV) + Convert.ToBase64String(dest);
            }
        }
开发者ID:RobertMandache,项目名称:StorageHub,代码行数:28,代码来源:Encryption.cs


注:本文中的System.Security.Cryptography.AesCryptoServiceProvider.CreateEncryptor方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。