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


C# RijndaelManaged.?.Clear方法代码示例

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


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

示例1: SifreleAes

        public static string SifreleAes(string text, string password)
        {
            if (string.IsNullOrEmpty(text))
                throw new ArgumentNullException(nameof(text));
            if (string.IsNullOrEmpty(password))
                throw new ArgumentNullException(nameof(password));

            string cikisString;
            RijndaelManaged aesAlgoritma = null;

            try {
                var key = new Rfc2898DeriveBytes(password, SaltDegeri);

                aesAlgoritma = new RijndaelManaged();
                aesAlgoritma.Key = key.GetBytes(aesAlgoritma.KeySize / 8);

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

                using (var msEncrypt = new MemoryStream()) {
                    msEncrypt.Write(BitConverter.GetBytes(aesAlgoritma.IV.Length), 0, sizeof(int));
                    msEncrypt.Write(aesAlgoritma.IV, 0, aesAlgoritma.IV.Length);
                    using (var csEncrypt = new CryptoStream(msEncrypt, encryptor, CryptoStreamMode.Write)) {
                        using (var swEncrypt = new StreamWriter(csEncrypt)) {
                            swEncrypt.Write(text);
                        }
                    }
                    cikisString = Convert.ToBase64String(msEncrypt.ToArray());
                }
            }
            finally {
                aesAlgoritma?.Clear();
            }

            return cikisString;
        }
开发者ID:ertseyhan,项目名称:Steg,代码行数:35,代码来源:Cryptography.cs

示例2: SifreCozAes

        public static string SifreCozAes(string text, string password)
        {
            if (string.IsNullOrEmpty(text))
                throw new ArgumentNullException(nameof(text));
            if (string.IsNullOrEmpty(password))
                throw new ArgumentNullException(nameof(password));

            RijndaelManaged aesAlg = null;
            string plainText;

            try {
                var key = new Rfc2898DeriveBytes(password, SaltDegeri);

                var bytes = Convert.FromBase64String(text);
                using (var msDecrypt = new MemoryStream(bytes))
                {
                    aesAlg = new RijndaelManaged();
                    aesAlg.Key = key.GetBytes(aesAlg.KeySize / 8);
                    aesAlg.IV = BaytDiziOku(msDecrypt);
                    var decryptor = aesAlg.CreateDecryptor(aesAlg.Key, aesAlg.IV);
                    using (var csDecrypt = new CryptoStream(msDecrypt, decryptor, CryptoStreamMode.Read)) {
                        using (var srDecrypt = new StreamReader(csDecrypt))
                            plainText = srDecrypt.ReadToEnd();
                    }
                }
            }
            finally {
                aesAlg?.Clear();
            }

            return plainText;
        }
开发者ID:ertseyhan,项目名称:Steg,代码行数:32,代码来源:Cryptography.cs

示例3: 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>
        protected string EncryptStringAes(string plainText, string sharedSecret)

        {
            string outStr = null; // Encrypted string to return
            byte[] salt = Encoding.UTF8.GetBytes($"{sharedSecret}{sharedSecret}{sharedSecret}{sharedSecret}");


            RijndaelManaged aesAlg = null; // RijndaelManaged object used to encrypt the data.
            try
            {
                // generate the key from the shared secret and the salt
                using (var key = new Rfc2898DeriveBytes(sharedSecret, salt))
                {
                    aesAlg = new RijndaelManaged();
                    aesAlg.Key = key.GetBytes(aesAlg.KeySize / 8);
                    aesAlg.IV = key.GetBytes(aesAlg.BlockSize / 8);
                }

                // Create a decrytor to perform the stream transform.
                using (ICryptoTransform 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))
                            {
                                //Write all data to the stream.
                                swEncrypt.Write(plainText);
                            }
                        }
                        outStr = Convert.ToBase64String(msEncrypt.ToArray());
                    }
                }
            }
            catch
            {
            }
            finally
            {
                // Clear the RijndaelManaged object.
                aesAlg?.Clear();
            }
            // Return the encrypted bytes from the memory stream.
            return outStr;
        }
开发者ID:paonath,项目名称:PH.PicoCrypt,代码行数:52,代码来源:Aes256.cs

示例4: DecryptStringAes

        /// <summary>
        /// Decrypt the given string.  Assumes the string was encrypted using 
        /// EncryptStringAES(), using an identical sharedSecret.
        /// </summary>
        /// <param name="cipherText">The text to decrypt.</param>
        /// <param name="sharedSecret">A password used to generate a key for decryption.</param>
        protected string DecryptStringAes(string cipherText, string sharedSecret)
        {
            if (string.IsNullOrEmpty(cipherText))
            {
                throw new ArgumentNullException("cipherText");
            }

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

            // Declare the RijndaelManaged object
            // used to decrypt the data.
            RijndaelManaged aesAlg = null;

            // Declare the string used to hold
            // the decrypted text.
            string plaintext = null;
            byte[] salt = Encoding.UTF8.GetBytes($"{sharedSecret}{sharedSecret}{sharedSecret}{sharedSecret}");

            try
            {
                // generate the key from the shared secret and the salt
                using (var key = new Rfc2898DeriveBytes(sharedSecret, salt))
                {
                    aesAlg = new RijndaelManaged();
                    aesAlg.Key = key.GetBytes(aesAlg.KeySize / 8);
                    aesAlg.IV = key.GetBytes(aesAlg.BlockSize / 8);
                }

                // Create a decrytor to perform the stream transform.
                using (ICryptoTransform decryptor = aesAlg.CreateDecryptor(aesAlg.Key, aesAlg.IV))
                {
                    byte[] bytes = Convert.FromBase64String(cipherText);
                    using (MemoryStream msDecrypt = new MemoryStream(bytes))
                    {
                        using (CryptoStream csDecrypt = new CryptoStream(msDecrypt, decryptor, CryptoStreamMode.Read))
                        {
                            using (StreamReader srDecrypt = new StreamReader(csDecrypt))

                                // Read the decrypted bytes from the decrypting stream
                                // and place them in a string.
                                plaintext = srDecrypt.ReadToEnd();
                        }
                    }
                }
            }
            catch
            {
            }
            finally
            {
                // Clear the RijndaelManaged object.
                aesAlg?.Clear();
            }
            return plaintext;
        }
开发者ID:paonath,项目名称:PH.PicoCrypt,代码行数:64,代码来源:Aes256.cs


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