本文整理汇总了C#中RijndaelManaged.Clear方法的典型用法代码示例。如果您正苦于以下问题:C# RijndaelManaged.Clear方法的具体用法?C# RijndaelManaged.Clear怎么用?C# RijndaelManaged.Clear使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类RijndaelManaged
的用法示例。
在下文中一共展示了RijndaelManaged.Clear方法的9个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: InitDecrypt
private ICryptoTransform InitDecrypt(string a_key)
{
byte[] keyBytes = Encoding.Unicode.GetBytes(a_key);
Rfc2898DeriveBytes derivedKey = new Rfc2898DeriveBytes(a_key, keyBytes);
RijndaelManaged rijndaelCSP = new RijndaelManaged();
rijndaelCSP.Key = derivedKey.GetBytes(rijndaelCSP.KeySize / 8);
rijndaelCSP.IV = derivedKey.GetBytes(rijndaelCSP.BlockSize / 8);
ICryptoTransform decryptor = rijndaelCSP.CreateDecryptor();
rijndaelCSP.Clear();
return decryptor;
}
示例2: decryptStringFromBytes_AES
private string decryptStringFromBytes_AES(byte[] cipherText, byte[] Key, byte[] IV)
{
if (cipherText == null || cipherText.Length <= 0)
{
throw new ArgumentNullException("cipherText");
}
if (Key == null || Key.Length <= 0)
{
throw new ArgumentNullException("Key");
}
if (IV == null || IV.Length <= 0)
{
throw new ArgumentNullException("Key");
}
MemoryStream memoryStream = null;
CryptoStream cryptoStream = null;
StreamReader streamReader = null;
RijndaelManaged rijndaelManaged = null;
string result = null;
try
{
rijndaelManaged = new RijndaelManaged();
rijndaelManaged.Key = Key;
rijndaelManaged.IV = IV;
var transform = rijndaelManaged.CreateDecryptor(rijndaelManaged.Key, rijndaelManaged.IV);
memoryStream = new MemoryStream(cipherText);
cryptoStream = new CryptoStream(memoryStream, transform, CryptoStreamMode.Read);
streamReader = new StreamReader(cryptoStream);
result = streamReader.ReadToEnd();
}
finally
{
if (streamReader != null)
{
streamReader.Close();
}
if (cryptoStream != null)
{
cryptoStream.Close();
}
if (memoryStream != null)
{
memoryStream.Close();
}
if (rijndaelManaged != null)
{
rijndaelManaged.Clear();
}
}
return result;
}
示例3: 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.
RijndaelManaged 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 the streams used for decryption.
byte[] bytes = Convert.FromBase64String( cipherText );
using ( MemoryStream msDecrypt = new MemoryStream( bytes ) )
{
// Create a RijndaelManaged object
// with the specified key and IV.
aesAlg = new RijndaelManaged();
aesAlg.Key = key.GetBytes( aesAlg.KeySize / 8 );
// Get the initialization vector from the encrypted stream
aesAlg.IV = ReadByteArray( msDecrypt );
// Create a decrytor to perform the stream transform.
ICryptoTransform decryptor = aesAlg.CreateDecryptor( aesAlg.Key, aesAlg.IV );
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;
}
示例4: GetScopeKey
// Protect a block of memory.
internal static void Protect
(byte[] userData, byte[] optionalEntropy,
MemoryProtectionScope scope, byte[] output)
{
// Get the key to use.
byte[] key = GetScopeKey(scope, optionalEntropy);
// Encrypt the block of memory using AES.
Rijndael alg = new RijndaelManaged();
alg.Mode = CipherMode.CFB;
ICryptoTransform transform = alg.CreateEncryptor(key, null);
transform.TransformBlock
(userData, 0, userData.Length, output, 0);
transform.Dispose();
alg.Clear();
Array.Clear(key, 0, key.Length);
}
示例5: 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>
public static string EncryptStringAES(string plainText, string sharedSecret)
{
if (string.IsNullOrEmpty(plainText))
throw new ArgumentNullException("plainText");
if (string.IsNullOrEmpty(sharedSecret))
throw new ArgumentNullException("sharedSecret");
string outStr = null; // Encrypted string to return
RijndaelManaged aesAlg = null; // RijndaelManaged object used to encrypt the data.
try {
// generate the key from the shared secret and the salt
Rfc2898DeriveBytes key = new Rfc2898DeriveBytes(sharedSecret, _salt);
// Create a RijndaelManaged object
aesAlg = new RijndaelManaged();
aesAlg.Key = key.GetBytes(aesAlg.KeySize / 8);
// Create a decryptor to perform the stream transform.
ICryptoTransform encryptor = aesAlg.CreateEncryptor(aesAlg.Key, aesAlg.IV);
// Create the streams used for encryption.
using (MemoryStream msEncrypt = new MemoryStream()) {
// prepend the IV
msEncrypt.Write(BitConverter.GetBytes(aesAlg.IV.Length), 0, sizeof(int));
msEncrypt.Write(aesAlg.IV, 0, aesAlg.IV.Length);
using (CryptoStream csEncrypt = new CryptoStream(msEncrypt, encryptor, CryptoStreamMode.Write)) {
using (StreamWriter swEncrypt = new StreamWriter(csEncrypt)) {
//Write all data to the stream.
swEncrypt.Write(plainText);
}
}
outStr = Convert.ToBase64String(msEncrypt.ToArray());
}
} finally {
// Clear the RijndaelManaged object.
if (aesAlg != null)
aesAlg.Clear();
}
// Return the encrypted bytes from the memory stream.
return outStr;
}
示例6: EncryptString
public static string EncryptString(string message, string KeyString, string IVString)
{
byte[] Key = ASCIIEncoding.UTF8.GetBytes(KeyString);
byte[] IV = ASCIIEncoding.UTF8.GetBytes(IVString);
string encrypted = null;
RijndaelManaged rj = new RijndaelManaged();
rj.BlockSize = 256;
rj.Key = Key;
rj.IV = IV;
rj.Mode = CipherMode.CBC;
try
{
MemoryStream ms = new MemoryStream();
using (CryptoStream cs = new CryptoStream(ms, rj.CreateEncryptor(Key, IV), CryptoStreamMode.Write))
{
using (StreamWriter sw = new StreamWriter(cs))
{
sw.Write(message);
sw.Close();
}
cs.Close();
}
byte[] encoded = ms.ToArray();
encrypted = Convert.ToBase64String(encoded);
ms.Close();
}
catch (CryptographicException e)
{
Console.WriteLine("A Cryptographic error occurred: {0}", e.Message);
return null;
}
catch (UnauthorizedAccessException e)
{
Console.WriteLine("A file error occurred: {0}", e.Message);
return null;
}
catch (Exception e)
{
Console.WriteLine("An error occurred: {0}", e.Message);
}
finally
{
rj.Clear();
}
return encrypted;
}
示例7: DecryptRJ256
public static String DecryptRJ256(byte[] cypher, string KeyString, string IVString)
{
UTF8Encoding encoding = new UTF8Encoding();
byte[] Key = encoding.GetBytes(KeyString);
byte[] IV = encoding.GetBytes(IVString);
string sRet = "";
RijndaelManaged rj = new RijndaelManaged();
// rj.Padding = PaddingMode.Zeros;
rj.Mode = CipherMode.CBC;
rj.KeySize = 256;
rj.BlockSize = 256;
rj.Key = Key;
rj.IV = IV;
try
{
MemoryStream ms = new MemoryStream(cypher);
using (CryptoStream cs = new CryptoStream(ms, rj.CreateDecryptor(Key, IV), CryptoStreamMode.Read))
{
using (StreamReader sr = new StreamReader(cs))
{
sRet = sr.ReadLine();
}
cs.Close();
}
}
finally
{
rj.Clear();
}
return sRet;
}
示例8: encryptStringToBytes_AES
private byte[] encryptStringToBytes_AES(string plainText, byte[] Key, byte[] IV)
{
if (plainText == null || plainText.Length <= 0)
{
throw new ArgumentNullException("plainText");
}
if (Key == null || Key.Length <= 0)
{
throw new ArgumentNullException("Key");
}
if (IV == null || IV.Length <= 0)
{
throw new ArgumentNullException("Key");
}
MemoryStream memoryStream = null;
CryptoStream cryptoStream = null;
StreamWriter streamWriter = null;
RijndaelManaged rijndaelManaged = null;
try
{
rijndaelManaged = new RijndaelManaged();
rijndaelManaged.Key = Key;
rijndaelManaged.IV = IV;
var transform = rijndaelManaged.CreateEncryptor(rijndaelManaged.Key, rijndaelManaged.IV);
memoryStream = new MemoryStream();
cryptoStream = new CryptoStream(memoryStream, transform, CryptoStreamMode.Write);
streamWriter = new StreamWriter(cryptoStream);
streamWriter.Write(plainText);
}
finally
{
if (streamWriter != null)
{
streamWriter.Close();
}
if (cryptoStream != null)
{
cryptoStream.Close();
}
if (memoryStream != null)
{
memoryStream.Close();
}
if (rijndaelManaged != null)
{
rijndaelManaged.Clear();
}
}
return memoryStream.ToArray();
}
示例9: encryptTitleKey
private void encryptTitleKey()
{
commonKeyIndex = newKeyIndex;
byte[] ckey = { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 };
byte[] iv = BitConverter.GetBytes(Shared.Swap(titleId));
Array.Resize(ref iv, 16);
RijndaelManaged rm = new RijndaelManaged();
rm.Mode = CipherMode.CBC;
rm.Padding = PaddingMode.None;
rm.KeySize = 128;
rm.BlockSize = 128;
rm.Key = ckey;
rm.IV = iv;
ICryptoTransform encryptor = rm.CreateEncryptor();
MemoryStream ms = new MemoryStream(decryptedTitleKey);
CryptoStream cs = new CryptoStream(ms, encryptor, CryptoStreamMode.Read);
cs.Read(encryptedTitleKey, 0, encryptedTitleKey.Length);
cs.Dispose();
ms.Dispose();
encryptor.Dispose();
rm.Clear();
}