當前位置: 首頁>>代碼示例>>C#>>正文


C# AesManaged.GenerateKey方法代碼示例

本文整理匯總了C#中System.Security.Cryptography.AesManaged.GenerateKey方法的典型用法代碼示例。如果您正苦於以下問題:C# AesManaged.GenerateKey方法的具體用法?C# AesManaged.GenerateKey怎麽用?C# AesManaged.GenerateKey使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在System.Security.Cryptography.AesManaged的用法示例。


在下文中一共展示了AesManaged.GenerateKey方法的15個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的C#代碼示例。

示例1: CreateNewAESSymmetricKeyset

        /// <summary>
        /// Creates a symmetric key.  See this link for more information behind the numbers
        /// http://blogs.msdn.com/b/shawnfa/archive/2006/10/09/the-differences-between-rijndael-and-aes.aspx
        /// </summary>
        /// <returns></returns>
        private SymmetricKey CreateNewAESSymmetricKeyset()
        {
            if (cert == null)
            {
                throw new InvalidOperationException("Unable to create new AES keyset; Certificate not loaded.");
            }

            byte[] symmKey, iv;

            using (AesManaged aes = new AesManaged())
            {
                aes.GenerateIV();
                aes.GenerateKey();

                symmKey = aes.Key;
                iv = aes.IV;

                aes.Clear();
            }

            // Encrypt the Symmetric Key for storage
            byte[] encryptedKey = EncryptRSA(symmKey, cert);

            SymmetricKey symmKeySet = new SymmetricKey() {
                iv = iv,
                Key = encryptedKey,
                CertificateThumbprint = cert.Thumbprint
            };

            return symmKeySet;
        }
開發者ID:justin,項目名稱:AzureTableEncryption,代碼行數:36,代碼來源:AzureTableKeyGenerator.cs

示例2: 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

示例3: Encrypt

        public EncryptedMessage Encrypt(byte[] input)
        {
            var em = new EncryptedMessage();
            using (var aes = new AesManaged())
            {
                aes.KeySize = 256;

                aes.GenerateIV();
                aes.GenerateKey();

                em.IV = aes.IV;
                em.Key = aes.Key;
                em.EncryptionType = MessageEncryptionType.Aes;

                using (var encryptor = aes.CreateEncryptor())
                {
                    using (var msOutput = new MemoryStream())
                    {
                        using (var cryptoStream = new CryptoStream(msOutput, encryptor, CryptoStreamMode.Write)) {
                            using (var msInput = new MemoryStream(input)) {
                                msInput.CopyTo(cryptoStream);
                            }
                        }
                        em.CipherBytes = msOutput.ToArray();
                    }
                }

                return em;
            }
        }
開發者ID:piksel,項目名稱:LibNemesis.NET,代碼行數:30,代碼來源:Rijndael.cs

示例4: AESEncryption

 public AESEncryption()
 {
     Algorithm = new AesManaged();
     Algorithm.KeySize = 256;
     Algorithm.Mode = CipherMode.CBC;
     Algorithm.GenerateIV();
     Algorithm.GenerateKey();
     Algorithm.Padding = PaddingMode.PKCS7;
 }
開發者ID:superit23,項目名稱:Yggdrasil,代碼行數:9,代碼來源:AESEncryption.cs

示例5: GenerateAESKey

 public static byte[] GenerateAESKey()
 {
     using (var provider = new AesManaged())
     {
         provider.KeySize = 256;
         provider.GenerateKey();
         return provider.Key;
     }
 }
開發者ID:WaysGRP2,項目名稱:Project.NET,代碼行數:9,代碼來源:ReversibleEncryption.cs

示例6: GenerateNewAesKey

 /// <summary>
 /// Generates a new random 256bit AES key.
 /// This can be used for the encryption of keys in the HOTP/TOTP classes
 /// </summary>
 /// <returns>256bit AES key</returns>
 public static byte[] GenerateNewAesKey()
 {
     using (AesManaged aesAlg = new AesManaged())
     {
         aesAlg.KeySize = 256;
         aesAlg.GenerateKey();
         return aesAlg.Key;
     }
 }
開發者ID:harshitc,項目名稱:SharpOtpLib,代碼行數:14,代碼來源:Utils.cs

