本文整理汇总了C#中System.Security.Cryptography.CryptoStream.ReadAllBinary方法的典型用法代码示例。如果您正苦于以下问题:C# CryptoStream.ReadAllBinary方法的具体用法?C# CryptoStream.ReadAllBinary怎么用?C# CryptoStream.ReadAllBinary使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类System.Security.Cryptography.CryptoStream
的用法示例。
在下文中一共展示了CryptoStream.ReadAllBinary方法的3个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: Decrypt
public static byte[] Decrypt(this byte[] data,
DeriveBytes key,
SymmetricAlgorithm algorithmUsing = null,
string initialVector = "OFRna73m*aze01xY",
int keySize = 256)
{
if (data.IsNull())
return null;
algorithmUsing = algorithmUsing.NullCheck(() => new RijndaelManaged());
Guard.NotEmpty(initialVector, "initialVector");
using (DeriveBytes derivedPassword = key)
{
using (SymmetricAlgorithm symmetricKey = algorithmUsing)
{
symmetricKey.Mode = CipherMode.CBC;
byte[] plainTextBytes;
using (
ICryptoTransform decryptor = symmetricKey.CreateDecryptor(derivedPassword.GetBytes(keySize/8),
initialVector.ToByteArray()))
{
using (var memStream = new MemoryStream(data))
{
using (var cryptoStream = new CryptoStream(memStream, decryptor, CryptoStreamMode.Read))
{
plainTextBytes = cryptoStream.ReadAllBinary();
}
}
}
symmetricKey.Clear();
return plainTextBytes;
}
}
}
示例2: Decrypt
public static byte[] Decrypt(this byte[] Data,
DeriveBytes Key,
SymmetricAlgorithm AlgorithmUsing = null,
string InitialVector = "OFRna73m*aze01xY",
int KeySize = 256)
{
if (Data.IsNull())
return null;
AlgorithmUsing = AlgorithmUsing.NullCheck(()=>new RijndaelManaged());
InitialVector.ThrowIfNullOrEmpty("InitialVector");
using (DeriveBytes DerivedPassword = Key)
{
using (SymmetricAlgorithm SymmetricKey = AlgorithmUsing)
{
SymmetricKey.Mode = CipherMode.CBC;
byte[] PlainTextBytes = null;
using (ICryptoTransform Decryptor = SymmetricKey.CreateDecryptor(DerivedPassword.GetBytes(KeySize / 8), InitialVector.ToByteArray()))
{
using (MemoryStream MemStream = new MemoryStream(Data))
{
using (CryptoStream CryptoStream = new CryptoStream(MemStream, Decryptor, CryptoStreamMode.Read))
{
PlainTextBytes = CryptoStream.ReadAllBinary();
}
}
}
SymmetricKey.Clear();
return PlainTextBytes;
}
}
}
示例3: Decrypt
/// <summary>
/// Decrypts a byte array
/// </summary>
/// <param name="Data">Data to be decrypted</param>
/// <param name="Key">Password to decrypt with</param>
/// <param name="Algorithm">Algorithm to use for decryption</param>
/// <param name="InitialVector">Needs to be 16 ASCII characters long</param>
/// <param name="KeySize">
/// Can be 64 (DES only), 128 (AES), 192 (AES and Triple DES), or 256 (AES)
/// </param>
/// <returns>A decrypted byte array</returns>
public byte[] Decrypt(byte[] Data, DeriveBytes Key, string Algorithm = "AES", string InitialVector = "OFRna73m*aze01xY", int KeySize = 256)
{
if (string.IsNullOrEmpty(InitialVector))
throw new ArgumentNullException(nameof(InitialVector));
if (Data == null)
return null;
using (SymmetricAlgorithm SymmetricKey = GetProvider(Algorithm))
{
byte[] PlainTextBytes = new byte[0];
if (SymmetricKey != null)
{
SymmetricKey.Mode = CipherMode.CBC;
using (ICryptoTransform Decryptor = SymmetricKey.CreateDecryptor(Key.GetBytes(KeySize / 8), InitialVector.ToByteArray()))
{
using (MemoryStream MemStream = new MemoryStream(Data))
{
using (CryptoStream CryptoStream = new CryptoStream(MemStream, Decryptor, CryptoStreamMode.Read))
{
PlainTextBytes = CryptoStream.ReadAllBinary();
}
}
}
SymmetricKey.Clear();
}
return PlainTextBytes;
}
}