本文整理汇总了C#中CryptoStream.FlushFinalBlock方法的典型用法代码示例。如果您正苦于以下问题:C# CryptoStream.FlushFinalBlock方法的具体用法?C# CryptoStream.FlushFinalBlock怎么用?C# CryptoStream.FlushFinalBlock使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类CryptoStream
的用法示例。
在下文中一共展示了CryptoStream.FlushFinalBlock方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: TestKnownEnc
static Boolean TestKnownEnc(Aes aes, Byte[] Key, Byte[] IV, Byte[] Plain, Byte[] Cipher)
{
Byte[] CipherCalculated;
Console.WriteLine("Encrypting the following bytes:");
PrintByteArray(Plain);
Console.WriteLine("With the following Key:");
PrintByteArray(Key);
Console.WriteLine("and IV:");
PrintByteArray(IV);
Console.WriteLine("Expecting this ciphertext:");
PrintByteArray(Cipher);
ICryptoTransform sse = aes.CreateEncryptor(Key, IV);
MemoryStream ms = new MemoryStream();
CryptoStream cs = new CryptoStream(ms, sse, CryptoStreamMode.Write);
cs.Write(Plain,0,Plain.Length);
cs.FlushFinalBlock();
CipherCalculated = ms.ToArray();
cs.Close();
Console.WriteLine("Computed this cyphertext:");
PrintByteArray(CipherCalculated);
if (!Compare(Cipher, CipherCalculated)) {
Console.WriteLine("ERROR: result is different from the expected");
return false;
}
Console.WriteLine("OK");
return true;
}
示例2: Encrypt
public static string Encrypt(this string text, string lKey)
{
try
{
using (Aes aes = new AesManaged())
{
Rfc2898DeriveBytes deriveBytes = new Rfc2898DeriveBytes(Encoding.UTF8.GetString(IVa, 0, IVa.Length), Encoding.UTF8.GetBytes(lKey));
aes.Key = deriveBytes.GetBytes(128 / 8);
aes.IV = aes.Key;
using (MemoryStream encryptionStream = new MemoryStream())
{
using (CryptoStream encrypt = new CryptoStream(encryptionStream, aes.CreateEncryptor(), CryptoStreamMode.Write))
{
byte[] cleanText = Encoding.UTF8.GetBytes(text);
encrypt.Write(cleanText, 0, cleanText.Length);
encrypt.FlushFinalBlock();
}
byte[] encryptedData = encryptionStream.ToArray();
string encryptedText = Convert.ToBase64String(encryptedData);
return encryptedText;
}
}
}
catch
{
return String.Empty;
}
}
示例3: Decrypt
//cmThe function used to decrypt the text
private static string Decrypt(string strText, string SaltKey)
{
byte[] byKey = { };
byte[] IV = { 0x12, 0x34, 0x56, 0x78, 0x90, 0xab, 0xcd, 0xef };
byte[] inputByteArray = new byte[strText.Length + 1];
try
{
byKey = System.Text.Encoding.UTF8.GetBytes(SaltKey.Substring(0, 8));
DESCryptoServiceProvider des = new DESCryptoServiceProvider();
inputByteArray = Convert.FromBase64String(strText);
MemoryStream ms = new MemoryStream();
CryptoStream cs = new CryptoStream(ms, des.CreateDecryptor(byKey, IV), CryptoStreamMode.Write);
cs.Write(inputByteArray, 0, inputByteArray.Length);
cs.FlushFinalBlock();
System.Text.Encoding encoding = System.Text.Encoding.UTF8;
return encoding.GetString(ms.ToArray());
}
catch (Exception ex)
{
return ex.Message;
}
}
示例4: ForCodeCoverage
public static bool ForCodeCoverage()
{
AesManaged aes = new AesManaged();
using (ICryptoTransform cte = aes.CreateEncryptor(), ctd = aes.CreateDecryptor())
{
byte[] plain1 = new byte[64];
for (int i = 0; i < plain1.Length; i++)
plain1[i] = (byte)i;
byte[] encr1 = cte.TransformFinalBlock(plain1, 0, plain1.Length);
using(MemoryStream ms = new MemoryStream())
using(CryptoStream cs = new CryptoStream(ms, ctd, CryptoStreamMode.Write))
{
cs.Write(encr1, 0, 16);
cs.Write(encr1, 16, 16);
cs.Write(encr1, 32, 16);
cs.Write(encr1, 48, encr1.Length-48);
cs.FlushFinalBlock();
byte[] plain2 = ms.ToArray();
if (!Compare(plain1, plain2))
{
Console.WriteLine("CodeCoverage case failed");
return false;
}
return true;
}
}
}
示例5: DecryptString
public static string DecryptString(string input, string cryptographicProvider_PublicKey, string cryptographicProvider_IV)
{
try
{
input = input.Replace(CryptographicProviderKey, String.Empty);
using (var dataString = new MemoryStream())
{
var service = new TripleDESCryptoServiceProvider().CreateDecryptor(Encoding.UTF8.GetBytes(cryptographicProvider_PublicKey),
Encoding.UTF8.GetBytes(cryptographicProvider_IV));
using (var cryptoStream = new CryptoStream(dataString, service, CryptoStreamMode.Write))
{
var inputData = Convert.FromBase64String(input);
cryptoStream.Write(inputData, 0, inputData.Length);
cryptoStream.FlushFinalBlock();
var dataResult = dataString.ToArray();
var resultString = Encoding.UTF8.GetString(Convert.FromBase64String(Convert.ToBase64String(dataResult)));
cryptoStream.Close();
return resultString;
}
}
}
catch (Exception e)
{
return e.ToString();
}
}
示例6: Encrypt
/// <summary>
/// Encrypt some text and return an encrypted byte array.
/// </summary>
/// <param name="TextValue">The Text to encrypt</param>
/// <returns>byte[] of the encrypted value</returns>
public byte[] Encrypt(string TextValue)
{
//Translates our text value into a byte array.
Byte[] bytes = UTFEncoder.GetBytes(TextValue);
//Used to stream the data in and out of the CryptoStream.
MemoryStream memoryStream = new MemoryStream();
/*
* We will have to write the unencrypted bytes to the
* stream, then read the encrypted result back from the
* stream.
*/
#region Write the decrypted value to the encryption stream
CryptoStream cs = new CryptoStream(memoryStream,
EncryptorTransform, CryptoStreamMode.Write);
cs.Write(bytes, 0, bytes.Length);
cs.FlushFinalBlock();
#endregion
#region Read encrypted value back out of the stream
memoryStream.Position = 0;
byte[] encrypted = new byte[memoryStream.Length];
memoryStream.Read(encrypted, 0, encrypted.Length);
#endregion
//Clean up.
cs.Close();
memoryStream.Close();
return encrypted;
}
示例7: Encrypt
public string Encrypt(string item, string password)
{
if (item == null) {
throw new ArgumentNullException ("item");
}
if (string.IsNullOrEmpty (password)) {
throw new ArgumentNullException ("password");
}
var des = new DESCryptoServiceProvider ();
des.GenerateIV ();
var rfc2898DeriveBytes = new Rfc2898DeriveBytes (password, des.IV, ITERATIONS);
byte[] key = rfc2898DeriveBytes.GetBytes (8);
using (var memoryStream = new MemoryStream()) {
using (var cryptoStream = new CryptoStream(memoryStream, des.CreateEncryptor(key, des.IV), CryptoStreamMode.Write)) {
memoryStream.Write (des.IV, 0, des.IV.Length);
byte[] bytes = Encoding.UTF8.GetBytes (item);
cryptoStream.Write (bytes, 0, bytes.Length);
cryptoStream.FlushFinalBlock ();
return Convert.ToBase64String (memoryStream.ToArray ());
}
}
}
示例8: Decrypt
public static string Decrypt(string inName, string password)
{
PasswordDeriveBytes pdb = new PasswordDeriveBytes(password, keySalt);
Rijndael alg = Rijndael.Create();
alg.Key = pdb.GetBytes(32);
alg.IV = pdb.GetBytes(16);
using (var fin = new FileStream(inName, FileMode.Open, FileAccess.Read))
using (var fout = new MemoryStream())
using (var cs = new CryptoStream(fout, alg.CreateDecryptor(), CryptoStreamMode.Write))
{
byte[] buffer = new byte[4096];
int bytesRead;
do
{
bytesRead = fin.Read(buffer, 0, buffer.Length);
cs.Write(buffer, 0, bytesRead);
}
while (bytesRead != 0);
cs.FlushFinalBlock();
return Encoding.ASCII.GetString(fout.ToArray());
}
}
示例9: Main
public static void Main() {
string PlainText = "Titan";
byte[] PlainBytes = new byte[5];
PlainBytes = Encoding.ASCII.GetBytes(PlainText.ToCharArray());
PrintByteArray(PlainBytes);
byte[] CipherBytes = new byte[8];
PasswordDeriveBytes pdb = new PasswordDeriveBytes("Titan", null);
byte[] IV = new byte[8];
byte[] Key = pdb.CryptDeriveKey("RC2", "SHA1", 40, IV);
PrintByteArray(Key);
PrintByteArray(IV);
// Now use the data to encrypt something
RC2CryptoServiceProvider rc2 = new RC2CryptoServiceProvider();
Console.WriteLine(rc2.Padding);
Console.WriteLine(rc2.Mode);
ICryptoTransform sse = rc2.CreateEncryptor(Key, IV);
MemoryStream ms = new MemoryStream();
CryptoStream cs1 = new CryptoStream(ms, sse, CryptoStreamMode.Write);
cs1.Write(PlainBytes, 0, PlainBytes.Length);
cs1.FlushFinalBlock();
CipherBytes = ms.ToArray();
cs1.Close();
Console.WriteLine(Encoding.ASCII.GetString(CipherBytes));
PrintByteArray(CipherBytes);
ICryptoTransform ssd = rc2.CreateDecryptor(Key, IV);
CryptoStream cs2 = new CryptoStream(new MemoryStream(CipherBytes), ssd, CryptoStreamMode.Read);
byte[] InitialText = new byte[5];
cs2.Read(InitialText, 0, 5);
Console.WriteLine(Encoding.ASCII.GetString(InitialText));
PrintByteArray(InitialText);
}
示例10: Encrypt
public static string Encrypt(string originalString, string sKey)
{
DESCryptoServiceProvider des = new DESCryptoServiceProvider();
// 把字符串放到byte数组中
byte[] inputByteArray = Encoding.Default.GetBytes(originalString);
des.Key = ASCIIEncoding.ASCII.GetBytes(sKey); //建立加密对象的密钥和偏移量
des.IV = ASCIIEncoding.ASCII.GetBytes(sKey); //原文使用ASCIIEncoding.ASCII方法的
//GetBytes方法
MemoryStream ms = new MemoryStream(); //使得输入密码必须输入英文文本
CryptoStream cs = new CryptoStream(ms, des.CreateEncryptor(), CryptoStreamMode.Write);
cs.Write(inputByteArray, 0, inputByteArray.Length);
cs.FlushFinalBlock();
StringBuilder ret = new StringBuilder();
foreach (byte b in ms.ToArray())
{
ret.AppendFormat("{0:X2}", b);
}
ret.ToString();
return ret.ToString();
}
示例11: Decrypt
public static string Decrypt(string originalString, string sKey)
{
DESCryptoServiceProvider des = new DESCryptoServiceProvider();
byte[] inputByteArray = new byte[originalString.Length / 2];
for (int x = 0; x < originalString.Length / 2; x++)
{
int i = (Convert.ToInt32(originalString.Substring(x * 2, 2), 16));
inputByteArray[x] = (byte)i;
}
//建立加密对象的密钥和偏移量,此值重要,不能修改
des.Key = ASCIIEncoding.ASCII.GetBytes(sKey);
des.IV = ASCIIEncoding.ASCII.GetBytes(sKey);
MemoryStream ms = new MemoryStream();
CryptoStream cs = new CryptoStream(ms, des.CreateDecryptor(), CryptoStreamMode.Write);
cs.Write(inputByteArray, 0, inputByteArray.Length);
cs.FlushFinalBlock();
//建立StringBuild对象,CreateDecrypt使用的是流对象,必须把解密后的文本变成流对象
StringBuilder ret = new StringBuilder();
return System.Text.Encoding.Default.GetString(ms.ToArray());
}
示例12: Encrypt
public static string Encrypt(string plainText, string passPhrase)
{
byte[] plainTextBytes = Encoding.UTF8.GetBytes(plainText);
using (PasswordDeriveBytes password = new PasswordDeriveBytes(passPhrase, null))
{
byte[] keyBytes = password.GetBytes(keysize / 8);
using (RijndaelManaged symmetricKey = new RijndaelManaged())
{
symmetricKey.Mode = CipherMode.CBC;
using (ICryptoTransform encryptor = symmetricKey.CreateEncryptor(keyBytes, initVectorBytes))
{
using (MemoryStream memoryStream = new MemoryStream())
{
using (CryptoStream cryptoStream = new CryptoStream(memoryStream, encryptor, CryptoStreamMode.Write))
{
cryptoStream.Write(plainTextBytes, 0, plainTextBytes.Length);
cryptoStream.FlushFinalBlock();
byte[] cipherTextBytes = memoryStream.ToArray();
return Convert.ToBase64String(cipherTextBytes);
}
}
}
}
}
}
示例13: Decrypt
public static string Decrypt(string stringToDecrypt)
{
string sEncryptionKey = "thuynnxkey";
byte[] key = { };
byte[] IV = { 10, 20, 30, 40, 50, 60, 70, 80 };
byte[] inputByteArray = new byte[stringToDecrypt.Length];
try
{
key = Encoding.UTF8.GetBytes(sEncryptionKey.Substring(0, 8));
DESCryptoServiceProvider des = new DESCryptoServiceProvider();
inputByteArray = Convert.FromBase64String(stringToDecrypt);
MemoryStream ms = new MemoryStream();
CryptoStream cs = new CryptoStream(ms, des.CreateDecryptor(key, IV), CryptoStreamMode.Write);
cs.Write(inputByteArray, 0, inputByteArray.Length);
cs.FlushFinalBlock();
cs.Close();
Encoding encoding = Encoding.UTF8;
return encoding.GetString(ms.ToArray());
}
catch (System.Exception ex)
{
SiAuto.Main.LogString("Lỗi :", ex.ToString());
return (string.Empty);
}
}
示例14: DeCryption
private string DeCryption(string content, string sKey, string sIV)
{
try
{
byte[] inputByteArray = Convert.FromBase64String(content);
using (DESCryptoServiceProvider desProvider = new DESCryptoServiceProvider())
{
//byte[] inputByteArray = Convert.FromBase64String(content);
desProvider.Key = System.Text.Encoding.ASCII.GetBytes(sKey);
desProvider.IV = System.Text.Encoding.ASCII.GetBytes(sIV);
System.IO.MemoryStream ms = new System.IO.MemoryStream();
using (CryptoStream cs = new CryptoStream(ms,desProvider.CreateDecryptor(), CryptoStreamMode.Write))
{
//cs.Clear();
cs.Write(inputByteArray,0,inputByteArray.Length);
cs.FlushFinalBlock();
cs.Close();
}
string str = System.Text.Encoding.UTF8.GetString(ms.ToArray());
ms.Close();
return str;
}
}
catch
{
return "String Not Base64 Encode!!!";
}
}
示例15: Encrypt
/// <summary>
/// Encrypt string using AES 128
/// </summary>
/// <param name="plaintext"></param>
/// <param name="key"></param>
/// <returns></returns>
public static string Encrypt(string plaintext, string key)
{
byte[] keybytes = Encoding.UTF8.GetBytes(key);
RijndaelManaged aes = new RijndaelManaged();
aes.Mode = CipherMode.CBC;
aes.Padding = PaddingMode.None;
byte[] IVbytes = Encoding.ASCII.GetBytes("dongbinhuiasxiny");
ICryptoTransform encryptor = aes.CreateEncryptor(keybytes, IVbytes);
System.IO.MemoryStream ms = new System.IO.MemoryStream();
CryptoStream cs = new CryptoStream(ms, encryptor, CryptoStreamMode.Write);
byte[] plainBytes = Encoding.UTF8.GetBytes(Convert.ToBase64String(Encoding.UTF8.GetBytes(plaintext)));
cs.Write(plainBytes, 0, plainBytes.Length);
cs.FlushFinalBlock();
byte[] cipherBytes = ms.ToArray();
ms.Close();
cs.Close();
return Convert.ToBase64String(cipherBytes, 0, cipherBytes.Length);
}