本文整理汇总了C#中System.Security.Cryptography.CryptoStream.FlushFinalBlock方法的典型用法代码示例。如果您正苦于以下问题:C# CryptoStream.FlushFinalBlock方法的具体用法?C# CryptoStream.FlushFinalBlock怎么用?C# CryptoStream.FlushFinalBlock使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类System.Security.Cryptography.CryptoStream
的用法示例。
在下文中一共展示了CryptoStream.FlushFinalBlock方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: Encrypt
/// <summary>
/// 加密方法
/// </summary>
/// <param name="pToEncrypt">需要加密字符串</param>
/// <param name="sKey">密钥</param>
/// <returns>加密后的字符串</returns>
public static string Encrypt(string pToEncrypt, string sKey)
{
try
{
DESCryptoServiceProvider des = new DESCryptoServiceProvider();
//把字符串放到byte数组中
//原来使用的UTF8编码,我改成Unicode编码了,不行
byte[] inputByteArray = Encoding.Default.GetBytes(pToEncrypt);
//建立加密对象的密钥和偏移量
//使得输入密码必须输入英文文本
des.Key = ASCIIEncoding.ASCII.GetBytes(sKey);
des.IV = ASCIIEncoding.ASCII.GetBytes(sKey);
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();
}
catch (Exception ex)
{
}
return "";
}
示例2: Encode
public static string Encode(string str, string key)
{
DESCryptoServiceProvider provider = new DESCryptoServiceProvider();
provider.Key = Encoding.ASCII.GetBytes(key.Substring(0, 8));
provider.IV = Encoding.ASCII.GetBytes(key.Substring(0, 8));
byte[] bytes = Encoding.UTF8.GetBytes(str);
MemoryStream stream = new MemoryStream();
CryptoStream stream2 = new CryptoStream(stream, provider.CreateEncryptor(), CryptoStreamMode.Write);
stream2.Write(bytes, 0, bytes.Length);
stream2.FlushFinalBlock();
StringBuilder builder = new StringBuilder();
foreach (byte num in stream.ToArray())
{
builder.AppendFormat("{0:X2}", num);
}
stream.Close();
return builder.ToString();
}
示例3: Decrypt
/// <summary>
/// 解密
/// </summary>
/// <param name="text">要被解密字符</param>
/// <param name="sKey">密钥</param>
/// <returns></returns>
public static string Decrypt(this string text, string sKey)
{
var provider = new DESCryptoServiceProvider();
int num = text.Length / 2;
byte[] buffer = new byte[num];
try
{
for (int i = 0; i < num; i++)
{
int num3 = Convert.ToInt32(text.Substring(i * 2, 2), 0x10);
buffer[i] = (byte)num3;
}
}
catch
{
return string.Empty;
}
provider.Key = Encoding.ASCII.GetBytes(FormsAuthentication.HashPasswordForStoringInConfigFile(sKey, "md5").Substring(0, 8));
provider.IV = Encoding.ASCII.GetBytes(FormsAuthentication.HashPasswordForStoringInConfigFile(sKey, "md5").Substring(0, 8));
MemoryStream stream = new MemoryStream();
CryptoStream stream2 = new CryptoStream(stream, provider.CreateDecryptor(), CryptoStreamMode.Write);
try
{
stream2.Write(buffer, 0, buffer.Length);
stream2.FlushFinalBlock();
}
catch
{
return string.Empty;
}
return Encoding.Default.GetString(stream.ToArray());
}
示例4: Encrypt
public static string Encrypt( string strEncryptString, string strEncryptionKey)
{
byte[] inputByteArray;
try
{
key = Encoding.UTF8.GetBytes(strEncryptionKey.Substring(0, 8));
DESCryptoServiceProvider des = new DESCryptoServiceProvider();
inputByteArray = Encoding.UTF8.GetBytes(strEncryptString);
MemoryStream ms = new MemoryStream();
CryptoStream cs = new CryptoStream(ms, des.CreateEncryptor(key, IV), CryptoStreamMode.Write);
cs.Write(inputByteArray, 0, inputByteArray.Length);
cs.FlushFinalBlock();
return Convert.ToBase64String(ms.ToArray());
}
catch (Exception eX)
{
throw eX;
}
}
示例5: EncryptString
public static string EncryptString(
string plainText,
string passPhrase,
string saltValue,
int passwordIterations,
string initVector,
int keySize)
{
byte[] initVectorBytes = initVector == null ? new byte[16] : Encoding.ASCII.GetBytes(initVector);
byte[] plainTextBytes = Encoding.UTF8.GetBytes(plainText);
byte[] keyBytes = GetKeyBytes(passPhrase, saltValue, passwordIterations, keySize);
RijndaelManaged symmetricKey = new RijndaelManaged();
symmetricKey.Mode = CipherMode.CBC;
ICryptoTransform encryptor = symmetricKey.CreateEncryptor(keyBytes, initVectorBytes);
byte[] cipherTextBytes;
using (MemoryStream memoryStream = new MemoryStream())
{
using (CryptoStream cryptoStream = new CryptoStream(memoryStream, encryptor, CryptoStreamMode.Write))
{
cryptoStream.Write(plainTextBytes, 0, plainTextBytes.Length);
cryptoStream.FlushFinalBlock();
cipherTextBytes = memoryStream.ToArray();
}
}
string cipherText = Convert.ToBase64String(cipherTextBytes);
return cipherText;
}
示例6: Encrypt
public static string Encrypt(string plainText, string passPhrase)
{
// Salt and IV is randomly generated each time, but is preprended to encrypted cipher text
// so that the same Salt and IV values can be used when decrypting.
var saltStringBytes = Generate256BitsOfRandomEntropy();
var ivStringBytes = Generate256BitsOfRandomEntropy();
var plainTextBytes = Encoding.UTF8.GetBytes(plainText);
var password = new Rfc2898DeriveBytes(passPhrase, saltStringBytes, DerivationIterations);
var keyBytes = password.GetBytes(Keysize / 8);
using (var symmetricKey = new RijndaelManaged())
{
symmetricKey.BlockSize = 256;
symmetricKey.Mode = CipherMode.CBC;
symmetricKey.Padding = PaddingMode.PKCS7;
using (var encryptor = symmetricKey.CreateEncryptor(keyBytes, ivStringBytes))
{
using (var memoryStream = new MemoryStream())
{
using (var cryptoStream = new CryptoStream(memoryStream, encryptor, CryptoStreamMode.Write))
{
cryptoStream.Write(plainTextBytes, 0, plainTextBytes.Length);
cryptoStream.FlushFinalBlock();
// Create the final bytes as a concatenation of the random salt bytes, the random iv bytes and the cipher bytes.
var cipherTextBytes = saltStringBytes;
cipherTextBytes = cipherTextBytes.Concat(ivStringBytes).ToArray();
cipherTextBytes = cipherTextBytes.Concat(memoryStream.ToArray()).ToArray();
memoryStream.Close();
cryptoStream.Close();
return Convert.ToBase64String(cipherTextBytes);
}
}
}
}
}
示例7: 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);
}
}
}
}
}
}
示例8: EncodeStr
/// <summary>
/// DES 加密算法
/// </summary>
/// <param name="str">待加密字符串</param>
/// <returns>加密后字符串</returns>
public static string EncodeStr(string str)
{
try
{
byte[] temp = Encoding.UTF8.GetBytes(str);
using (DESCryptoServiceProvider desc = new DESCryptoServiceProvider())
{
desc.Key = ASCIIEncoding.ASCII.GetBytes(key); desc.IV = ASCIIEncoding.ASCII.GetBytes(iv);
using (MemoryStream ms = new MemoryStream())
{
using (CryptoStream cs = new CryptoStream(ms, desc.CreateEncryptor(), CryptoStreamMode.Write))
{
cs.Write(temp, 0, temp.Length);
cs.FlushFinalBlock();
}
StringBuilder sb = new StringBuilder();
foreach (byte b in ms.ToArray())
{
sb.AppendFormat("{0:X2}", b);
}
return sb.ToString();
}
}
}
catch (Exception ex)
{
throw ex;
}
}
示例9: Encrypt
/// <summary>
/// ��������
/// </summary>
/// <param name="Text">ԭ��</param>
/// <param name="sKey">��Կ</param>
/// <returns>����</returns>
public static string Encrypt(string Text, string sKey)
{
DESCryptoServiceProvider desKey = new DESCryptoServiceProvider();
byte[] inputByteArray = Encoding.Default.GetBytes(Text);
byte[] keyByteArray = Encoding.Default.GetBytes(sKey);
MD5CryptoServiceProvider md5 = new MD5CryptoServiceProvider();
md5.ComputeHash(keyByteArray);
desKey.Key = HalveByteArray(md5.Hash);
desKey.IV = HalveByteArray(md5.Hash);
System.IO.MemoryStream ms = new System.IO.MemoryStream();
CryptoStream cs = new CryptoStream(ms, desKey.CreateEncryptor(), CryptoStreamMode.Write);
cs.Write(inputByteArray, 0, inputByteArray.Length);
cs.FlushFinalBlock();
StringBuilder result = new StringBuilder();
foreach (byte b in ms.ToArray())
{
result.AppendFormat("{0:X2}", b);
}
return result.ToString();
}
示例10: Encrypt
public static string Encrypt(string plainText)
{
var registryKey =
Registry.LocalMachine.OpenSubKey(@"SOFTWARE\Microsoft\VisualStudio\10.0\PbsEncryptionKey");
var password = (string)registryKey.GetValue("Key");
var passwordDerivedBytes = new PasswordDeriveBytes(password, _salt);
var plainTextBytes = Encoding.Unicode.GetBytes(plainText);
using (var memoryStream = new MemoryStream())
{
using (Rijndael rijndael = Rijndael.Create())
{
using (
ICryptoTransform cryptoTransform = rijndael.CreateEncryptor(passwordDerivedBytes.GetBytes(32), rgbIV: passwordDerivedBytes.GetBytes(16)))
{
using (
var cryptoStream = new CryptoStream(memoryStream, cryptoTransform,
CryptoStreamMode.Write))
{
cryptoStream.Write(plainTextBytes, 0, plainTextBytes.Length);
cryptoStream.FlushFinalBlock();
return Convert.ToBase64String(memoryStream.ToArray());
}
}
}
}
}
示例11: Encrypt
/// <summary>
/// 用给定的Key进行加密
/// </summary>
/// <param name="key">密钥</param>
/// <param name="encryptString">要加密的字符串</param>
/// <returns>加密后的字符串</returns>
public static string Encrypt(string encryptString, string key)
{
if (string.IsNullOrWhiteSpace(key))
key = _Key;
else
key += _Key;
if (string.IsNullOrWhiteSpace(encryptString))
encryptString = string.Empty;
byte[] keyBytes = Encoding.UTF8.GetBytes(key.Substring(0, 8));
byte[] keyIV = keyBytes;
byte[] inputByteArray = Encoding.UTF8.GetBytes(encryptString);
using (DESCryptoServiceProvider provider = new DESCryptoServiceProvider())
{
MemoryStream mStream = new MemoryStream();
CryptoStream cStream = new CryptoStream(mStream, provider.CreateEncryptor(keyBytes, keyIV), CryptoStreamMode.Write);
cStream.Write(inputByteArray, 0, inputByteArray.Length);
cStream.FlushFinalBlock();
//Get the data back from the memory stream, and into a string
StringBuilder ret = new StringBuilder();
foreach (byte b in mStream.ToArray())
{
//Format as hex
ret.AppendFormat("{0:X2}", b);
}
return ret.ToString();
}
}
示例12: GetCriptografiaSimetrica
/// <summary>
/// A chave deve possuir 16 com caracteres "1234567890123456"
/// </summary>
/// <param name="str"></param>
/// <param name="key"></param>
/// <returns></returns>
public static string GetCriptografiaSimetrica(this string str, string key)
{
using (TripleDESCryptoServiceProvider provider = new TripleDESCryptoServiceProvider())
{
provider.Mode = CipherMode.CFB;
provider.Padding = PaddingMode.PKCS7;
MemoryStream mStream = new MemoryStream();
CryptoStream cs = new CryptoStream(mStream, provider.CreateEncryptor(Encoding.UTF8.GetBytes(key), new byte[] { 138, 154, 251, 188, 64, 108, 167, 121 }), CryptoStreamMode.Write);
byte[] toEncrypt = new UTF8Encoding().GetBytes(str);
cs.Write(toEncrypt, 0, toEncrypt.Length);
cs.FlushFinalBlock();
byte[] ret = mStream.ToArray();
mStream.Close();
cs.Close();
str = Convert.ToBase64String(ret);
}
return str;
}
示例13: GetDescriptografiaSimetrica
/// <summary>
/// A chave deve possuir 16 com caracteres "1234567890123456"
/// </summary>
/// <param name="str"></param>
/// <param name="key"></param>
/// <returns></returns>
public static string GetDescriptografiaSimetrica(this string str, string key)
{
using (TripleDESCryptoServiceProvider provider = new TripleDESCryptoServiceProvider())
{
provider.Mode = CipherMode.CFB;
provider.Padding = PaddingMode.PKCS7;
byte[] inputEquivalent = Convert.FromBase64String(str);
MemoryStream msDecrypt = new MemoryStream();
CryptoStream csDecrypt = new CryptoStream(msDecrypt, provider.CreateDecryptor(Encoding.UTF8.GetBytes(key), new byte[] { 138, 154, 251, 188, 64, 108, 167, 121 }), CryptoStreamMode.Write);
csDecrypt.Write(inputEquivalent, 0, inputEquivalent.Length);
csDecrypt.FlushFinalBlock();
csDecrypt.Close();
str = Encoding.UTF8.GetString(msDecrypt.ToArray());
msDecrypt.Close();
}
return str;
}
示例14: DESDeCode
public static string DESDeCode(string pToDecrypt, string sKey)
{
// HttpContext.Current.Response.Write(pToDecrypt + "<br>" + sKey);
// HttpContext.Current.Response.End();
DESCryptoServiceProvider des = new DESCryptoServiceProvider();
byte[] inputByteArray = new byte[pToDecrypt.Length / 2];
for (int x = 0; x < pToDecrypt.Length / 2; x++)
{
int i = (Convert.ToInt32(pToDecrypt.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();
StringBuilder ret = new StringBuilder();
// return HttpContext.Current.Server.UrlDecode(System.Text.Encoding.Default.GetString(ms.ToArray()));
return System.Text.Encoding.Default.GetString(ms.ToArray());
}
示例15: Decrypt
public static string Decrypt(string text)
{
try
{
key = Encoding.UTF8.GetBytes(stringKey.Substring(0, 8));
DESCryptoServiceProvider des = new DESCryptoServiceProvider();
Byte[] byteArray = Convert.FromBase64String(text);
MemoryStream memoryStream = new MemoryStream();
CryptoStream cryptoStream = new CryptoStream(memoryStream,
des.CreateDecryptor(key, IV), CryptoStreamMode.Write);
cryptoStream.Write(byteArray, 0, byteArray.Length);
cryptoStream.FlushFinalBlock();
return Encoding.UTF8.GetString(memoryStream.ToArray());
}
catch (Exception ex)
{
// Handle Exception Here
}
return string.Empty;
}