本文整理汇总了C#中System.Security.Cryptography.SymmetricAlgorithm.NullCheck方法的典型用法代码示例。如果您正苦于以下问题:C# SymmetricAlgorithm.NullCheck方法的具体用法?C# SymmetricAlgorithm.NullCheck怎么用?C# SymmetricAlgorithm.NullCheck使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类System.Security.Cryptography.SymmetricAlgorithm
的用法示例。
在下文中一共展示了SymmetricAlgorithm.NullCheck方法的6个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的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: Encrypt
public static byte[] Encrypt(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[] CipherTextBytes = null;
using (ICryptoTransform Encryptor = SymmetricKey.CreateEncryptor(DerivedPassword.GetBytes(KeySize / 8), InitialVector.ToByteArray()))
{
using (MemoryStream MemStream = new MemoryStream())
{
using (CryptoStream CryptoStream = new CryptoStream(MemStream, Encryptor, CryptoStreamMode.Write))
{
CryptoStream.Write(Data, 0, Data.Length);
CryptoStream.FlushFinalBlock();
CipherTextBytes = MemStream.ToArray();
}
}
}
SymmetricKey.Clear();
return CipherTextBytes;
}
}
}
示例3: 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;
}
}
}
示例4: Encrypt
public static byte[] Encrypt(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[] cipherTextBytes;
using (
ICryptoTransform encryptor = symmetricKey.CreateEncryptor(derivedPassword.GetBytes(keySize/8),
initialVector.ToByteArray()))
{
using (var memStream = new MemoryStream())
{
using (var cryptoStream = new CryptoStream(memStream, encryptor, CryptoStreamMode.Write))
{
cryptoStream.Write(data, 0, data.Length);
cryptoStream.FlushFinalBlock();
cipherTextBytes = memStream.ToArray();
}
}
}
symmetricKey.Clear();
return cipherTextBytes;
}
}
}
示例5: 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="AlgorithmUsing">Algorithm to use for decryption</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 64 (DES only), 128 (AES), 192 (AES and Triple DES), or 256 (AES)</param>
/// <returns>A decrypted byte array</returns>
public static byte[] Decrypt(this byte[] Data, string Key,
SymmetricAlgorithm AlgorithmUsing = null, string Salt = "Kosher",
string HashAlgorithm = "SHA1", int PasswordIterations = 2,
string InitialVector = "OFRna73m*aze01xY", int KeySize = 256)
{
if (Data.IsNull())
return null;
AlgorithmUsing = AlgorithmUsing.NullCheck(new RijndaelManaged());
Key.ThrowIfNullOrEmpty("Key");
Salt.ThrowIfNullOrEmpty("Salt");
HashAlgorithm.ThrowIfNullOrEmpty("HashAlgorithm");
InitialVector.ThrowIfNullOrEmpty("InitialVector");
using (PasswordDeriveBytes DerivedPassword = new PasswordDeriveBytes(Key, Salt.ToByteArray(), HashAlgorithm, PasswordIterations))
{
using (SymmetricAlgorithm SymmetricKey = AlgorithmUsing)
{
SymmetricKey.Mode = CipherMode.CBC;
byte[] PlainTextBytes = new byte[Data.Length];
int ByteCount = 0;
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))
{
ByteCount = CryptoStream.Read(PlainTextBytes, 0, PlainTextBytes.Length);
MemStream.Close();
CryptoStream.Close();
}
}
}
SymmetricKey.Clear();
Array.Resize(ref PlainTextBytes, ByteCount);
return PlainTextBytes;
}
}
}
示例6: Encrypt
/// <summary>
/// Encrypts a byte array
/// </summary>
/// <param name="Data">Data to be encrypted</param>
/// <param name="Key">Password to encrypt with</param>
/// <param name="AlgorithmUsing">Algorithm to use for encryption (defaults to AES)</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 64 (DES only), 128 (AES), 192 (AES and Triple DES), or 256 (AES)</param>
/// <returns>An encrypted byte array</returns>
public static byte[] Encrypt(this byte[] Data, string Key,
SymmetricAlgorithm AlgorithmUsing = null, string Salt = "Kosher",
string HashAlgorithm = "SHA1", int PasswordIterations = 2,
string InitialVector = "OFRna73m*aze01xY", int KeySize = 256)
{
if (Data.IsNull())
return null;
AlgorithmUsing = AlgorithmUsing.NullCheck(new RijndaelManaged());
Key.ThrowIfNullOrEmpty("Key");
Salt.ThrowIfNullOrEmpty("Salt");
HashAlgorithm.ThrowIfNullOrEmpty("HashAlgorithm");
InitialVector.ThrowIfNullOrEmpty("InitialVector");
using (PasswordDeriveBytes DerivedPassword = new PasswordDeriveBytes(Key, Salt.ToByteArray(), HashAlgorithm, PasswordIterations))
{
using (SymmetricAlgorithm SymmetricKey = AlgorithmUsing)
{
SymmetricKey.Mode = CipherMode.CBC;
byte[] CipherTextBytes = null;
using (ICryptoTransform Encryptor = SymmetricKey.CreateEncryptor(DerivedPassword.GetBytes(KeySize / 8), InitialVector.ToByteArray()))
{
using (MemoryStream MemStream = new MemoryStream())
{
using (CryptoStream CryptoStream = new CryptoStream(MemStream, Encryptor, CryptoStreamMode.Write))
{
CryptoStream.Write(Data, 0, Data.Length);
CryptoStream.FlushFinalBlock();
CipherTextBytes = MemStream.ToArray();
MemStream.Close();
CryptoStream.Close();
}
}
}
SymmetricKey.Clear();
return CipherTextBytes;
}
}
}