本文整理汇总了C#中System.Security.Cryptography.RijndaelManaged.Clear方法的典型用法代码示例。如果您正苦于以下问题:C# RijndaelManaged.Clear方法的具体用法?C# RijndaelManaged.Clear怎么用?C# RijndaelManaged.Clear使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类System.Security.Cryptography.RijndaelManaged
的用法示例。
在下文中一共展示了RijndaelManaged.Clear方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: Criptografar
public static string Criptografar(string texto)
{
if(string.IsNullOrEmpty(texto))
return String.Empty;
string outStr;
RijndaelManaged aesAlg = null;
try
{
var key = new Rfc2898DeriveBytes(Segredo, Complemento);
aesAlg = new RijndaelManaged();
aesAlg.Key = key.GetBytes(aesAlg.KeySize / 8);
aesAlg.IV = key.GetBytes(aesAlg.BlockSize / 8);
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(texto);
}
}
outStr = Convert.ToBase64String(msEncrypt.ToArray());
}
}
finally
{
if (aesAlg != null)
aesAlg.Clear();
}
return outStr;
}
示例2: Decrypt
public string Decrypt(string strEncryptedText)
{
if (strEncryptedText == null || strEncryptedText.Equals(""))
return strEncryptedText;
string strDecryptedText = null;
RijndaelManaged rijndael = new RijndaelManaged();
ICryptoTransform decryptor = rijndael.CreateDecryptor(Key, IV);
byte[] byteEncryptedText = Convert.FromBase64String(strEncryptedText);
MemoryStream memStream = new MemoryStream(byteEncryptedText);
CryptoStream decryptStream = null;
try
{
decryptStream = new CryptoStream(memStream, decryptor, CryptoStreamMode.Read);
byte[] byteDecryptedText = new byte[byteEncryptedText.Length];
int decryptedByteCount = decryptStream.Read(byteDecryptedText, 0, byteDecryptedText.Length);
strDecryptedText = Encoding.UTF8.GetString(byteDecryptedText, 0, decryptedByteCount);
}
finally
{
if (rijndael != null) rijndael.Clear();
if (decryptor != null) decryptor.Dispose();
if (memStream != null) memStream.Close();
if (decryptStream != null) decryptStream.Close();
}
if (UseSalt)
strDecryptedText = strDecryptedText.Substring(8);
return strDecryptedText;
}
示例3: Descriptografar
public static string Descriptografar(string codigo)
{
try
{
if (string.IsNullOrEmpty(codigo))
return String.Empty;
string retorno;
var chave = new Rfc2898DeriveBytes(Segredo, Complemento);
var algoritimo = new RijndaelManaged();
algoritimo.Key = chave.GetBytes(algoritimo.KeySize / 8);
algoritimo.IV = chave.GetBytes(algoritimo.BlockSize / 8);
var descriptografor = algoritimo.CreateDecryptor(algoritimo.Key, algoritimo.IV);
var bytes = Convert.FromBase64String(codigo);
using (var memoryStream = new MemoryStream(bytes))
using (var cryptoStream = new CryptoStream(memoryStream, descriptografor, CryptoStreamMode.Read))
using (var streamReader = new StreamReader(cryptoStream))
retorno = streamReader.ReadToEnd();
algoritimo.Clear();
return retorno;
}
catch (Exception)
{
return "DEU PAU";
}
}
示例4: AESDecryptWithoutVector
/// <summary>
/// AES解密(无向量)
/// </summary>
/// <param name="encryptedBytes">被加密的明文</param>
/// <param name="key">密钥</param>
/// <returns>明文</returns>
public static string AESDecryptWithoutVector(String Data, String Key)
{
Byte[] encryptedBytes = Convert.FromBase64String(Data);
Byte[] bKey = new Byte[32];
Array.Copy(Encoding.UTF8.GetBytes(Key.PadRight(bKey.Length)), bKey, bKey.Length);
MemoryStream mStream = new MemoryStream(encryptedBytes);
//mStream.Write( encryptedBytes, 0, encryptedBytes.Length );
//mStream.Seek( 0, SeekOrigin.Begin );
RijndaelManaged aes = new RijndaelManaged();
aes.Mode = CipherMode.ECB;
aes.Padding = PaddingMode.PKCS7;
aes.KeySize = 128;
aes.Key = bKey;
//aes.IV = _iV;
CryptoStream cryptoStream = new CryptoStream(mStream, aes.CreateDecryptor(), CryptoStreamMode.Read);
try
{
byte[] tmp = new byte[encryptedBytes.Length + 32];
int len = cryptoStream.Read(tmp, 0, encryptedBytes.Length + 32);
byte[] ret = new byte[len];
Array.Copy(tmp, 0, ret, 0, len);
return Encoding.UTF8.GetString(ret);
}
finally
{
cryptoStream.Close();
mStream.Close();
aes.Clear();
}
}
示例5: DecryptFile
public static void DecryptFile(string inputFile, string outputFile, string skey)
{
RijndaelManaged aes = new RijndaelManaged();
try
{
byte[] key = ASCIIEncoding.UTF8.GetBytes(skey);
using (FileStream fsCrypt = new FileStream(inputFile, FileMode.Open))
{
using (FileStream fsOut = new FileStream(outputFile, FileMode.Create))
{
using (CryptoStream cs = new CryptoStream(fsCrypt, aes.CreateDecryptor(key, key), CryptoStreamMode.Read))
{
int data;
while ((data = cs.ReadByte()) != -1)
{
fsOut.WriteByte((byte)data);
}
aes.Clear();
}
}
}
}
catch (Exception ex)
{
Console.WriteLine(ex.Message);
aes.Clear();
}
}
示例6: AesDecrypt
/// <summary>
/// Decrypts input string from Rijndael (AES) algorithm with CBC blocking and PKCS7 padding.
/// </summary>
/// <param name="inputBytes">Encrypted binary array to decrypt</param>
/// <param name="password"></param>
/// <returns>string of Decrypted data</returns>
/// <remarks>The key and IV are the same, and use strEncryptionPassword.</remarks>
private static string AesDecrypt(byte[] inputBytes, string password)
{
var AES = new RijndaelManaged
{
Padding = PaddingMode.PKCS7,
Mode = CipherMode.CBC,
KeySize = 128,
BlockSize = 128
};
byte[] keyAndIvBytes = Encoding.UTF8.GetBytes(password);
//create streams and decryptor object
try
{
using (var memoryStream = new MemoryStream(inputBytes))
{
using (
var cryptoStream = new CryptoStream(memoryStream,
AES.CreateDecryptor(keyAndIvBytes, keyAndIvBytes),
CryptoStreamMode.Read))
{
var outputBytes = new byte[inputBytes.Length + 1];
cryptoStream.Read(outputBytes, 0, outputBytes.Length);
return Encoding.UTF8.GetString(outputBytes);
}
}
}
finally
{
AES.Clear();
}
}
示例7: ReadObjectFromXml
private static object ReadObjectFromXml(string path)
{
try
{
object ob;
using (Stream fs = new FileStream(path, FileMode.Open))
{
var rm = new RijndaelManaged();
rm.BlockSize = rm.KeySize;
var secretKey = new PasswordDeriveBytes(Encoding.Unicode.GetBytes(Salt),
Encoding.ASCII.GetBytes(Salt));
// ReSharper disable once CSharpWarnings::CS0618
rm.Key = secretKey.GetBytes(rm.KeySize/8);
rm.IV = rm.Key;
var desEncrypt = rm.CreateDecryptor();
using (var cs = new CryptoStream(fs, desEncrypt, CryptoStreamMode.Read))
{
var xmlser = new XmlSerializer(typeof (Data));
ob = xmlser.Deserialize(cs);
}
rm.Clear();
desEncrypt.Dispose();
}
return ob;
}
catch (Exception ex)
{
Logger.Write(ex.Message);
return null;
}
}
示例8: DecryptIt
public String DecryptIt(String s, byte[] key = null, byte[] IV = null, PaddingMode padding = PaddingMode.PKCS7)
{
String result;
//magically assign key and IV if one isn't given as an argument
key = key ?? cryptKey;
IV = IV ?? cryptIV;
RijndaelManaged rijn = new RijndaelManaged();
rijn.Mode = CipherMode.CBC;
rijn.Padding = padding;
rijn.BlockSize = 256;
using (MemoryStream msDecrypt = new MemoryStream(Convert.FromBase64String(s)))
{
using (ICryptoTransform decryptor = rijn.CreateDecryptor(key, IV))
{
using (CryptoStream csDecrypt = new CryptoStream(msDecrypt, decryptor, CryptoStreamMode.Read))
{
using (StreamReader swDecrypt = new StreamReader(csDecrypt))
{
result = swDecrypt.ReadToEnd();
}
}
}
}
rijn.Clear();
return result;
}
示例9: EncryptData
public static string EncryptData(string strData, string key)
{
if (string.IsNullOrEmpty(strData))
return "";
byte[] InitialVectorBytes = Encoding.ASCII.GetBytes(_initVector);
byte[] SaltValueBytes = Encoding.ASCII.GetBytes(_salt);
byte[] PlainTextBytes = Encoding.UTF8.GetBytes(strData);
PasswordDeriveBytes DerivedPassword = new PasswordDeriveBytes(key, SaltValueBytes, _hashAlgo, _passIter);
byte[] KeyBytes = DerivedPassword.GetBytes(_keySize / 8);
RijndaelManaged SymmetricKey = new RijndaelManaged();
SymmetricKey.Mode = CipherMode.CBC;
byte[] CipherTextBytes = null;
using (ICryptoTransform Encryptor = SymmetricKey.CreateEncryptor(KeyBytes, InitialVectorBytes))
{
using (MemoryStream MemStream = new MemoryStream())
{
using (CryptoStream CryptoStream = new CryptoStream(MemStream, Encryptor, CryptoStreamMode.Write))
{
CryptoStream.Write(PlainTextBytes, 0, PlainTextBytes.Length);
CryptoStream.FlushFinalBlock();
CipherTextBytes = MemStream.ToArray();
MemStream.Close();
CryptoStream.Close();
}
}
}
SymmetricKey.Clear();
return Convert.ToBase64String(CipherTextBytes);
}
示例10: Decrypt
/// <summary>
/// Decrypts a string
/// </summary>
/// <param name="cipherText">Text to be decrypted</param>
/// <param name="password">Password to decrypt with</param>
/// <param name="salt">Salt to decrypt with</param>
/// <param name="hashAlgorithm">Can be either SHA1 or MD5</param>
/// <param name="passwordIterations">Number of iterations to do. The number of times the algorithm is run on the text. </param>
/// <param name="initialVector">Needs to be 16 ASCII characters long</param>
/// <param name="keySize">Can be 128, 192, or 256</param>
/// <returns>A decrypted string</returns>
public static string Decrypt(string cipherText, string password, string salt = "69ad1bfbd6605f3f6a3011460cdfb9db7757e0f9", string hashAlgorithm = "SHA1", int passwordIterations = 2, string initialVector = "OFRna73m*aze01xY", int keySize = 256)
{
if (string.IsNullOrEmpty(cipherText))
return "";
byte[] initialVectorBytes = Encoding.ASCII.GetBytes(initialVector);
byte[] saltValueBytes = Encoding.ASCII.GetBytes(salt);
byte[] cipherTextBytes = Convert.FromBase64String(cipherText);
var derivedPassword = new PasswordDeriveBytes(password, saltValueBytes, hashAlgorithm, passwordIterations);
byte[] keyBytes = derivedPassword.GetBytes(keySize / 8);
var symmetricKey = new RijndaelManaged {Mode = CipherMode.CBC};
byte[] plainTextBytes = new byte[cipherTextBytes.Length];
int byteCount;
using (ICryptoTransform decryptor = symmetricKey.CreateDecryptor(keyBytes, initialVectorBytes))
{
using (MemoryStream memStream = new MemoryStream(cipherTextBytes))
{
using (CryptoStream cryptoStream = new CryptoStream(memStream, decryptor, CryptoStreamMode.Read))
{
byteCount = cryptoStream.Read(plainTextBytes, 0, plainTextBytes.Length);
memStream.Close();
cryptoStream.Close();
}
}
}
symmetricKey.Clear();
return Encoding.UTF8.GetString(plainTextBytes, 0, byteCount);
}
示例11: Decrypt
/// <summary>
/// Decrypts a string
/// </summary>
/// <param name="CipherText">Text to be decrypted</param>
/// <param name="Password">Password to decrypt with</param>
/// <param name="Salt">Salt to decrypt with</param>
/// <param name="HashAlgorithm">Can be either SHA1 or MD5</param>
/// <param name="PasswordIterations">Number of iterations to do</param>
/// <param name="InitialVector">Needs to be 16 ASCII characters long</param>
/// <param name="KeySize">Can be 128, 192, or 256</param>
/// <returns>A decrypted string</returns>
public static string Decrypt(string CipherText, string Password,
string Salt = "Kosher", string HashAlgorithm = "SHA1",
int PasswordIterations = 2, string InitialVector = "OFRna73m*aze01xY",
int KeySize = 256)
{
if (string.IsNullOrEmpty(CipherText))
return "";
byte[] InitialVectorBytes = Encoding.ASCII.GetBytes(InitialVector);
byte[] SaltValueBytes = Encoding.ASCII.GetBytes(Salt);
byte[] CipherTextBytes = Convert.FromBase64String(CipherText);
PasswordDeriveBytes DerivedPassword = new PasswordDeriveBytes(Password, SaltValueBytes, HashAlgorithm, PasswordIterations);
byte[] KeyBytes = DerivedPassword.GetBytes(KeySize / 8);
RijndaelManaged SymmetricKey = new RijndaelManaged();
SymmetricKey.Mode = CipherMode.CBC;
byte[] PlainTextBytes = new byte[CipherTextBytes.Length];
int ByteCount = 0;
using (ICryptoTransform Decryptor = SymmetricKey.CreateDecryptor(KeyBytes, InitialVectorBytes))
{
using (MemoryStream MemStream = new MemoryStream(CipherTextBytes))
{
using (CryptoStream CryptoStream = new CryptoStream(MemStream, Decryptor, CryptoStreamMode.Read))
{
ByteCount = CryptoStream.Read(PlainTextBytes, 0, PlainTextBytes.Length);
MemStream.Close();
CryptoStream.Close();
}
}
}
SymmetricKey.Clear();
return Encoding.UTF8.GetString(PlainTextBytes, 0, ByteCount);
}
示例12: Decrypt
public static byte[] Decrypt(byte[] cipherBytes)
{
RijndaelManaged Crypto = null;
MemoryStream ms = null;
ICryptoTransform Decryptor = null;
CryptoStream cryptoStream = null;
Byte[] plainTextBytes = new byte[cipherBytes.Length];
using (Crypto = new RijndaelManaged())
{
Crypto.Key = aesKey;
Crypto.IV = aesIV;
ms = new MemoryStream(cipherBytes);
Decryptor = Crypto.CreateDecryptor(Crypto.Key, Crypto.IV);
cryptoStream = new CryptoStream(ms, Decryptor, CryptoStreamMode.Read);
cryptoStream.Read(plainTextBytes, 0, plainTextBytes.Length);
if (Crypto != null)
Crypto.Clear();
ms.Flush();
ms.Close();
}
int i = plainTextBytes.Length - 1;
while (plainTextBytes[i] == 0)
--i;
byte[] finalResule = new byte[i + 1];
Array.Copy(plainTextBytes, finalResule, i + 1);
return finalResule;
}
示例13: Encrypt
/// <summary>
/// Encrypts a string
/// </summary>
/// <param name="PlainText">Text to be encrypted</param>
/// <param name="Password">Password to encrypt with</param>
/// <param name="Salt">Salt to encrypt with</param>
/// <param name="HashAlgorithm">Can be either SHA1 or MD5</param>
/// <param name="PasswordIterations">Number of iterations to do</param>
/// <param name="InitialVector">Needs to be 16 ASCII characters long</param>
/// <param name="KeySize">Can be 128, 192, or 256</param>
/// <returns>An encrypted string</returns>
public static string Encrypt(string PlainText, string Password,
string Salt = "Kosher", string HashAlgorithm = "SHA1",
int PasswordIterations = 2, string InitialVector = "OFRna73m*aze01xY",
int KeySize = 256)
{
if (string.IsNullOrEmpty(PlainText))
return "";
byte[] InitialVectorBytes = Encoding.ASCII.GetBytes(InitialVector);
byte[] SaltValueBytes = Encoding.ASCII.GetBytes(Salt);
byte[] PlainTextBytes = Encoding.UTF8.GetBytes(PlainText);
PasswordDeriveBytes DerivedPassword = new PasswordDeriveBytes(Password, SaltValueBytes, HashAlgorithm, PasswordIterations);
byte[] KeyBytes = DerivedPassword.GetBytes(KeySize / 8);
RijndaelManaged SymmetricKey = new RijndaelManaged();
SymmetricKey.Mode = CipherMode.CBC;
byte[] CipherTextBytes = null;
using (ICryptoTransform Encryptor = SymmetricKey.CreateEncryptor(KeyBytes, InitialVectorBytes))
{
using (MemoryStream MemStream = new MemoryStream())
{
using (CryptoStream CryptoStream = new CryptoStream(MemStream, Encryptor, CryptoStreamMode.Write))
{
CryptoStream.Write(PlainTextBytes, 0, PlainTextBytes.Length);
CryptoStream.FlushFinalBlock();
CipherTextBytes = MemStream.ToArray();
MemStream.Close();
CryptoStream.Close();
}
}
}
SymmetricKey.Clear();
return Convert.ToBase64String(CipherTextBytes);
}
示例14: AESDecrypt
public static string AESDecrypt(string decryptString, string key)
{
byte[] inputData = new byte[decryptString.Length / 2]; Convert.FromBase64String(decryptString);
int arrayIndex = 0;
foreach (byte b in inputData)
{
inputData[arrayIndex] = (byte)Convert.ToInt32(decryptString.Substring(arrayIndex * 2, 2), 16);
arrayIndex = arrayIndex + 1;
}
RijndaelManaged rijndaelManager = new RijndaelManaged();
rijndaelManager.Key = Encoding.UTF8.GetBytes(key.PadRight(32, ' ').Substring(0, 32));
rijndaelManager.IV = AESIV;
rijndaelManager.Mode = CipherMode.CBC;
rijndaelManager.Padding = PaddingMode.PKCS7;
ICryptoTransform rijndaelDecryptor = rijndaelManager.CreateDecryptor();
try
{
byte[] decryptedData = rijndaelDecryptor.TransformFinalBlock(inputData, 0, inputData.Length);
return Encoding.UTF8.GetString(decryptedData);
}
catch
{
return string.Empty;
}
finally
{
rijndaelDecryptor.Dispose();
rijndaelManager.Clear();
}
}
示例15: AESDecryptBase64
public static string AESDecryptBase64(string decryptString, string key)
{
byte[] inputData = Convert.FromBase64String(decryptString);
RijndaelManaged rijndaelManager = new RijndaelManaged();
rijndaelManager.Key = Encoding.UTF8.GetBytes(key.PadRight(32, ' ').Substring(0, 32));
rijndaelManager.IV = AESIV;
rijndaelManager.Mode = CipherMode.CBC;
rijndaelManager.Padding = PaddingMode.PKCS7;
ICryptoTransform rijndaelDecryptor = rijndaelManager.CreateDecryptor();
try
{
byte[] decryptedData = rijndaelDecryptor.TransformFinalBlock(inputData, 0, inputData.Length);
return Encoding.UTF8.GetString(decryptedData);
}
catch
{
return string.Empty;
}
finally
{
rijndaelDecryptor.Dispose();
rijndaelManager.Clear();
}
}