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


C# AesManaged.Clear方法代码示例

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


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

示例1: Encrypt

        public static byte[] Encrypt(string text, string password, byte[] salt = null)
        {
            if (string.IsNullOrEmpty(text)) throw new ArgumentNullException("text");
            if (string.IsNullOrEmpty(password)) throw new ArgumentNullException("password");

            if (salt == null) salt = DefaultSalt;
            var aes = new AesManaged();

            byte[] encryptedData;

            try
            {
                aes.SetKey(password, salt);
                var encryptor = aes.CreateEncryptor(aes.Key, aes.IV);
                using (var ms = new MemoryStream())
                {
                    ms.Write(BitConverter.GetBytes(aes.IV.Length), 0, sizeof(int));
                    ms.Write(aes.IV, 0, aes.IV.Length);
                    using (var cs = new CryptoStream(ms, encryptor, CryptoStreamMode.Write))
                    using (var sw = new StreamWriter(cs))
                        sw.Write(text);
                    encryptedData = ms.ToArray();
                }
            }
            finally
            {
                aes.Clear();
            }

            return encryptedData;
        }
开发者ID:curtisrutland,项目名称:CryptoHelper,代码行数:31,代码来源:AESHelper.cs

示例2: Decrypt

        public byte[] Decrypt(byte[] cipher, byte[] key, byte[] IV)
        {
            System.Collections.Generic.List<byte> bytes = new List<byte>();

            using (AesManaged _aesAlgorithm = new AesManaged())
            {
                _aesAlgorithm.Key = key;
                _aesAlgorithm.IV = IV;

                using (ICryptoTransform decryptor = _aesAlgorithm.CreateDecryptor(_aesAlgorithm.Key, _aesAlgorithm.IV))
                {
                    using (System.IO.MemoryStream str = new System.IO.MemoryStream(cipher))
                    {
                        using (CryptoStream crypto = new CryptoStream(str, decryptor, CryptoStreamMode.Read))
                        {
                            int b = crypto.ReadByte();

                            while (b > -1)
                            {
                                b = crypto.ReadByte();
                                bytes.Add((byte)b);
                            }
                        }
                    }
                    _aesAlgorithm.Clear();
                }
            }

            return bytes.ToArray();
        }
开发者ID:shovelheadfxe,项目名称:DevLab,代码行数:30,代码来源:CMS.cs

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

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

示例5: Decryption

		public string Decryption() {
			try {
				byte[] encrypted=Convert.FromBase64String(textInput.Text);
				MemoryStream ms=null;
				CryptoStream cs=null;
				StreamReader sr=null;
				Aes aes=new AesManaged();
				aes.Key=key;
				aes.IV=new byte[16];
				ICryptoTransform decryptor=aes.CreateDecryptor(aes.Key,aes.IV);
				ms=new MemoryStream(encrypted);
				cs=new CryptoStream(ms,decryptor,CryptoStreamMode.Read);
				sr=new StreamReader(cs);
				string decrypted=sr.ReadToEnd();
				ms.Dispose();
				cs.Dispose();
				sr.Dispose();
				if(aes!=null) {
					aes.Clear();
				}
				return decrypted;
			}
			catch { 
				MessageBox.Show("Text entered was not valid encrypted text.");
				return"";
			}
		}
开发者ID:mnisl,项目名称:OD,代码行数:27,代码来源:FormEhrEncryption.cs

示例6: Decrypt

 public static string Decrypt(string str,byte[] key)
 {
     //No need to check RemotingRole; no call to db.
     if(str==""){
         return "";
     }
     try {
         byte[] encrypted=Convert.FromBase64String(str);
         MemoryStream ms=null;
         CryptoStream cs=null;
         StreamReader sr=null;
         Aes aes=new AesManaged();
         aes.Key=key;
         aes.IV=new byte[16];
         ICryptoTransform decryptor=aes.CreateDecryptor(aes.Key,aes.IV);
         ms=new MemoryStream(encrypted);
         cs=new CryptoStream(ms,decryptor,CryptoStreamMode.Read);
         sr=new StreamReader(cs);
         string decrypted=sr.ReadToEnd();
         ms.Dispose();
         cs.Dispose();
         sr.Dispose();
         if(aes!=null) {
             aes.Clear();
         }
         return decrypted;
     }
     catch {
         //MessageBox.Show("Text entered was not valid encrypted text.");
         return"";
     }
 }
