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


C# AesManaged.CreateEncryptor方法代码示例

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


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

示例1: Encrypt

        public static string Encrypt(string key, string text)
        {
            // Our symmetric encryption algorithm
            AesManaged aes = new AesManaged();

            // We're using the PBKDF2 standard for password-based key generation
            Rfc2898DeriveBytes rfc = new Rfc2898DeriveBytes("password", Encoding.UTF8.GetBytes(key));

            // Setting our parameters
            aes.BlockSize = aes.LegalBlockSizes[0].MaxSize;
            aes.KeySize = aes.LegalKeySizes[0].MaxSize;

            aes.Key = rfc.GetBytes(aes.KeySize / 8);
            aes.IV = rfc.GetBytes(aes.BlockSize / 8);

            // Encryption
            ICryptoTransform encryptTransf = aes.CreateEncryptor();

            // Output stream, can be also a FileStream
            MemoryStream encryptStream = new MemoryStream();
            CryptoStream encryptor = new CryptoStream(encryptStream, encryptTransf, CryptoStreamMode.Write);

            byte[] utfData = Encoding.Unicode.GetBytes(text);

            encryptor.Write(utfData, 0, utfData.Length);
            encryptor.Flush();
            encryptor.Close();

            // return encrypted content
            return Convert.ToBase64String(encryptStream.ToArray());
        }
开发者ID:CarlosVV,项目名称:mediavf,代码行数:31,代码来源:EncryptionUtility.cs

示例2: Encrypt

        public static string Encrypt(string plainText, string key)
        {
            if (string.IsNullOrEmpty(plainText))
                throw new ArgumentNullException("plainText");
            if (string.IsNullOrEmpty(key))
                throw new ArgumentNullException("key");

            using (var keyDerivationFunction = new Rfc2898DeriveBytes(key, _saltSize))
            {
                var saltBytes = keyDerivationFunction.Salt;
                var keyBytes = keyDerivationFunction.GetBytes(32);
                var ivBytes = keyDerivationFunction.GetBytes(16);

                using (var aesManaged = new AesManaged())
                using (var encryptor = aesManaged.CreateEncryptor(keyBytes, ivBytes))
                using (var memoryStream = new MemoryStream())
                {
                    using (var cryptoStream = new CryptoStream(memoryStream, encryptor, CryptoStreamMode.Write))
                    using (var streamWriter = new StreamWriter(cryptoStream))
                        streamWriter.Write(plainText);

                    var cipherTextBytes = memoryStream.ToArray();
                    Array.Resize(ref saltBytes, saltBytes.Length + cipherTextBytes.Length);
                    Array.Copy(cipherTextBytes, 0, saltBytes, _saltSize, cipherTextBytes.Length);

                    return Convert.ToBase64String(saltBytes);
                }
            }
        }
开发者ID:vfrz,项目名称:ISNProjects,代码行数:29,代码来源:AES.cs

示例3: Encrypt

        public static string Encrypt(string plainText, string key)
        {
            if(string.IsNullOrEmpty(plainText)) {
                throw new ArgumentNullException("plainText");
            }

            if(string.IsNullOrEmpty(key)) {
                throw new ArgumentNullException("key");
            }

            using(var keyDerivationFunction = new Rfc2898DeriveBytes(key, SALT_SIZE)) {
                byte[] saltBytes = keyDerivationFunction.Salt;
                byte[] keyBytes = keyDerivationFunction.GetBytes(32);
                byte[] ivBytes = keyDerivationFunction.GetBytes(16);

                using(var aesManaged = new AesManaged()) {
                    aesManaged.KeySize = 256;

                    using(var encryptor = aesManaged.CreateEncryptor(keyBytes, ivBytes)) {
                        MemoryStream memoryStream = null;
                        CryptoStream cryptoStream = null;

                        return WriteMemoryStream(plainText, ref saltBytes, encryptor, ref memoryStream, ref cryptoStream);
                    }
                }
            }
        }