示例7: GenerateEncryptionParameters

        private void GenerateEncryptionParameters(X509Certificate2 certificate)
        {
            var aesManaged = new AesManaged();
            aesManaged.GenerateIV();
            aesManaged.GenerateKey();

            string ivToStoreInConfig = Convert.ToBase64String(aesManaged.IV);
            string encryptionKeyAsString = Convert.ToBase64String(aesManaged.Key);
            string keyToStoreInConfig = RsaEncrypt(certificate, encryptionKeyAsString);

            ConfigurationManager.AppSettings["EncryptionIV"] = ivToStoreInConfig;
            ConfigurationManager.AppSettings["EncryptionKey"] = keyToStoreInConfig;
        }
開發者ID:tomasking,項目名稱:CryptographyDemo,代碼行數:13,代碼來源:HybridEncryptionDemo.cs

示例8: GenerateAESKeys

        private static void GenerateAESKeys()
        {
            var sw = Stopwatch.StartNew();
            var aesM = new AesManaged();
            for (int i = 0; i < NUM_AES_KEYS; i++)
            {
                aesM.GenerateKey();
                byte[] result = aesM.Key;
                string hexString = ConvertToHexString(result);
                //Console.WriteLine("AES KEY{0}",hexString);
            }
            //   Console.WriteLine("AES KEY:" + sw.Elapsed.ToString());

        }
開發者ID:qiao-snail,項目名稱:TheCode,代碼行數:14,代碼來源:Program.cs

示例9: CreateNewAesSymmetricKeyset

        public SymmetricKey CreateNewAesSymmetricKeyset()
        {
            var aes = new AesManaged();
            aes.GenerateIV();
            aes.GenerateKey();

            var symmKeySet = new SymmetricKey()
            {
                Iv = RsaEncryptBytes(aes.IV),
                Key = RsaEncryptBytes(aes.Key)
            };

            return symmKeySet;
        }
開發者ID:VijitJain,項目名稱:Azure.Security,代碼行數:14,代碼來源:RsaHelper.cs

示例10: TokenCrypt

 static TokenCrypt()
 {
     try
     {
         CryptKey = JsonHelper.ParseJson<AesKey>(File.ReadAllText(Path.Combine(AppDomain.CurrentDomain.BaseDirectory, KeyFile)).Trim());
     }
     catch(FileNotFoundException)
     {
         using(var aes = new AesManaged())
         {
             aes.GenerateKey();
             aes.GenerateIV();
             CryptKey = new AesKey { Key = aes.Key, IV = aes.IV };
             File.WriteAllText(Path.Combine(AppDomain.CurrentDomain.BaseDirectory, KeyFile), CryptKey.ToJsonString());
         }
     }
 }
開發者ID:HackerDom,項目名稱:ructfe-2015,代碼行數:17,代碼來源:TokenCrypt.cs

示例11: UnityCryptoManager

        public UnityCryptoManager()
        {
            m_RSA_Exch_Formatter = new RiaWebSoftRu.Cryptography.Crypto.Formater.RSAESPKCSFormater();
            m_RSA = new RiaWebSoftRu.Cryptography.Crypto.RsaManaged(m_KeySize);
            m_SHA = new SHA256Managed();
            m_RIJ = new AesManaged();
            //m_RSA.GenerateKey(); Not necessary in unity.  Unity clients gets a public key from server and uses that to encrypt the Rijndael key

            m_SHA                    = new SHA256Managed();

            m_RIJ.Padding            = PaddingMode.PKCS7;
            m_RIJ.Mode               = CipherMode.CBC;
            m_RIJ.IV                 = new byte[16];
            m_RIJ.BlockSize          = 128;
            m_RIJ.KeySize            = 256;
            m_RIJ.GenerateKey();

            m_PublicKey = Encoding.UTF8.GetBytes(m_RSA.ToXmlString(false).ToString());
            m_PrivateKey = Encoding.UTF8.GetBytes(m_RSA.ToXmlString(true).ToString());
        }
開發者ID:kamilion,項目名稱:WISP,代碼行數:20,代碼來源:UnityCryptoManager.cs

示例12: AESCipher

        public AESCipher()
        {
            this.utf8Encoding = new System.Text.UTF8Encoding();
            this.aes = new AesManaged();
            this.aes.Mode = CipherMode.CBC;
            this.aes.Padding = PaddingMode.PKCS7;
            this.aes.KeySize = 128;
            this.aes.BlockSize = 128;

            aes.GenerateKey();
            aes.GenerateIV();

            if (File.Exists(@"C:\Users\Chris\Documents\Cambridge\Part II Project\Code\Encryption Example\EncryptionExample\AESkey.xml") == true)
                File.Delete(@"C:\Users\Chris\Documents\Cambridge\Part II Project\Code\Encryption Example\EncryptionExample\AESkey.xml");

            //provide AES encryption key params
            FileStream fs = new FileStream(@"C:\Users\Chris\Documents\Cambridge\Part II Project\Code\Encryption Example\EncryptionExample\AESkey.xml", FileMode.CreateNew, FileAccess.ReadWrite);
            StreamWriter sw = new StreamWriter(fs);
            string aesKeyXML = byte2Hex(aes.Key);
            sw.Write("<AESkey>"+ aesKeyXML + "</AESkey>");
            sw.Close();
            fs.Close();
        }