开发者ID:nampn,项目名称:ODental,代码行数:32,代码来源:CentralConnections.cs

示例7: Decrypt

        public static string Decrypt(byte[] encryptedData, string password, byte[] salt = null)
        {
            if (encryptedData == null) throw new ArgumentNullException("encryptedData");
            if (string.IsNullOrEmpty(password)) throw new ArgumentNullException("password");

            if (salt == null) salt = DefaultSalt;
            var aes = new AesManaged();

            string text;

            try
            {
                aes.SetKey(password, salt);
                using (var ms = new MemoryStream(encryptedData))
                {
                    aes.IV = ms.GetIV();
                    var decryptor = aes.CreateDecryptor(aes.Key, aes.IV);
                    using (var cs = new CryptoStream(ms, decryptor, CryptoStreamMode.Read))
                    using (var sr = new StreamReader(cs))
                        text = sr.ReadToEnd();
                }
            }
            finally
            {
                aes.Clear();
            }

            return text;
        }
开发者ID:curtisrutland,项目名称:CryptoHelper,代码行数:29,代码来源:AESHelper.cs

示例8: Encrypt

		///<summary>Encrypts signature text and returns a base 64 string so that it can go directly into the database.</summary>
		public static string Encrypt(string str,byte[] key){
			//No need to check RemotingRole; no call to db.
			if(str==""){
				return "";
			}
			byte[] ecryptBytes=Encoding.UTF8.GetBytes(str);
			MemoryStream ms=new MemoryStream();
			CryptoStream cs=null;
			Aes aes=new AesManaged();
			aes.Key=key;
			aes.IV=new byte[16];
			ICryptoTransform encryptor=aes.CreateEncryptor(aes.Key,aes.IV);
			cs=new CryptoStream(ms,encryptor,CryptoStreamMode.Write);
			cs.Write(ecryptBytes,0,ecryptBytes.Length);
			cs.FlushFinalBlock();
			byte[] encryptedBytes=new byte[ms.Length];
			ms.Position=0;
			ms.Read(encryptedBytes,0,(int)ms.Length);
			cs.Dispose();
			ms.Dispose();
			if(aes!=null) {
				aes.Clear();
			}
			return Convert.ToBase64String(encryptedBytes);			
		}
开发者ID:mnisl,项目名称:OD,代码行数:26,代码来源:CentralConnections.cs

示例9: dbEncrypt

        public int dbEncrypt(String partition, int size, String data, out String dataOut)
        {
            AesManaged aes = null;
            MemoryStream memoryStream = null;
            CryptoStream cryptoStream = null;

            try
            {
                aes = new AesManaged();
                aes.Key = readKeyFromFile(partition);

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

                byte[] buf = Encoding.UTF8.GetBytes(data);
                cryptoStream.Write(buf, 0, buf.Length);
                cryptoStream.FlushFinalBlock();

                dataOut = Convert.ToBase64String(memoryStream.ToArray());
            }
            finally
            {
                if (cryptoStream != null)
                    cryptoStream.Close();

                if (memoryStream != null)
                    memoryStream.Close();

                if (aes != null)
                    aes.Clear();
            }

            return getErrorCode() == 0 ? 1 : 0;
        }
开发者ID:arissetyawan,项目名称:rhodes,代码行数:34,代码来源:RhoCrypt.cs