开发者ID:2nfro,项目名称:dotNet,代码行数:27,代码来源:AES.cs

示例4: Encrypt

        public Secret Encrypt(string plainText)
        {
            Secret result = new Secret();

            using (MemoryStream ms = new MemoryStream())
            {
                using (Aes aesAlg = new AesManaged())
                {
                    aesAlg.Key = _key;
                    result.IV = aesAlg.IV;

                    ICryptoTransform encryptor = aesAlg.CreateEncryptor();

                    using (CryptoStream cs = new CryptoStream(ms, encryptor, CryptoStreamMode.Write))
                    {
                        byte[] rawPlaintext = Encoding.Unicode.GetBytes(plainText);
                        cs.Write(rawPlaintext, 0, rawPlaintext.Length);
                        cs.FlushFinalBlock();

                        // get the encrypted text
                        ms.Seek(0, SeekOrigin.Begin);
                        byte[] content = new byte[ms.Length];
                        ms.Read(content, 0, content.Length);
                        result.Data = content;
                    }
                }
            }

            return result;
        }
开发者ID:lijunhe,项目名称:trigramsoft-lijun,代码行数:30,代码来源:AESEncryption.cs

示例5: EncryptIt

        public String EncryptIt(String s)
        {
            Encoding byteEncoder = Encoding.UTF8;

            byte[] rijnKey = byteEncoder.GetBytes("h504lXb1erd4ilw7"); //this is key for encrypt data
            byte[] rijnIV = byteEncoder.GetBytes("4hx7e4bwM15d0CrL"); //this is iv for encrypt data

            String result;
            
            AesManaged rijn = new AesManaged();
            
            //Using AES-128
            rijn.KeySize = 128;
            rijn.BlockSize = 128;

            using (MemoryStream msEncrypt = new MemoryStream())
            {
                using (ICryptoTransform encryptor = rijn.CreateEncryptor(rijnKey, rijnIV))
                {
                    using (CryptoStream csEncrypt = new CryptoStream(msEncrypt, encryptor, CryptoStreamMode.Write))
                    {
                        using (StreamWriter swEncrypt = new StreamWriter(csEncrypt))
                        {
                            swEncrypt.Write(s);
                        }
                    }
                }
                result = Convert.ToBase64String(msEncrypt.ToArray());
            }
            rijn.Clear();

            return result;
        }
开发者ID:Handika-GEMkey,项目名称:SolidareNew,代码行数:33,代码来源:Encrypt.cs

