本文整理汇总了C#中System.Security.Cryptography.CryptoStream.Flush方法的典型用法代码示例。如果您正苦于以下问题:C# CryptoStream.Flush方法的具体用法?C# CryptoStream.Flush怎么用?C# CryptoStream.Flush使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类System.Security.Cryptography.CryptoStream
的用法示例。
在下文中一共展示了CryptoStream.Flush方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: GetDeCryptedMemoryStream
public static MemoryStream GetDeCryptedMemoryStream(string outputFilePath, string password = "password")
{
var ms = new MemoryStream();
var inFile = new FileStream(outputFilePath, FileMode.Open, FileAccess.Read);
var algorithm = GetAlgorithm(password);
//There could be a case, where more data is appended than required, as a result Serialization fails
//var length = inFile.Length > 1024 ? 1024 : inFile.Length;
var length = 1;
var fileData = new byte[length];
var encryptedStream = new CryptoStream(inFile, algorithm.CreateDecryptor(), CryptoStreamMode.Read);
while (encryptedStream.Read(fileData, 0, fileData.Length) != 0)
{
ms.Write(fileData, 0, fileData.Length);
}
encryptedStream.Flush();
encryptedStream.Close();
inFile.Close();
ms.Position = 0;
return ms;
}
示例2: Decrypt
/// <summary>
/// Decrypt a string
/// </summary>
/// <param name="input">Input string in base 64 format</param>
/// <returns>Decrypted string</returns>
public static string Decrypt(string input, string password)
{
byte[] encryptedBytes = Convert.FromBase64String(input);
byte[] saltBytes = Encoding.UTF8.GetBytes(password);
string decryptedString = string.Empty;
using (var aes = new AesManaged())
{
Rfc2898DeriveBytes rfc = new Rfc2898DeriveBytes(password, saltBytes);
aes.BlockSize = aes.LegalBlockSizes[0].MaxSize;
aes.KeySize = aes.LegalKeySizes[0].MaxSize;
aes.Key = rfc.GetBytes(aes.KeySize / 8);
aes.IV = rfc.GetBytes(aes.BlockSize / 8);
using (ICryptoTransform decryptTransform = aes.CreateDecryptor())
{
using (MemoryStream decryptedStream = new MemoryStream())
{
CryptoStream decryptor =
new CryptoStream(decryptedStream, decryptTransform, CryptoStreamMode.Write);
decryptor.Write(encryptedBytes, 0, encryptedBytes.Length);
decryptor.Flush();
decryptor.Close();
byte[] decryptBytes = decryptedStream.ToArray();
decryptedString =
UTF8Encoding.UTF8.GetString(decryptBytes, 0, decryptBytes.Length);
}
}
}
return decryptedString;
}
示例3: Encrypt
public static string Encrypt(string key, string text)
{
// Our symmetric encryption algorithm
AesManaged aes = new AesManaged();
// We're using the PBKDF2 standard for password-based key generation
Rfc2898DeriveBytes rfc = new Rfc2898DeriveBytes("password", Encoding.UTF8.GetBytes(key));
// Setting our parameters
aes.BlockSize = aes.LegalBlockSizes[0].MaxSize;
aes.KeySize = aes.LegalKeySizes[0].MaxSize;
aes.Key = rfc.GetBytes(aes.KeySize / 8);
aes.IV = rfc.GetBytes(aes.BlockSize / 8);
// Encryption
ICryptoTransform encryptTransf = aes.CreateEncryptor();
// Output stream, can be also a FileStream
MemoryStream encryptStream = new MemoryStream();
CryptoStream encryptor = new CryptoStream(encryptStream, encryptTransf, CryptoStreamMode.Write);
byte[] utfData = Encoding.Unicode.GetBytes(text);
encryptor.Write(utfData, 0, utfData.Length);
encryptor.Flush();
encryptor.Close();
// return encrypted content
return Convert.ToBase64String(encryptStream.ToArray());
}
示例4: Encrypt
/// <summary>
///
/// </summary>
/// <param name="value"></param>
/// <returns></returns>
public static string Encrypt(string password)
{
byte[] utfData = UTF8Encoding.UTF8.GetBytes(password);
byte[] saltBytes = Encoding.UTF8.GetBytes("Element5");
string encryptedString = string.Empty;
using (AesManaged aes = new AesManaged())
{
Rfc2898DeriveBytes rfc = new Rfc2898DeriveBytes(password, saltBytes);
aes.BlockSize = aes.LegalBlockSizes[0].MaxSize;
aes.KeySize = aes.LegalKeySizes[0].MaxSize;
aes.Key = rfc.GetBytes(aes.KeySize / 8);
aes.IV = rfc.GetBytes(aes.BlockSize / 8);
using (ICryptoTransform encryptTransform = aes.CreateEncryptor())
{
using (MemoryStream encryptedStream = new MemoryStream())
{
using (CryptoStream encryptor =
new CryptoStream(encryptedStream, encryptTransform, CryptoStreamMode.Write))
{
encryptor.Write(utfData, 0, utfData.Length);
encryptor.Flush();
encryptor.Close();
byte[] encryptBytes = encryptedStream.ToArray();
encryptedString = Convert.ToBase64String(encryptBytes);
encryptedString = encryptedString.Substring(0, 20);
}
}
}
}
return encryptedString;
}
示例5: Decrypt
public string Decrypt(byte[] key, string encryptedString)
{
// Initialize
AesManaged decryptor = new AesManaged();
byte[] encryptedData = Convert.FromBase64String(encryptedString);
// Set the key
decryptor.Key = key;
decryptor.IV = key;
// create a memory stream
using (MemoryStream decryptionStream = new MemoryStream())
{
// Create the crypto stream
using (
CryptoStream decrypt = new CryptoStream(decryptionStream, decryptor.CreateDecryptor(),
CryptoStreamMode.Write))
{
// Encrypt
decrypt.Write(encryptedData, 0, encryptedData.Length);
decrypt.Flush();
decrypt.Close();
// Return the unencrypted data
byte[] decryptedData = decryptionStream.ToArray();
return UTF8Encoding.UTF8.GetString(decryptedData, 0, decryptedData.Length);
}
}
}
示例6: DecryptString
/// <summary>
/// Decrypt a Base64 string using Triple Data Encryption Standard(TDES) 256 bit symmetric encryption format
/// and returns the decrypted string. The decryption key is generated based on the FiWare
/// framework assembly public key.
/// </summary>
/// <param name="valueToDecrypt">Input String to decrypy in Base64 format.</param>
/// <returns>Decrypted output string.</returns>
public string DecryptString(string valueToDecrypt)
{
using (MemoryStream decryVal = new MemoryStream())
{
using (TripleDESCryptoServiceProvider cryptoProvider = new TripleDESCryptoServiceProvider())
{
symKey = new byte[24];
symIV = new byte[8];
byte[] publicKey = Assembly.GetExecutingAssembly().GetName().GetPublicKey();
Encoding encoding = Encoding.UTF8;
string keyValue = encoding.GetString(publicKey);
GenerateKeyVector(keyValue);
cryptoProvider.Key = symKey;
cryptoProvider.IV = symIV;
using (ICryptoTransform cryptoTrans = cryptoProvider.CreateDecryptor())
{
using (CryptoStream cryptoStreamDecr = new CryptoStream(decryVal, cryptoTrans, CryptoStreamMode.Write))
{
byte[] arrayInput = Convert.FromBase64String(valueToDecrypt);
cryptoStreamDecr.Write(arrayInput, 0, arrayInput.Length);
cryptoStreamDecr.Flush();
}
}
return encoding.GetString(decryVal.ToArray());
}
}
}
示例7: EncryptFile
public static bool EncryptFile(string alg, string key, string srcFile, string dstFile)
{
try
{
SymmetricAlgorithm cipher = SymmetricAlgorithm.Create(alg);
cipher.Key = Encoding.Default.GetBytes(key);
cipher.IV = cipher.Key;
FileStream iStream = File.OpenRead(srcFile);
FileStream oStream = new FileStream(dstFile, FileMode.Create);
CryptoStream cStream = new CryptoStream(oStream, cipher.CreateEncryptor(), CryptoStreamMode.Write);
byte[] buffer = new byte[4096];
int len = iStream.Read(buffer, 0, buffer.Length);
while (len > 0)
{
cStream.Write(buffer, 0, len);
len = iStream.Read(buffer, 0, buffer.Length);
}
cStream.Flush();
cStream.Close();
oStream.Close();
iStream.Close();
return true;
}
catch
{
return false;
}
}
示例8: Encrypt
public string Encrypt(string data)
{
try
{
List<byte> hexString = new List<byte>(Encoding.Default.GetBytes(data));
while (hexString.Count % 16 != 0)
{
hexString.Add(0x00);
}
_rijndael.Key = _chatKey;
CryptoStream stream = new CryptoStream(new MemoryStream(hexString.ToArray()), _rijndael.CreateEncryptor(), CryptoStreamMode.Read);
MemoryStream textBytes = new MemoryStream();
#if NET20 || NET35
byte[] buffer = new byte[1024];
int read = stream.Read(buffer, 0, buffer.Length);
stream.Flush();
textBytes.Write(buffer, 0, read);
#else
stream.CopyTo(textBytes);
#endif
return Convert.ToBase64String(textBytes.ToArray());
}
catch (CryptographicException e)
{
Debug.WriteLine("A Cryptographic error occurred: {0}", e.Message);
return null;
}
}
示例9: EncryptAesCbc
/// <summary>
/// Encrypt a message using AES in CBC (cipher-block chaining) mode.
/// </summary>
/// <param name="plaintext">The message (plaintext) to encrypt</param>
/// <param name="key">An AES key</param>
/// <param name="iv">The IV to use or null to use a 0 IV</param>
/// <param name="addHmac">When set, a SHA256-based HMAC (HMAC256) of 32 bytes using the same key is added to the plaintext
/// before it is encrypted.</param>
/// <returns>The ciphertext derived by encrypting the orignal message using AES in CBC mode</returns>
public static byte[] EncryptAesCbc(byte[] plaintext, byte[] key, byte[] iv = null, bool addHmac = false)
{
using (Aes aes =Aes.Create())
// using (AesCryptoServiceProvider aes = new AesCryptoServiceProvider())
{
aes.Key = key;
if (iv == null)
iv = NullIv;
aes.Mode = CipherMode.CBC;
aes.IV = iv;
// Encrypt the message with the key using CBC and InitializationVector=0
byte[] cipherText;
using (System.IO.MemoryStream ciphertext = new System.IO.MemoryStream())
{
using (CryptoStream cs = new CryptoStream(ciphertext, aes.CreateEncryptor(), CryptoStreamMode.Write))
{
cs.Write(plaintext, 0, plaintext.Length);
if (addHmac)
{
byte[] hmac = new HMACSHA256(key).ComputeHash(plaintext);
cs.Write(hmac, 0, hmac.Length);
}
cs.Flush();
}
cipherText = ciphertext.ToArray();
}
return cipherText;
}
}
示例10: SyncCrypt
/// <summary>
/// Metoda za simetrično kriptiranje
/// </summary>
/// <param name="file"></param>
public void SyncCrypt(string file)
{
string zapis;
zapis = file + ".ecb";
FileStream fstreamU = File.OpenRead(file),
fstreamO = File.OpenWrite(zapis);
long lSize = fstreamU.Length;
byte[] bytes = new byte[BufferSize];
int read = -1;
Rijndael rijndaelAlg = Rijndael.Create();
rijndaelAlg.Mode = CipherMode.ECB;
TextReader streamreader = new StreamReader("tajni_kljuc.txt");
string secretKey = streamreader.ReadLine();
rijndaelAlg.Key = Convert.FromBase64String(secretKey);
streamreader.Close();
CryptoStream cout = new CryptoStream(fstreamO, rijndaelAlg.CreateEncryptor(), CryptoStreamMode.Write);
BinaryWriter bw = new BinaryWriter(cout);
bw.Write(lSize);
while ((read = fstreamU.Read(bytes, 0, bytes.Length)) != 0)
{
cout.Write(bytes, 0, read);
}
cout.Flush();
cout.Close();
cout.Dispose();
fstreamU.Flush();
fstreamU.Close();
fstreamU.Dispose();
}
示例11: InvalidOperationException
EncryptedValue IEncryptionService.Encrypt(string value)
{
if (Key == null)
throw new InvalidOperationException("Cannot encrypt because a Key was not configured. Please specify 'RijndaelEncryptionServiceConfig' in your application's configuration file.");
using (var rijndael = new RijndaelManaged())
{
rijndael.Key = Key;
rijndael.Mode = CipherMode.CBC;
rijndael.GenerateIV();
using (var encryptor = rijndael.CreateEncryptor())
using (var memoryStream = new MemoryStream())
using (var cryptoStream = new CryptoStream(memoryStream, encryptor, CryptoStreamMode.Write))
using (var writer = new StreamWriter(cryptoStream))
{
writer.Write(value);
writer.Flush();
cryptoStream.Flush();
cryptoStream.FlushFinalBlock();
return new EncryptedValue
{
EncryptedBase64Value = Convert.ToBase64String(memoryStream.ToArray()),
Base64Iv = Convert.ToBase64String(rijndael.IV)
};
}
}
}
示例12: DecryptString
/// <summary>
/// Decrypt encrypted string
/// </summary>
/// <param name="Str">Encrypted string</param>
/// <param name="Password">Password used for encryption</param>
/// <param name="Salt">Salt string used for encryption (at least 8 bytes)</param>
/// <returns>Decrypted string if success; otherwise - empty string</returns>
public static string DecryptString(string Str, string Password = "tdcm1234", string Salt = "tdcm1234")
{
try
{
using (Aes aes = new AesManaged())
{
Rfc2898DeriveBytes deriveBytes = new Rfc2898DeriveBytes(Password, Encoding.UTF8.GetBytes(Salt));
aes.Key = deriveBytes.GetBytes(128 / 8);
aes.IV = aes.Key;
using (MemoryStream decryptionStream = new MemoryStream())
{
using (CryptoStream decrypt = new CryptoStream(decryptionStream, aes.CreateDecryptor(), CryptoStreamMode.Write))
{
byte[] encryptedData = Convert.FromBase64String(Str);
decrypt.Write(encryptedData, 0, encryptedData.Length);
decrypt.Flush();
}
byte[] decryptedData = decryptionStream.ToArray();
return UTF8Encoding.UTF8.GetString(decryptedData, 0, decryptedData.Length);
}
}
}
catch (Exception ex)
{
Debug.WriteLine("Error : AES ~ DecryptString ; " + ex.Message);
return "";
}
}
示例13: Encrypt
public EncryptedValue Encrypt(string value)
{
using (var rijndael = new RijndaelManaged())
{
rijndael.Key = Key;
rijndael.Mode = CipherMode.CBC;
rijndael.GenerateIV();
using (var encryptor = rijndael.CreateEncryptor())
using (var memoryStream = new MemoryStream())
using (var cryptoStream = new CryptoStream(memoryStream, encryptor, CryptoStreamMode.Write))
using (var writer = new StreamWriter(cryptoStream))
{
writer.Write(value);
writer.Flush();
cryptoStream.Flush();
cryptoStream.FlushFinalBlock();
return new EncryptedValue
{
EncryptedBase64Value = Convert.ToBase64String(memoryStream.ToArray()),
Base64Iv = Convert.ToBase64String(rijndael.IV)
};
}
}
}
示例14: EncryptFile
public static void EncryptFile(string strInFile, string strOutFile)
{
// Create a file pointer to the text file to encrypt.
FileStream fileInText = new FileStream(strInFile,FileMode.Open, FileAccess.Read);
// Create a file pointer for the encrypted output file.
FileStream fileOutEncrypted = new FileStream(strOutFile, FileMode.Create, FileAccess.Write);
// Start encryption service and provided string parameter as key.
DESCryptoServiceProvider DES = new DESCryptoServiceProvider();
DES.Key = ASCIIEncoding.ASCII.GetBytes(KEY);
DES.IV = ASCIIEncoding.ASCII.GetBytes(KEY);
ICryptoTransform desencrypt = DES.CreateEncryptor();
// Links the key and the data in a stream.
CryptoStream cryptostream = new CryptoStream(fileOutEncrypted,desencrypt,CryptoStreamMode.Write);
byte[] bytearrayInput = new byte[fileInText.Length];
fileInText.Read(bytearrayInput, 0, bytearrayInput.Length);
cryptostream.Write(bytearrayInput, 0, bytearrayInput.Length);
cryptostream.Flush();
cryptostream.Close();
fileInText.Close();
}
示例15: FullByteRangeTest
public void FullByteRangeTest()
{
//writing
MemoryStream ms = new MemoryStream(); //this could be any stream we want to write to
YEncEncoder encoder = new YEncEncoder();
CryptoStream cs = new CryptoStream(ms, encoder, CryptoStreamMode.Write);
byte[] bytes = new byte[256];
for(int i=byte.MinValue; i<=byte.MaxValue; i++)
{
bytes[i] = (byte)i;
}
BinaryWriter w = new BinaryWriter(cs);
w.Write(bytes, 0, 256);
w.Flush();
cs.Flush();
//reading back from the memorystream
ms.Position = 0;
YEncDecoder decoder = new YEncDecoder();
CryptoStream cs2 = new CryptoStream(ms, decoder, CryptoStreamMode.Read);
BinaryReader r = new BinaryReader(cs2);
byte[] newBytes = r.ReadBytes(256);
Assert.AreEqual(BitConverter.ToString(bytes, 0, 256), BitConverter.ToString(newBytes, 0, 256));
}