示例10: DecryptCBC

        public static byte[] DecryptCBC(byte[] cipherTextBodyBytes, byte[] secretBytes, byte[] saltBytes, int keySize)
        {
            byte[] key = null;
            byte[] iv = null;

            if (keySize == 128)
            {
                key = GetIteratedMD5(secretBytes, saltBytes, 1);
                iv = GetIteratedMD5(secretBytes, saltBytes, 2);
            }
            else if (keySize == 256)
            {
                byte[] keyA = GetIteratedMD5(secretBytes, saltBytes, 1);
                byte[] keyB = GetIteratedMD5(secretBytes, saltBytes, 2);
                key = new byte[keyA.Length + keyB.Length];

                Buffer.BlockCopy(keyA, 0, key, 0, keyA.Length); // merge keyA and keyB into key
                Buffer.BlockCopy(keyB, 0, key, keyA.Length, keyB.Length);

                iv = GetIteratedMD5(secretBytes, saltBytes, 3);
            }
            else
                throw new Exception("Key size must be 128 or 256");

            AesManaged aes = new AesManaged();
            aes.Mode = CipherMode.CBC;
            aes.Padding = PaddingMode.PKCS7;
            aes.KeySize = keySize;
            aes.BlockSize = 128;
            aes.Key = key;
            aes.IV = iv;

            byte[] clearTextBody = null;

            using (ICryptoTransform Decryptor = aes.CreateDecryptor())
            {
                using (MemoryStream mStream = new MemoryStream())
                {
                    using (CryptoStream CryptoStream = new CryptoStream(mStream, Decryptor, CryptoStreamMode.Write))
                    {
                        CryptoStream.Write(cipherTextBodyBytes, 0, cipherTextBodyBytes.Length);
                        CryptoStream.FlushFinalBlock();

                        clearTextBody = mStream.ToArray();
                        CryptoStream.Close();
                    }
                    mStream.Close();
                }
            }
            aes.Clear();

            return clearTextBody;
        }
开发者ID:SH4ZB0T,项目名称:AESCryptNET,代码行数:53,代码来源:AESCryptNet.cs

示例11: 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>
    public static 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.
        AesManaged aesAlg = null;

        // Declare the string used to hold
        // the decrypted text.
        string plaintext = null;

        try
        {
            // generate the key from the shared secret and the salt
            Rfc2898DeriveBytes key = new Rfc2898DeriveBytes(sharedSecret, _salt);

            // Create a RijndaelManaged object
            // with the specified key and IV.
            aesAlg = new AesManaged();
            aesAlg.Key = key.GetBytes(aesAlg.KeySize / 8);
            aesAlg.IV = key.GetBytes(aesAlg.BlockSize / 8);

            // Create a decrytor to perform the stream transform.
            ICryptoTransform decryptor = aesAlg.CreateDecryptor(aesAlg.Key, aesAlg.IV);
            // Create the streams used for decryption.
            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();
                }
            }
        }
        finally
        {
            // Clear the RijndaelManaged object.
            if (aesAlg != null)
                aesAlg.Clear();
        }

        return plaintext;
    }
开发者ID:crawford,项目名称:WpGatekeeper,代码行数:57,代码来源:Crypto.cs

示例12: Decrypt

        public static string Decrypt(string dataToDecrypt, string salt)
        {
            AesManaged aes = null;
            MemoryStream memoryStream = null;

            string password = "7b4b8593-ea55-4de8-bf86-02332fc41504";

            try
            {
                //Generate a Key based on a Password and HMACSHA1 pseudo-random number generator
                //Salt must be at least 8 bytes long
                //Use an iteration count of at least 1000
                Rfc2898DeriveBytes rfc2898 = new Rfc2898DeriveBytes(password, Encoding.UTF8.GetBytes(salt), 10000);

                //Create AES algorithm
                aes = new AesManaged();
                //Key derived from byte array with 32 pseudo-random key bytes
                aes.Key = rfc2898.GetBytes(32);
                //IV derived from byte array with 16 pseudo-random key bytes
                aes.IV = rfc2898.GetBytes(16);

                //Create Memory and Crypto Streams
                memoryStream = new MemoryStream();
                CryptoStream cryptoStream = new CryptoStream(memoryStream, aes.CreateDecryptor(), CryptoStreamMode.Write);

                //Decrypt Data
                byte[] data = Convert.FromBase64String(dataToDecrypt);
                cryptoStream.Write(data, 0, data.Length);
                cryptoStream.FlushFinalBlock();

                //Return Decrypted String
                byte[] decryptBytes = memoryStream.ToArray();

                //Dispose
                if (cryptoStream != null)
                    cryptoStream.Dispose();

                //Retval
                return Encoding.UTF8.GetString(decryptBytes, 0, decryptBytes.Length);
            }
            finally
            {
                if (memoryStream != null)
                    memoryStream.Dispose();

                if (aes != null)
                    aes.Clear();
            }
        }
