本文整理汇总了C#中System.Security.Cryptography.CryptoStream.Clear方法的典型用法代码示例。如果您正苦于以下问题:C# CryptoStream.Clear方法的具体用法?C# CryptoStream.Clear怎么用?C# CryptoStream.Clear使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类System.Security.Cryptography.CryptoStream
的用法示例。
在下文中一共展示了CryptoStream.Clear方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: CreyptoStreamExample
public void CreyptoStreamExample()
{
var algorithm = SymmetricAlgorithm.Create();
//algorithm.GenerateKey();
//algorithm.GenerateIV();
using(var file = File.Create(@".\key.txt"))
{
// file.Write(algorithm.Key, 0, algorithm.Key.Length);
}
using (var file = File.Create(@".\IV.txt"))
{
//file.Write(algorithm.IV, 0, algorithm.IV.Length);
}
algorithm.Key = KeyAndIV;
algorithm.IV = KeyAndIV;
var encryptor = algorithm.CreateEncryptor();
algorithm.Padding = PaddingMode.None;
using(var inputStream = new FileStream(@".\EncryptedFile.txt", FileMode.OpenOrCreate))
using(var cryptoStream = new CryptoStream(inputStream, encryptor, CryptoStreamMode.Write))
{
var input = Encoding.UTF8.GetBytes(SecretText);
cryptoStream.Write(input, 0, input.Length);
cryptoStream.Clear();
}
}
示例2: Decrypt
private const String SALT_VALUE = "[email protected]#23!56GH"; //any character
#endregion Fields
#region Methods
public static string Decrypt(string cipherText)
{
var initVectorBytes = Encoding.ASCII.GetBytes(INIT_VECTOR);
var saltValueBytes = Encoding.ASCII.GetBytes(SALT_VALUE);
var cipherTextBytes = Convert.FromBase64String(cipherText);
var password = new PasswordDeriveBytes(PASSPHRASE, saltValueBytes, HASH_ALGORITM, PASSWORD_ITERATION);
var keyBytes = password.GetBytes(KEY_SIZE / 8);
var symmetricKey = new RijndaelManaged();
symmetricKey.Mode = CipherMode.CBC;
symmetricKey.Padding = PaddingMode.None;
var decryptor = symmetricKey.CreateDecryptor(keyBytes, initVectorBytes);
var memoryStream = new MemoryStream(cipherTextBytes);
var cryptoStream = new CryptoStream(memoryStream, decryptor, CryptoStreamMode.Read);
var plainTextBytes = new byte[cipherTextBytes.Length];
int decryptedByteCount = cryptoStream.Read(plainTextBytes, 0, plainTextBytes.Length);
cryptoStream.Clear();
memoryStream.Close();
cryptoStream.Close();
var plainText = Encoding.UTF8.GetString(plainTextBytes, 0, decryptedByteCount);
return plainText;
}
示例3: Encrypt
public byte[] Encrypt(byte[] srcData, string strKey)
{
byte[] buffer = HashDigest.StringDigest(strKey, DigestType.MD5);
SymmetricAlgorithm algorithm = SymmetricAlgorithm.Create("3DES");
algorithm.Key = buffer;
algorithm.IV = Keys;
new DESCryptoServiceProvider();
MemoryStream stream = new MemoryStream();
CryptoStream stream2 = new CryptoStream(stream, algorithm.CreateEncryptor(), CryptoStreamMode.Write);
stream2.Write(srcData, 0, srcData.Length);
stream2.FlushFinalBlock();
stream2.Clear();
return stream.ToArray();
}
示例4: Crypt
// Internal helper function to do the encryption/decryption
protected static byte[] Crypt(byte[] bValue, string sPassword, string sSalt, bool bEncrypt)
{
byte[] bReturnVal = null;
AesManaged aesTransform = null;
MemoryStream msStream = null;
CryptoStream csCryptoStream = null;
try
{
// Rfc2898DeriveBytes (PBKDF2) is similar to using PasswordDeriveBytes (PBKDF1) but the algorithm is stronger apparently
Rfc2898DeriveBytes pwdGen = new Rfc2898DeriveBytes(sPassword, Encoding.Unicode.GetBytes(sSalt), 1000);
// Create our AES Key and Initialization Vector (IV) values
aesTransform = new AesManaged();
aesTransform.Key = pwdGen.GetBytes(aesTransform.KeySize / 8);
aesTransform.IV = pwdGen.GetBytes(aesTransform.BlockSize / 8);
aesTransform.Padding = PaddingMode.ISO10126;// Pad with random characters rather than zeros
// Depending on if we're encrypting or decrypting create the proper transform object
ICryptoTransform ctTransform = (bEncrypt ? aesTransform.CreateEncryptor() : aesTransform.CreateDecryptor());
// Create our memory stream and encryption/decryption stream object
msStream = new MemoryStream();
csCryptoStream = new CryptoStream(msStream, ctTransform, CryptoStreamMode.Write);
// Encrypt/Decrypt the value
csCryptoStream.Write(bValue, 0, bValue.Length);
csCryptoStream.FlushFinalBlock();
// Turn our encrypted/decrypted memory stream value into a byte array
bReturnVal = msStream.ToArray();
}
finally
{
if (csCryptoStream != null) { csCryptoStream.Clear(); csCryptoStream.Close(); }
if (msStream != null) { msStream.Close(); }
if (aesTransform != null) { aesTransform.Clear(); }
}
// Return the encrypted/decrypted value
return bReturnVal;
}
示例5: Decrypt
/// <summary>
/// 解密
/// </summary>
/// <param name="srcData">源数据</param>
/// <param name="strKey">密钥</param>
/// <returns>解密后的字节数组,若解密失败则返回null</returns>
public byte[] Decrypt(byte[] srcData, string strKey)
{
if (null == srcData || srcData.Length < 1) return null;
//将密钥散列
byte[] srcKeyData = HashDigest.StringDigest(strKey, DigestType.MD5);
SymmetricAlgorithm symAlg = SymmetricAlgorithm.Create("3DES");
symAlg.Key = srcKeyData;
symAlg.IV = Keys;
DESCryptoServiceProvider dCSP = new DESCryptoServiceProvider();
MemoryStream mStream = new MemoryStream();
CryptoStream cStream = new CryptoStream(
mStream,
symAlg.CreateDecryptor(),
CryptoStreamMode.Write);
cStream.Write(srcData, 0, srcData.Length);
cStream.FlushFinalBlock();
cStream.Clear();
return mStream.ToArray();
}
示例6: EncryptDecryptRefreshToken
/// <summary>
/// Encrypts or decrypts the refresh token.
/// </summary>
/// <param name="key">Key used for decryption</param>
/// <param name="bytes">Refresh Token bytes</param>
/// <param name="encryptor">AES object for encryption or decryption</param>
/// <param name="isEncrypt">Checks for encryption/decryption</param>
/// <returns>Returns the encrypted or decrypted refresh token depending on the AES object being passed</returns>
private static string EncryptDecryptRefreshToken(string key, byte[] bytes, bool isEncrypt)
{
string refreshToken = string.Empty;
if (!string.IsNullOrEmpty(ConstantStrings.EncryptionVector) && !string.IsNullOrEmpty(key) && null != bytes)
{
byte[] vector = new byte[ConstantStrings.EncryptionVector.Length * sizeof(char)];
System.Buffer.BlockCopy(key.ToCharArray(), 0, vector, 0, vector.Length);
CryptoStream cryptoStream = null;
using (Aes encryptor = Aes.Create())
{
using (Rfc2898DeriveBytes encryptObject = new Rfc2898DeriveBytes(key, vector))
{
encryptor.Key = encryptObject.GetBytes(32); // 32 bytes encryption key
encryptor.IV = encryptObject.GetBytes(16); // 16 bytes initialization vector
}
using (MemoryStream memoryStream = new MemoryStream())
{
if (isEncrypt)
{
// To encrypt the refresh token
cryptoStream = new CryptoStream(memoryStream, encryptor.CreateEncryptor(), CryptoStreamMode.Write); // Creates an object to apply the cryptographic transformation to a memory stream
cryptoStream.Write(bytes, 0, bytes.Length); // Applies the cryptographic transformation to refresh token
cryptoStream.Clear();
refreshToken = Convert.ToBase64String(memoryStream.ToArray());
}
else
{
// To decrypt the refresh token
cryptoStream = new CryptoStream(memoryStream, encryptor.CreateDecryptor(), CryptoStreamMode.Write);
cryptoStream.Write(bytes, 0, bytes.Length);
cryptoStream.Clear();
refreshToken = Encoding.Unicode.GetString(memoryStream.ToArray());
}
}
}
}
return refreshToken;
}
示例7: DesDecrypt
/// <summary>
/// DES算法解密
/// </summary>
/// <param name="pToDecrypt">密文</param>
/// <param name="sKey">加密Key</param>
/// <returns></returns>
public static string DesDecrypt(string pToDecrypt, string sKey)
{
TripleDESCryptoServiceProvider provider = new TripleDESCryptoServiceProvider();
byte[] buffer = new byte[pToDecrypt.Length / 2];
for (int i = 0; i < (pToDecrypt.Length / 2); i++)
{
int num2 = Convert.ToInt32(pToDecrypt.Substring(i * 2, 2), 0x10);
buffer[i] = (byte)num2;
}
provider.Key = new MD5CryptoServiceProvider().ComputeHash(Encoding.UTF8.GetBytes(sKey));
provider.Mode = CipherMode.ECB;
provider.Padding = PaddingMode.PKCS7;
MemoryStream stream = new MemoryStream();
CryptoStream crypto = new CryptoStream(stream, provider.CreateDecryptor(), CryptoStreamMode.Write);
crypto.Write(buffer, 0, buffer.Length);
crypto.FlushFinalBlock();
string builder = Encoding.UTF8.GetString(stream.ToArray());
stream.Close();
crypto.Clear();
crypto.Close();
provider.Clear();
return builder;
}
示例8: Read_Disposed_Break
[ExpectedException (typeof (ArgumentNullException))] // should fail like previous test case
#endif
public void Read_Disposed_Break ()
{
// do no corrupt readStream in further tests
using (Stream s = new MemoryStream ()) {
byte[] buffer = new byte [8];
cs = new CryptoStream (s, encryptor, CryptoStreamMode.Read);
int len = cs.Read (buffer, 0, 4);
Assert.AreEqual (4, len, "Read 4");
cs.Clear ();
len = cs.Read (buffer, 3, 4);
}
}
示例9: Write_Disposed
[ExpectedException (typeof (ArgumentNullException))] // to match exception throw by Read in a similar case
#endif
public void Write_Disposed ()
{
// do no corrupt writeStream in further tests
using (Stream s = new MemoryStream ()) {
byte[] buffer = new byte [8];
cs = new CryptoStream (s, encryptor, CryptoStreamMode.Write);
cs.Clear ();
cs.Write (buffer, 0, 8);
}
}
示例10: FlushFinalBlock_Disposed
public void FlushFinalBlock_Disposed ()
{
// do no corrupt writeStream in further tests
using (Stream s = new MemoryStream ()) {
cs = new CryptoStream (s, encryptor, CryptoStreamMode.Write);
cs.Clear ();
cs.FlushFinalBlock ();
}
}
示例11: Read_Disposed
public void Read_Disposed ()
{
// do no corrupt readStream in further tests
using (Stream s = new MemoryStream ()) {
byte[] buffer = new byte [8];
cs = new CryptoStream (s, encryptor, CryptoStreamMode.Read);
cs.Clear ();
Assert.AreEqual (0, cs.Read (buffer, 0, 8), "Read from disposed");
}
}
示例12: DecryptString
/// <summary>
/// 解密为字符串
/// </summary>
/// <param name="s"></param>
/// <returns></returns>
public string DecryptString(string s)
{
CheckUtil.ArgumentNotNullOrEmpty(s, "s");
byte[] inputData = Convert.FromBase64String(s);
string result = string.Empty;
using (MemoryStream ms = new System.IO.MemoryStream(inputData))
{
symmetricAlgorithmProvider.Key = GetLegalKey();
symmetricAlgorithmProvider.IV = GetLegalIV();
ICryptoTransform encrypto = symmetricAlgorithmProvider.CreateDecryptor();
CryptoStream cs = new CryptoStream(ms, encrypto, CryptoStreamMode.Read);
StreamReader sr = new StreamReader(cs);
result = sr.ReadLine();
//CryptoStream cs = new CryptoStream(ms, encrypto, CryptoStreamMode.Read);
//byte[] outputData = new byte[inputData.Length];
//cs.Read(outputData, 0, outputData.Length);
//result = Encoding.UTF8.GetString(outputData).TrimEnd(new char[] { '\0' });
cs.Clear();
cs.Close();
}
return result;
}
示例13: DecryptData
public void DecryptData(string inName, string outName)
{
FileStream fileStream1 = new FileStream(inName, FileMode.Open, FileAccess.Read);
FileStream fileStream2 = new FileStream(outName, FileMode.OpenOrCreate, FileAccess.Write);
fileStream2.SetLength(0L);
this.desKey[0] = (byte) 20;
this.desKey[1] = (byte) 157;
this.desKey[2] = (byte) 64;
this.desKey[3] = (byte) 213;
this.desKey[4] = (byte) 193;
this.desKey[5] = (byte) 46;
this.desKey[6] = (byte) 85;
this.desKey[7] = (byte) 2;
this.desIV[0] = (byte) 0;
this.desIV[1] = (byte) 0;
this.desIV[2] = (byte) 0;
this.desIV[3] = (byte) 0;
this.desIV[4] = (byte) 0;
this.desIV[5] = (byte) 0;
this.desIV[6] = (byte) 0;
this.desIV[7] = (byte) 0;
byte[] numArray = new byte[8];
long num = 8L;
long length = fileStream1.Length;
DESCryptoServiceProvider cryptoServiceProvider = new DESCryptoServiceProvider();
cryptoServiceProvider.Mode = CipherMode.CBC;
cryptoServiceProvider.Padding = PaddingMode.None;
CryptoStream cryptoStream = new CryptoStream((Stream) fileStream2, cryptoServiceProvider.CreateDecryptor(this.desKey, this.desIV), CryptoStreamMode.Write);
for (; num <= length; {
int count;
num = (long) Convert.ToInt32((double) num + (double) count / (double) cryptoServiceProvider.BlockSize * (double) cryptoServiceProvider.BlockSize);
}
)
{
count = fileStream1.Read(numArray, 0, 8);
cryptoStream.Write(numArray, 0, count);
}
cryptoStream.Close();
fileStream1.Close();
fileStream2.Close();
cryptoStream.Clear();
cryptoServiceProvider.Clear();
}
示例14: Decrypt
/// <summary>
/// ����� "������������"
/// </summary>
private bool Decrypt(byte[] data, int length, byte[] key, byte[] iv)
{
Rijndael rijndael = new RijndaelManaged();
rijndael.Mode = CipherMode.CBC;
rijndael.BlockSize = rijndael.KeySize = 256;
ICryptoTransform ict = rijndael.CreateDecryptor(key, iv);
MemoryStream ms = new MemoryStream();
ms.Write(data, 0, length);
ms.Flush();
ms.Seek(0, SeekOrigin.Begin);
CryptoStream cs = new CryptoStream(ms, ict, CryptoStreamMode.Read);
try
{
cs.Read(data, 0, length);
}
catch(CryptographicException)
{
return false;
}
cs.Close();
cs.Clear();
rijndael.Clear();
return true;
}
示例15: EncryptByteArray
////////////////////////////////////////////////////////////////////
// Encrypt byte array
////////////////////////////////////////////////////////////////////
internal Byte[] EncryptByteArray(
Int32 ObjectNumber,
Byte[] PlainText
)
{
// create encryption key
Byte[] EncryptionKey = CreateEncryptionKey(ObjectNumber);
Byte[] CipherText;
if(EncryptionType == EncryptionType.Aes128)
{
MemoryStream OutputStream = null;
CryptoStream CryptoStream = null;
// generate new initialization vector IV
AES.GenerateIV();
// create cipher text buffer including initialization vector
Int32 CipherTextLen = (PlainText.Length & 0x7ffffff0) + 16;
CipherText = new Byte[CipherTextLen + 16];
Array.Copy(AES.IV, 0, CipherText, 0, 16);
// set encryption key and key length
AES.Key = EncryptionKey;
// Create the streams used for encryption.
OutputStream = new MemoryStream();
CryptoStream = new CryptoStream(OutputStream, AES.CreateEncryptor(), CryptoStreamMode.Write);
// write plain text byte array
CryptoStream.Write(PlainText, 0, PlainText.Length);
// encrypt plain text to cipher text
CryptoStream.FlushFinalBlock();
// get the result
OutputStream.Seek(0, SeekOrigin.Begin);
OutputStream.Read(CipherText, 16, CipherTextLen);
// release resources
CryptoStream.Clear();
OutputStream.Close();
}
else
{
CipherText = (Byte[]) PlainText.Clone();
EncryptRC4(EncryptionKey, CipherText);
}
// return result
return(CipherText);
}