開發者ID:Chrisjm89,項目名稱:EncryptedFacebook,代碼行數:23,代碼來源:AESCipher.cs

示例13: Encrypt

        /**
         * Encrypts a specified string using AES encryption and returns
         * the encrypted string and key used.
         */
        public static string[] Encrypt(string str)
        {
            byte[] bytes = Encoding.UTF8.GetBytes(str);

            var manager = new AesManaged
            {
                KeySize = 256,
                Padding = PaddingMode.PKCS7
            };

            manager.GenerateKey();
            manager.GenerateIV();

            ICryptoTransform c = manager.CreateEncryptor();

            bytes = c.TransformFinalBlock(bytes, 0, bytes.Length);

            string hash = BitConverter.ToString(manager.IV).Replace("-", "");
            hash += BitConverter.ToString(bytes).Replace("-", "");
            string key = BitConverter.ToString(manager.Key).Replace("-", "");

            return new[] {hash, key};
        }
開發者ID:WildAndrewLee,項目名稱:File-Mask,代碼行數:27,代碼來源:Crypt.cs

示例14: EncryptStringToByte

        public static byte[] EncryptStringToByte(string sourceText,ref byte[] key,ref byte[] IV)
        {
            if (sourceText == null || sourceText == "")
            {
                return null;
            }

            byte[] result;
            AesManaged aesAlg = new AesManaged();

            if (key == null || key.Length <= 0 || IV == null || IV.Length <= 0)
            {
                aesAlg.GenerateKey();
                aesAlg.GenerateIV();

                key = aesAlg.Key;
                IV = aesAlg.IV;
            }
            else
            {
                aesAlg.Key = key;
                aesAlg.IV = IV;
            }

            ICryptoTransform encryptor = aesAlg.CreateEncryptor();

            MemoryStream msEncrypt = new MemoryStream();
            CryptoStream csEncrypt = new CryptoStream(msEncrypt,encryptor, CryptoStreamMode.Write);
            StreamWriter swEncrypt = new StreamWriter(csEncrypt);

            swEncrypt.Write(sourceText);
            swEncrypt.Close();
            csEncrypt.Close();
            result = msEncrypt.ToArray();

            return result;
        }
開發者ID:darthrado,項目名稱:LoLoLoL,代碼行數:37,代碼來源:AES.cs

示例15: RsaHelperBytesShouldSucceed

        public void RsaHelperBytesShouldSucceed()
        {
            var directory = TestContext.DeploymentDirectory;
            var helper = new RsaHelper(Path.Combine(directory, "TestCertificate.pfx"), CertificatePassword);

            var aes = new AesManaged();
            aes.GenerateIV();
            aes.GenerateKey();

            var originalKey = aes.Key;
            var originalIv = aes.IV;

            var encryptedKeyBytes = helper.RsaEncryptBytes(aes.Key);
            var encryptedIvBytes = helper.RsaEncryptBytes(aes.IV);

            encryptedIvBytes.Should().NotBeNull("IV failed to encrypt");
            encryptedKeyBytes.Should().NotBeNull("Key failed to encrypt");

            var decryptedKeyBytes = helper.RsaDecryptToBytes(encryptedKeyBytes);
            var decryptedIvBytes = helper.RsaDecryptToBytes(encryptedIvBytes);

            originalKey.ShouldBeEquivalentTo(decryptedKeyBytes);
            originalIv.ShouldBeEquivalentTo(decryptedIvBytes);
        }
開發者ID:VijitJain,項目名稱:Azure.Security,代碼行數:24,代碼來源:RsaHelperTests.cs


注:本文中的System.Security.Cryptography.AesManaged.GenerateKey方法示例由純淨天空整理自Github/MSDocs等開源代碼及文檔管理平台,相關代碼片段篩選自各路編程大神貢獻的開源項目,源碼版權歸原作者所有,傳播和使用請參考對應項目的License;未經允許,請勿轉載。