示例6: EncryptStringToBytes

        public static byte[] EncryptStringToBytes(string plainText, byte[] key, byte[] IV)
        {
            byte[] encrypted;

            using (Aes aesAlg = new AesManaged())
            {
                Rfc2898DeriveBytes deriveBytes = new Rfc2898DeriveBytes(Encoding.UTF8.GetString(IV, 0, IV.Length), key);
                aesAlg.Key = deriveBytes.GetBytes(128 / 8);
                aesAlg.IV = aesAlg.Key;

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

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

            return encrypted;
        }
开发者ID:TomasLinhart,项目名称:RememberPassword,代码行数:27,代码来源:AesCryptoServiceHelper.cs

示例7: Encrypt

        /// <summary>
        /// Encrypts the plainText input using the given Key.
        /// A 128 bit random salt will be generated and prepended to the ciphertext before it is returned.
        /// </summary>
        /// <param name="plainText">The plain text to encrypt.</param>
        /// <param name="password">The plain text encryption key.</param>
        /// <returns>The salt and the ciphertext</returns>
        public static byte[] Encrypt(byte[] plainText, string password)
        {
            if (plainText == null || plainText.Length == 0) throw new ArgumentNullException("plainText");
            if (string.IsNullOrEmpty(password)) throw new ArgumentNullException("password");

            // Derive a new Salt, Key, and IV from the password
            using (var keyDerivationFunction = new Rfc2898DeriveBytes(password, SaltSize))
            {
                var saltBytes = keyDerivationFunction.Salt;
                var keyBytes = keyDerivationFunction.GetBytes(32);
                var ivBytes = keyDerivationFunction.GetBytes(16);

                using (var aesManaged = new AesManaged())
                using (var encryptor = aesManaged.CreateEncryptor(keyBytes, ivBytes))
                using (var memoryStream = new MemoryStream())
                using (var cryptoStream = new CryptoStream(memoryStream, encryptor, CryptoStreamMode.Write))
                {
                    using (var streamWriter = new BinaryWriter(cryptoStream))
                    {
                        streamWriter.Write(plainText);
                    }

                    var cipherTextBytes = memoryStream.ToArray();
                    Array.Resize(ref saltBytes, saltBytes.Length + cipherTextBytes.Length);
                    Array.Copy(cipherTextBytes, 0, saltBytes, SaltSize, cipherTextBytes.Length);

                    return saltBytes;
                }
            }
        }
开发者ID:richard-green,项目名称:LCGoogleApps,代码行数:37,代码来源:SymmetricEncryption.cs

示例8: EncryptText

 public static string EncryptText(string textToEncrypt, string key, string salt)
 {
     if (string.IsNullOrWhiteSpace(textToEncrypt)) return null;
     if (string.IsNullOrWhiteSpace(key)) return null;
     if (string.IsNullOrWhiteSpace(salt)) return null;
     using (MemoryStream ms = new MemoryStream())
     {
         try
         {
             using (AesManaged aes = new AesManaged())
             {
                 Rfc2898DeriveBytes deriveBytes = new Rfc2898DeriveBytes(key, Encoding.UTF8.GetBytes(salt));
                 aes.Key = deriveBytes.GetBytes(128 / 8);
                 ms.Write(BitConverter.GetBytes(aes.IV.Length), 0, sizeof(int));
                 ms.Write(aes.IV, 0, aes.IV.Length);
                 using (CryptoStream cs = new CryptoStream(ms, aes.CreateEncryptor(), CryptoStreamMode.Write))
                 {
                     byte[] bytesFromText = Encoding.Unicode.GetBytes(textToEncrypt);
                     cs.Write(bytesFromText, 0, bytesFromText.Length);
                     cs.FlushFinalBlock();
                 }
                 return BytesToBase64String(ms.ToArray());// Return the encrypted bytes from the memory stream and convert to Base64                        
             }
         }
         catch
         {
             return null;
         }
     }
 }
开发者ID:rahilkidwai,项目名称:Net.Utility,代码行数:30,代码来源:CryptographyHelper.cs

示例9: EncryptFile

        public static void EncryptFile(string inputFile, string outputFile, string skey)
        {
            try
            {
                using (Aes aes = new AesManaged())
                {
                    var key = new Rfc2898DeriveBytes(skey, salt);

                    aes.Key = key.GetBytes(aes.KeySize / 8);
                    aes.IV = key.GetBytes(aes.BlockSize / 8);

                    using (FileStream fsCrypt = new FileStream(outputFile, FileMode.Create))
                    {
                        using (ICryptoTransform encryptor = aes.CreateEncryptor())
                        {
                            using (CryptoStream cs = new CryptoStream(fsCrypt, encryptor, CryptoStreamMode.Write))
                            {
                                using (FileStream fsIn = new FileStream(inputFile, FileMode.Open))
                                {
                                    int data;
                                    while ((data = fsIn.ReadByte()) != -1)
                                    {
                                        cs.WriteByte((byte)data);
                                    }
                                }
                            }
                        }
                    }
                }
            }
            catch { throw; }
        }
开发者ID:kaminskaarleta,项目名称:Zpi2WebApiWithLib,代码行数:32,代码来源:FileCryptography.cs

示例10: EncryptFile

        internal static void EncryptFile(string inputFile, string outputFile, string password)
        {

            try {
                var UE = new UnicodeEncoding();
                byte[] key = UE.GetBytes(password);

                string cryptFile = outputFile;
                var fsCrypt = new FileStream(cryptFile, FileMode.Create);

                var AesMngd = new AesManaged();

                var cs = new CryptoStream(fsCrypt,
                    AesMngd.CreateEncryptor(key, key),
                    CryptoStreamMode.Write);

                using (var fsIn = new FileStream(inputFile, FileMode.Open)) {
                    int data;
                    while ((data = fsIn.ReadByte()) != -1)
                        cs.WriteByte((byte)data);
                }
                cs.Close();
                fsCrypt.Close();
            } catch {
                Shared.MSGBOX(Shared.GetRes("#D.02#","Error"),"Encryption failed!",System.Windows.Application.Current.MainWindow);
            }
        }
开发者ID:RasyidUFA,项目名称:UFSJ,代码行数:27,代码来源:Shared.IO.cs

示例11: EncryptStringAes

        /// <summary>Encrypt the given string using AES.  The string can be decrypted using
        /// DecryptStringAes().  The sharedSecret parameters must match.</summary>
        /// <param name="plainText">The text to encrypt.</param>
        /// <param name="sharedSecret">A password used to generate a key for encryption.</param>
        /// <param name="salt">The salt used for encryption.</param>
        /// <returns></returns>
        public static string EncryptStringAes(string plainText, string sharedSecret, string salt)
        {
            Guard.AgainstNullOrEmpty(plainText);
            Guard.AgainstNullOrEmpty(sharedSecret);
            Guard.AgainstNullOrEmpty(salt);

            try
            {
                using (var key = new Rfc2898DeriveBytes(sharedSecret, Encoding.ASCII.GetBytes(salt)))
                using (var aesManaged = new AesManaged())
                {
                    aesManaged.Key = key.GetBytes(aesManaged.KeySize / 8);
                    aesManaged.IV = key.GetBytes(aesManaged.BlockSize / 8);

                    using (var encryptor = aesManaged.CreateEncryptor(aesManaged.Key, aesManaged.IV))
                    using (var memoryStream = new MemoryStream())
                    using (var cryptoStream = new CryptoStream(memoryStream, encryptor, CryptoStreamMode.Write))
                    using (var streamWriter = new StreamWriter(cryptoStream))
                    {
                        //Write all data to the stream.
                        streamWriter.Write(plainText);
                        streamWriter.Close();
                        return Convert.ToBase64String(memoryStream.ToArray());
                    }
                }
            }
            catch
            {
                return string.Empty;
            }
        }
开发者ID:SuperAwesomeCode,项目名称:SuperAwesomeCode,代码行数:37,代码来源:Crypto.cs

示例12: DecryptsToOriginalPlainText

			public void DecryptsToOriginalPlainText()
			{
				byte[] plaintextBytes = Encoding.UTF8.GetBytes("This is a test! It needs to be 128 characters long at least. This is a test! It needs to be 128 characters long at least. This is a test! It needs to be 128 characters long at least. This is a test! It needs to be 128 characters long at least. This is a test! It needs to be 128 characters long at least. This is a test! It needs to be 128 characters long at least. This is a test! It needs to be 128 characters long at least. This is a test! It needs to be 128 characters long at least.");
				byte[] decryptedBytes;

				using (SymmetricAlgorithm algorithm = new AesManaged())
				{
					byte[] wrongDecryptionKey = algorithm.Key;

					algorithm.GenerateKey();

					byte[] encryptionKey = algorithm.Key;

					Assert.AreNotEqual(encryptionKey, wrongDecryptionKey);

					byte[] ciphertextBytes, iv;
					using (Encryptor encryptor = algorithm.CreateEncryptor(encryptionKey, out iv))
					{
						Assert.AreEqual(encryptionKey, encryptor.Algorithm.Key);
						Assert.AreEqual(iv, encryptor.Algorithm.IV);

						ciphertextBytes = encryptor.Encrypt(plaintextBytes);
					}

					using (Decryptor decryptor = new Decryptor(algorithm, encryptionKey, iv, Encryption.DefaultOptions))
					{
						Assert.AreEqual(encryptionKey, decryptor.Algorithm.Key);
						Assert.AreEqual(iv, decryptor.Algorithm.IV);

						decryptedBytes = decryptor.Decrypt(ciphertextBytes);
					}
				}

				Assert.AreEqual(plaintextBytes, decryptedBytes);
			}
开发者ID:jimkropa,项目名称:misc.corlib,代码行数:35,代码来源:EncyrptionAndDecryptionTests.cs

示例13: Encrypt

 public string Encrypt(byte[] key, string dataToEncrypt)
 {
     // Initialize
     AesManaged encryptor = new AesManaged();
     // Set the key
     encryptor.Key = key;
     encryptor.IV = key;
     // create a memory stream
     using (MemoryStream encryptionStream = new MemoryStream())
     {
         // Create the crypto stream
         using (
             CryptoStream encrypt = new CryptoStream(encryptionStream, encryptor.CreateEncryptor(),
                 CryptoStreamMode.Write))
         {
             // Encrypt
             byte[] utfD1 = UTF8Encoding.UTF8.GetBytes(dataToEncrypt);
             encrypt.Write(utfD1, 0, utfD1.Length);
             encrypt.FlushFinalBlock();
             encrypt.Close();
             // Return the encrypted data
             return Convert.ToBase64String(encryptionStream.ToArray());
         }
     }
 }
开发者ID:pavel99,项目名称:Community-Medicine,代码行数:25,代码来源:CenterManager.cs

示例14: EncryptAES

 public byte[] EncryptAES(ParametroEntreda Entrada)
 {
     if (Entrada.plainText == null || Entrada.plainText.Length <= 0)
         throw new ArgumentNullException("plainText");
     if (Entrada.Key == null || Entrada.Key.Length <= 0)
         throw new ArgumentNullException("Key");
     if (Entrada.IV == null || Entrada.IV.Length <= 0)
         throw new ArgumentNullException("Key");
     byte[] encrypted;
     using (AesManaged aesAlg = new AesManaged())
     {
         aesAlg.Key = Entrada.Key;
         aesAlg.IV = Entrada.IV;
         ICryptoTransform encryptor = aesAlg.CreateEncryptor(aesAlg.Key, aesAlg.IV);
         using (MemoryStream msEncrypt = new MemoryStream())
         {
             using (CryptoStream csEncrypt = new CryptoStream(msEncrypt, encryptor, CryptoStreamMode.Write))
             {
                 using (StreamWriter swEncrypt = new StreamWriter(csEncrypt))
                 {
                     swEncrypt.Write(Entrada.plainText);
                 }
                 encrypted = msEncrypt.ToArray();
             }
         }
     }
     return encrypted;
 }
开发者ID:Romulomathiasferreira,项目名称:InfoTech2u,代码行数:28,代码来源:AES.cs

示例15: ForCodeCoverage

    public static bool ForCodeCoverage()
    {
        AesManaged aes = new AesManaged();
        using (ICryptoTransform cte = aes.CreateEncryptor(), ctd = aes.CreateDecryptor())
        {
            byte[] plain1 = new byte[64];
            for (int i = 0; i < plain1.Length; i++)
                plain1[i] = (byte)i;

            byte[] encr1 = cte.TransformFinalBlock(plain1, 0, plain1.Length);
            using(MemoryStream ms = new MemoryStream())
            using(CryptoStream cs = new CryptoStream(ms, ctd, CryptoStreamMode.Write))
            {
                cs.Write(encr1, 0, 16);
                cs.Write(encr1, 16, 16);
                cs.Write(encr1, 32, 16);
                cs.Write(encr1, 48, encr1.Length-48);
                cs.FlushFinalBlock();
                byte[] plain2 = ms.ToArray();

                if (!Compare(plain1, plain2))
                {
                    Console.WriteLine("CodeCoverage case failed");
                    return false;
                }
                return true;
            }
        }
    }
开发者ID:aura1213,项目名称:netmf-interpreter,代码行数:29,代码来源:AESTrans2.cs


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