本文整理汇总了C#中System.Security.Cryptography.RijndaelManaged.Dispose方法的典型用法代码示例。如果您正苦于以下问题:C# RijndaelManaged.Dispose方法的具体用法?C# RijndaelManaged.Dispose怎么用?C# RijndaelManaged.Dispose使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类System.Security.Cryptography.RijndaelManaged
的用法示例。
在下文中一共展示了RijndaelManaged.Dispose方法的9个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: CreateIV
public byte[] CreateIV()
{
var r = new RijndaelManaged();
r.GenerateIV();
var result = r.IV;
r.Dispose();
return result;
}
示例2: EncryptFile
public void EncryptFile(PasswordSheet sheet)
{
FileStream stream = null;
PasswordDeriveBytes secretKey = null;
RijndaelManaged rijndaelCipher = null;
ICryptoTransform encryptor = null;
CryptoStream cryptoStream = null;
try
{
stream = new FileStream(_fileName, FileMode.OpenOrCreate, FileAccess.Write);
stream.SetLength(0);
secretKey = GetPasswordBytes();
rijndaelCipher = new RijndaelManaged();
encryptor = rijndaelCipher.CreateEncryptor(secretKey.GetBytes(32), secretKey.GetBytes(16));
// Defines a stream that links data streams to cryptographic transformations
cryptoStream = new CryptoStream(stream, encryptor, CryptoStreamMode.Write);
byte[] data = null;
using (var sheetStream = new MemoryStream())
{
XmlSerializer serializer = new XmlSerializer(typeof(PasswordSheet));
serializer.Serialize(sheetStream, sheet);
data = sheetStream.GetBuffer();
}
cryptoStream.Write(data, 0, data.Length);
}
catch (Exception ex)
{
Console.Error.WriteLine(ex.Message);
}
finally
{
if (secretKey != null)
{
secretKey.Dispose();
}
if (rijndaelCipher != null)
{
rijndaelCipher.Dispose();
}
if (cryptoStream != null)
{
cryptoStream.Dispose();
}
}
}
示例3: DecryptFile
public PasswordSheet DecryptFile()
{
PasswordSheet decryptedData = null;
FileStream stream = null;
PasswordDeriveBytes secretKey = null;
RijndaelManaged rijndaelCipher = null;
ICryptoTransform decryptor = null;
CryptoStream cryptoStream = null;
try
{
rijndaelCipher = new RijndaelManaged();
// Making of the key for decryption
secretKey = GetPasswordBytes();
// Creates a symmetric Rijndael decryptor object.
decryptor = rijndaelCipher.CreateDecryptor(secretKey.GetBytes(32), secretKey.GetBytes(16));
stream = new FileStream(_fileName, FileMode.Open, FileAccess.Read);
// Defines the cryptographics stream for decryption.THe stream contains decrpted data
cryptoStream = new CryptoStream(stream, decryptor, CryptoStreamMode.Read);
XmlSerializer serializer = new XmlSerializer(typeof(PasswordSheet));
decryptedData = (PasswordSheet)serializer.Deserialize(cryptoStream);
}
catch (Exception ex)
{
Console.Error.WriteLine(ex.Message);
}
finally
{
if (stream != null)
{
stream.Dispose();
}
if (secretKey != null)
{
secretKey.Dispose();
}
if (rijndaelCipher != null)
{
rijndaelCipher.Dispose();
}
if (cryptoStream != null)
{
cryptoStream.Dispose();
}
}
return decryptedData;
}
示例4: RijndaelCipherText
public string RijndaelCipherText(string plaintext)//
{
string key = "jhIBty*&%98BjKMhgu#@$_)mJEDYT%$J"; //32byte
string iv = "jHYjnUyt435#$VHJ"; //16byte
//RijndaelManaged
byte[] plainbytes = UTF8Encoding.UTF8.GetBytes(plaintext);
RijndaelManaged rdm = new RijndaelManaged();
rdm.BlockSize = 128;
rdm.KeySize = 256;
ICryptoTransform ict = rdm.CreateEncryptor(UTF8Encoding.UTF8.GetBytes(key), UTF8Encoding.UTF8.GetBytes(iv));//
byte[] encryptedbytearr = ict.TransformFinalBlock(plainbytes, 0, plainbytes.Length);
ict.Dispose();
rdm.Dispose();
return (Convert.ToBase64String(encryptedbytearr));
}
示例5: Decrypt
public Stream Decrypt(RSAParameters privateKey, string fileName)
{
if (!File.Exists(fileName))
{
throw new ArgumentException(string.Format("File {0} does not exist", fileName));
}
var rsaProvider = new RSACryptoServiceProvider();
rsaProvider.ImportParameters(privateKey);
var ms = new MemoryStream();
using (var fs = new FileStream(fileName, FileMode.Open))
using (var reader = new StreamReader(fs))
{
var keyLength = ReadUntil(reader, EncryptedXmlWriterTraceListener.Delimiter);
var keyString = ReadUntil(reader, EncryptedXmlWriterTraceListener.Delimiter);
var ivLength = ReadUntil(reader, EncryptedXmlWriterTraceListener.Delimiter);
var ivString = ReadUntil(reader, EncryptedXmlWriterTraceListener.Delimiter);
var key = rsaProvider.Decrypt(Convert.FromBase64String(keyString), false);
var iv = rsaProvider.Decrypt(Convert.FromBase64String(ivString), false);
RijndaelManaged rijndaelManaged = new RijndaelManaged();
rijndaelManaged.Padding = PaddingMode.Zeros;
rijndaelManaged.IV = iv;
rijndaelManaged.Key = key;
fs.Seek(keyLength.Length + int.Parse(keyLength) + ivLength.Length + int.Parse(ivLength) + 4, SeekOrigin.Begin);
var crypto = new CryptoStream(fs, rijndaelManaged.CreateDecryptor(rijndaelManaged.Key, rijndaelManaged.IV), CryptoStreamMode.Read);
crypto.CopyTo(ms);
ms.Seek(0, SeekOrigin.Begin);
rijndaelManaged.Dispose();
}
rsaProvider.Dispose();
return ms;
}
开发者ID:josephcooney,项目名称:EncryptedXmlWriterTraceListener,代码行数:38,代码来源:EncryptedXmlTextWriterDecrypter.cs
示例6: Main
static void Main(string[] args)
{
const int n = 100 * 1000;
var sw = new Stopwatch();
Random r = new Random();
var data = new byte[1024];
var key8B = new byte[8];
var key16B = new byte[16];
var key24B = new byte[24];
var key32B = new byte[32];
r.NextBytes(data);
r.NextBytes(key8B);
r.NextBytes(key16B);
r.NextBytes(key24B);
r.NextBytes(key32B);
Action<string> outputToConsole = (s) =>
{
Console.ForegroundColor = ConsoleColor.Yellow;
Console.WriteLine(s);
};
// AES
Console.ForegroundColor = ConsoleColor.DarkCyan;
Console.WriteLine("AES");
var aes = new AesCryptoServiceProvider();
aes.Padding = PaddingMode.PKCS7;
aes.Key = key16B;
Action doAes = () => EncryptDecryptAndDispose(aes.CreateEncryptor(), aes.CreateDecryptor(), data);
doAes.Repeat(n)
.OutputPerformance(sw, outputToConsole)();
aes.Dispose();
// RSA
Console.ForegroundColor = ConsoleColor.DarkCyan;
Console.WriteLine("DES");
var des = new DESCryptoServiceProvider();
des.IV = key8B;
des.Key = key8B;
Action doDes = () => EncryptDecryptAndDispose(des.CreateEncryptor(), des.CreateDecryptor(), data);
doDes.Repeat(n)
.OutputPerformance(sw, outputToConsole)();
des.Dispose();
// RC2
Console.ForegroundColor = ConsoleColor.DarkCyan;
Console.WriteLine("RC2");
var rc2 = new RC2CryptoServiceProvider();
rc2.IV = key8B;
rc2.Key = key8B;
Action doRc2 = () => EncryptDecryptAndDispose(rc2.CreateEncryptor(), rc2.CreateDecryptor(), data);
doRc2.Repeat(n)
.OutputPerformance(sw, outputToConsole)();
rc2.Dispose();
// Rijndael
Console.ForegroundColor = ConsoleColor.DarkCyan;
Console.WriteLine("Rijndael");
var rijndael = new RijndaelManaged();
rijndael.IV = key16B;
rijndael.Key = key16B;
Action doRijndael = () => EncryptDecryptAndDispose(rijndael.CreateEncryptor(), rijndael.CreateDecryptor(), data);
doRijndael.Repeat(n)
.OutputPerformance(sw, outputToConsole)();
rijndael.Dispose();
// 3DES
Console.ForegroundColor = ConsoleColor.DarkCyan;
Console.WriteLine("3DES");
var tripleDes = new TripleDESCryptoServiceProvider();
tripleDes.IV = key8B;
tripleDes.Key = key24B;
Action do3des = () => EncryptDecryptAndDispose(tripleDes.CreateEncryptor(), tripleDes.CreateDecryptor(), data);
do3des.Repeat(n)
.OutputPerformance(sw, outputToConsole)();
tripleDes.Dispose();
// RSA
Console.ForegroundColor = ConsoleColor.DarkCyan;
Console.WriteLine("RSA");
RSAParameters param = new RSAParameters();
param.Exponent = new byte[] {0, 1, 0};
var store = new X509Store(StoreLocation.LocalMachine);
store.Open(OpenFlags.ReadOnly);
X509Certificate cert = null;
foreach (X509Certificate cer in store.Certificates)
{
if (cer != null)
{
cert = cer;
break;
}
}
param.Modulus = cert.GetPublicKey();
var rsa = new RSACryptoServiceProvider();
rsa.ImportParameters(param);
Action doRsa = () =>
{
//.........这里部分代码省略.........
示例7: EncryptFile
public static void EncryptFile(string sourceFile, string destFile, string password)
{
RijndaelManaged rijndael = null;
byte[] key, iv;
FileStream outfs = null;
ICryptoTransform encryptor = null;
CryptoStream cryptostream = null;
FileStream infs = null;
try
{
rijndael = new RijndaelManaged();
GenerateKeyFromPassword(password, rijndael.KeySize, out key, rijndael.BlockSize, out iv);
rijndael.Key = key;
rijndael.IV = iv;
outfs = new FileStream(destFile, FileMode.Create, FileAccess.Write);
encryptor = rijndael.CreateEncryptor();
cryptostream = new CryptoStream(outfs, encryptor, CryptoStreamMode.Write);
infs = new FileStream(sourceFile, FileMode.Open, FileAccess.Read);
byte[] bs = new byte[1024];
int readLen;
while ((readLen = infs.Read(bs, 0, bs.Length)) > 0)
{
cryptostream.Write(bs, 0, readLen);
}
}
catch (Exception)
{
throw;
}
finally
{
if (rijndael != null) rijndael.Dispose();
if (infs != null) infs.Dispose();
if (cryptostream != null) cryptostream.Close();
if (encryptor != null) encryptor.Dispose();
if (outfs != null) outfs.Dispose();
}
}
示例8: Create
/// <summary>
/// Create a new SymmCipher object with a random key based on the alg and mode supplied.
/// </summary>
/// <param name="algId"></param>
/// <param name="numBits"></param>
/// <param name="mode"></param>
/// <returns></returns>
public static SymmCipher Create(SymDefObject symDef = null, byte[] keyData = null, byte[] iv = null)
{
if (symDef == null)
{
symDef = new SymDefObject(TpmAlgId.Aes, 128, TpmAlgId.Cfb);
}
else if (symDef.Algorithm != TpmAlgId.Aes)
{
Globs.Throw<ArgumentException>("Unsupported symmetric algorithm " + symDef.Algorithm);
return null;
}
#if TSS_USE_BCRYPT
var alg = new BCryptAlgorithm(Native.BCRYPT_AES_ALGORITHM);
if (keyData == null)
{
keyData = Globs.GetRandomBytes(symDef.KeyBits / 8);
}
var key = alg.GenerateSymKey(symDef, keyData, GetBlockSize(symDef));
//key = BCryptInterface.ExportSymKey(keyHandle);
//keyHandle = alg.LoadSymKey(key, symDef, GetBlockSize(symDef));
alg.Close();
return new SymmCipher(key, keyData, iv);
#else
SymmetricAlgorithm alg = new RijndaelManaged();
// DES and __3DES are not supported in TPM 2.0 v 0.96 and above
//switch (algId) {
// case TpmAlgId.Aes: alg = new RijndaelManaged(); break;
// case TpmAlgId.__3DES: alg = new TripleDESCryptoServiceProvider(); break;
// case TpmAlgId.Des: alg = new DESCryptoServiceProvider(); break;
//}
int blockSize = GetBlockSize(symDef);
alg.KeySize = symDef.KeyBits;
alg.BlockSize = blockSize * 8;
alg.Padding = PaddingMode.None;
alg.Mode = GetCipherMode(symDef.Mode);
// REVISIT: Get this right for other modes
alg.FeedbackSize = alg.BlockSize;
if (keyData == null)
{
// Generate random key
alg.IV = Globs.GetZeroBytes(blockSize);
try
{
alg.GenerateKey();
}
catch (Exception)
{
alg.Dispose();
throw;
}
}
else
{
// Use supplied key bits
alg.Key = keyData;
if (iv == null)
{
iv = Globs.GetZeroBytes(blockSize);
}
else if (iv.Length != blockSize)
{
Array.Resize(ref iv, blockSize);
}
alg.IV = iv;
}
return new SymmCipher(alg);
#endif
}
示例9: 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
var key = new Rfc2898DeriveBytes(sharedSecret, Salt);
// Create the streams used for decryption.
byte[] bytes = Convert.FromBase64String(cipherText);
using (var 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 (var csDecrypt = new CryptoStream(msDecrypt, decryptor, CryptoStreamMode.Read))
{
using (var 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();
aesAlg.Dispose();
}
}
return plaintext;
}