本文整理汇总了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;
}
示例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;
}
示例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;
}
示例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;
}