开发者ID:mbmccormick,项目名称:Mojito,代码行数:49,代码来源:Security.cs

示例13: Decode

        public string Decode(string data)
        {
            AesManaged aes = new AesManaged();

            byte[] buf = CharUtil.DecodeString(data, mask);
            using (MemoryStream mStream = new MemoryStream())
            {
                using (CryptoStream cStream = new CryptoStream(mStream, aes.CreateDecryptor(keys, salt), CryptoStreamMode.Write))
                {
                    cStream.Write(buf, 0, buf.Length);
                    cStream.FlushFinalBlock();
                    buf = mStream.ToArray();
                }
            }
            aes.Clear();

            return Encoding.UTF8.GetString(buf, 0, buf.Length);
        }
开发者ID:burstas,项目名称:rmps,代码行数:18,代码来源:UserModel.cs

示例14: Decrypt

        private const string _salt = "salt"; // Not for UI. Do not change.

        #endregion Fields

        #region Methods

        public static string Decrypt(string dataToDecrypt, string password = "", string salt = "")
        {
            AesManaged aes = null;
            MemoryStream memoryStream = null;
            CryptoStream cryptoStream = null;

            try
            {
                if (password.CompareTo("") == 0) password = _password;
                if (salt.CompareTo("") == 0) salt = _salt;

                //Generate a Key based on a Password, Salt and HMACSHA1 pseudo-random number generator
                Rfc2898DeriveBytes rfc2898 = new Rfc2898DeriveBytes(password, Encoding.UTF8.GetBytes(salt));

                //Create AES algorithm with 256 bit key and 128-bit block size
                aes = new AesManaged();
                aes.Key = rfc2898.GetBytes(aes.KeySize / 8);
                aes.IV = rfc2898.GetBytes(aes.BlockSize / 8);

                //Create Memory and Crypto Streams
                memoryStream = new MemoryStream();
                cryptoStream = new CryptoStream(memoryStream, aes.CreateDecryptor(), CryptoStreamMode.Write);

                //Decrypt Data
                byte[] data = Convert.FromBase64String(dataToDecrypt);
                cryptoStream.Write(data, 0, data.Length);
                cryptoStream.FlushFinalBlock();

                //Return Decrypted String
                byte[] decryptBytes = memoryStream.ToArray();
                return Encoding.UTF8.GetString(decryptBytes, 0, decryptBytes.Length);
            }
            finally
            {
                if (cryptoStream != null)
                    cryptoStream.Dispose();

                if (memoryStream != null)
                    memoryStream.Dispose();

                if (aes != null)
                    aes.Clear();
            }
        }
开发者ID:VladimirBabiy,项目名称:HelpersAndUtils,代码行数:50,代码来源:AesEncryptor.cs

示例15: Decrypt

        public static bool Decrypt(string cliperText, string key, out string originText)
        {
            int cliperTextLength = cliperText.Length;
            if (cliperTextLength % 2 != 0)
            {
                originText = String.Empty;
                return false;
            }

            byte[] inputData = new byte[cliperTextLength / 2];
            for (int i = 0; i < inputData.Length; i++)
            {
                inputData[i] = Convert.ToByte(cliperText.Substring(i * 2, 2), 16);
            }

            var aes = new AesManaged()
            {
                Padding = _padding,
                Mode = _cipherMode,
                IV = ORIGIN_IV,
                Key = Encoding.UTF8.GetBytes(key.PadRight(KEY_LENGTH, PADDING_CHAR).Substring(0, KEY_LENGTH))
            };

            var decryptor = aes.CreateDecryptor();
            bool decryptSuccess = true;
            try
            {
                byte[] decryptedData = decryptor.TransformFinalBlock(inputData, 0, inputData.Length);
                originText = Encoding.UTF8.GetString(decryptedData);
            }
            catch
            {
                originText = String.Empty;
            }
            finally
            {
                decryptor.Dispose();
                if (aes != null)
                {
                    aes.Clear();
                }
            }
            return decryptSuccess;
        }
开发者ID:JerryXia,项目名称:ML,代码行数:44,代码来源:AesCryptoService.cs


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