本文整理汇总了C#中System.Security.Cryptography.CryptoStream.Read方法的典型用法代码示例。如果您正苦于以下问题:C# CryptoStream.Read方法的具体用法?C# CryptoStream.Read怎么用?C# CryptoStream.Read使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类System.Security.Cryptography.CryptoStream
的用法示例。
在下文中一共展示了CryptoStream.Read方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: DecryptString
protected static string DecryptString(string InputText, string Password)
{
try
{
RijndaelManaged RijndaelCipher = new RijndaelManaged();
byte[] EncryptedData = Convert.FromBase64String(InputText);
byte[] Salt = Encoding.ASCII.GetBytes(Password.Length.ToString());
PasswordDeriveBytes SecretKey = new PasswordDeriveBytes(Password, Salt);
// Create a decryptor from the existing SecretKey bytes.
ICryptoTransform Decryptor = RijndaelCipher.CreateDecryptor(SecretKey.GetBytes(16), SecretKey.GetBytes(16));
MemoryStream memoryStream = new MemoryStream(EncryptedData);
// Create a CryptoStream. (always use Read mode for decryption).
CryptoStream cryptoStream = new CryptoStream(memoryStream, Decryptor, CryptoStreamMode.Read);
// Since at this point we don't know what the size of decrypted data
// will be, allocate the buffer long enough to hold EncryptedData;
// DecryptedData is never longer than EncryptedData.
byte[] PlainText = new byte[EncryptedData.Length];
// Start decrypting.
int DecryptedCount = cryptoStream.Read(PlainText, 0, PlainText.Length);
memoryStream.Close();
cryptoStream.Close();
// Convert decrypted data into a string.
string DecryptedData = Encoding.Unicode.GetString(PlainText, 0, DecryptedCount);
// Return decrypted string.
return DecryptedData;
}
catch (Exception exception)
{
return (exception.Message);
}
}
示例2: AESDecryptWithoutVector
/// <summary>
/// AES解密(无向量)
/// </summary>
/// <param name="encryptedBytes">被加密的明文</param>
/// <param name="key">密钥</param>
/// <returns>明文</returns>
public static string AESDecryptWithoutVector(String Data, String Key)
{
Byte[] encryptedBytes = Convert.FromBase64String(Data);
Byte[] bKey = new Byte[32];
Array.Copy(Encoding.UTF8.GetBytes(Key.PadRight(bKey.Length)), bKey, bKey.Length);
MemoryStream mStream = new MemoryStream(encryptedBytes);
//mStream.Write( encryptedBytes, 0, encryptedBytes.Length );
//mStream.Seek( 0, SeekOrigin.Begin );
RijndaelManaged aes = new RijndaelManaged();
aes.Mode = CipherMode.ECB;
aes.Padding = PaddingMode.PKCS7;
aes.KeySize = 128;
aes.Key = bKey;
//aes.IV = _iV;
CryptoStream cryptoStream = new CryptoStream(mStream, aes.CreateDecryptor(), CryptoStreamMode.Read);
try
{
byte[] tmp = new byte[encryptedBytes.Length + 32];
int len = cryptoStream.Read(tmp, 0, encryptedBytes.Length + 32);
byte[] ret = new byte[len];
Array.Copy(tmp, 0, ret, 0, len);
return Encoding.UTF8.GetString(ret);
}
finally
{
cryptoStream.Close();
mStream.Close();
aes.Clear();
}
}
示例3: Decrypt
public string Decrypt(string strEncryptedText)
{
if (strEncryptedText == null || strEncryptedText.Equals(""))
return strEncryptedText;
string strDecryptedText = null;
RijndaelManaged rijndael = new RijndaelManaged();
ICryptoTransform decryptor = rijndael.CreateDecryptor(Key, IV);
byte[] byteEncryptedText = Convert.FromBase64String(strEncryptedText);
MemoryStream memStream = new MemoryStream(byteEncryptedText);
CryptoStream decryptStream = null;
try
{
decryptStream = new CryptoStream(memStream, decryptor, CryptoStreamMode.Read);
byte[] byteDecryptedText = new byte[byteEncryptedText.Length];
int decryptedByteCount = decryptStream.Read(byteDecryptedText, 0, byteDecryptedText.Length);
strDecryptedText = Encoding.UTF8.GetString(byteDecryptedText, 0, decryptedByteCount);
}
finally
{
if (rijndael != null) rijndael.Clear();
if (decryptor != null) decryptor.Dispose();
if (memStream != null) memStream.Close();
if (decryptStream != null) decryptStream.Close();
}
if (UseSalt)
strDecryptedText = strDecryptedText.Substring(8);
return strDecryptedText;
}
示例4: Decrypt128Byte
/// <summary>
/// Decrypts the specified data using a 128-bit cipher. The key can be any length.
/// </summary>
/// <param name="Data">The data to be decrypted.</param>
/// <param name="Key">The key used to decrypt the data.</param>
/// <returns>A string containing the decoded data.</returns>
public static byte[] Decrypt128Byte(byte[] Data, byte[] Key)
{
RijndaelManaged AES = null;
var MS = new MemoryStream(Data);
CryptoStream CS = null;
StreamReader DS = null;
try
{
//Get the IV and length corrected Key.
KeyData KeyData = GenerateKeyIV128(Key);
//Create the AES crytpograhy object.
AES = new RijndaelManaged { BlockSize = 128, KeySize = 128, Key = KeyData.Key, IV = KeyData.IV, Mode = CipherMode.CBC, Padding = PaddingMode.PKCS7 };
CS = new CryptoStream(MS, AES.CreateDecryptor(), CryptoStreamMode.Read);
DS = new StreamReader(CS);
var D = new byte[CS.Length];
CS.Read(D, 0, (int)CS.Length - 1);
return D;
}
finally
{
if (AES != null) AES.Clear();
MS.Dispose();
if (CS != null) CS.Dispose();
if (DS != null) DS.Dispose();
}
}
示例5: Decrypt
public static string Decrypt(string cipherText, string passPhrase)
{
// Get the complete stream of bytes that represent:
// [32 bytes of Salt] + [32 bytes of IV] + [n bytes of CipherText]
var cipherTextBytesWithSaltAndIv = Convert.FromBase64String(cipherText);
// Get the saltbytes by extracting the first 32 bytes from the supplied cipherText bytes.
var saltStringBytes = cipherTextBytesWithSaltAndIv.Take(Keysize / 8).ToArray();
// Get the IV bytes by extracting the next 32 bytes from the supplied cipherText bytes.
var ivStringBytes = cipherTextBytesWithSaltAndIv.Skip(Keysize / 8).Take(Keysize / 8).ToArray();
// Get the actual cipher text bytes by removing the first 64 bytes from the cipherText string.
var cipherTextBytes = cipherTextBytesWithSaltAndIv.Skip((Keysize / 8) * 2).Take(cipherTextBytesWithSaltAndIv.Length - ((Keysize / 8) * 2)).ToArray();
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 decryptor = symmetricKey.CreateDecryptor(keyBytes, ivStringBytes))
{
using (var memoryStream = new MemoryStream(cipherTextBytes))
{
using (var cryptoStream = new CryptoStream(memoryStream, decryptor, CryptoStreamMode.Read))
{
var plainTextBytes = new byte[cipherTextBytes.Length];
var decryptedByteCount = cryptoStream.Read(plainTextBytes, 0, plainTextBytes.Length);
memoryStream.Close();
cryptoStream.Close();
return Encoding.UTF8.GetString(plainTextBytes, 0, decryptedByteCount);
}
}
}
}
}
示例6: DecryptManaged
private byte[] DecryptManaged(byte[] Key, byte[] Vector, byte[] Data, PaddingMode Padding = PaddingMode.Zeros)
{
byte[] decryptedBytes;
int count = 0;
using (MemoryStream stream = new MemoryStream(Data))
{
using (RijndaelManaged cipher = new RijndaelManaged())
{
cipher.Mode = CipherMode.CBC;
cipher.Padding = Padding;
cipher.KeySize = Key.Length * 8;
cipher.BlockSize = Vector.Length * 8;
using (ICryptoTransform decryptor = cipher.CreateDecryptor(Key, Vector))
{
using (CryptoStream reader = new CryptoStream(stream, decryptor, CryptoStreamMode.Read))
{
decryptedBytes = new byte[stream.Length];
count = reader.Read(decryptedBytes, 0, decryptedBytes.Length);
}
}
cipher.Clear();
}
}
return decryptedBytes;
}
示例7: DecryptString
public static string DecryptString(String cipherText, string Key)
{
byte[] tmpCipherText = Convert.FromBase64String(cipherText);
byte[] tmpKey = GenerateAlgotihmInputs(Key);
using (RijndaelManaged alg = new RijndaelManaged())
{
alg.Key = tmpKey;
alg.IV = tmpKey;
// Create a decrytor to perform the stream transform.
ICryptoTransform decryptor = alg.CreateDecryptor(alg.Key, alg.IV);
// Create the streams used for decryption.
using (MemoryStream msDecrypt = new MemoryStream(tmpCipherText))
{
using (CryptoStream csDecrypt = new CryptoStream(msDecrypt, decryptor, CryptoStreamMode.Read))
{
// Place les données déchiffrées dans un tableau d'octet
byte[] plainTextData = new byte[tmpCipherText.Length];
int decryptedByteCount = csDecrypt.Read(plainTextData, 0, plainTextData.Length);
return Encoding.UTF8.GetString(plainTextData, 0, decryptedByteCount);
}
}
}
}
示例8: Desencriptar
public static byte[] Desencriptar(byte[] mensajeEncriptado,
SymmetricAlgorithm algoritmo)
{
int numeroBytesDesencriptados = 0;
// La clase SymmetricAlgorithm delega el proceso de desencriptación de datos
// Una instancia de ICryptoTransform transforma texto plano en texto cifrado o vice versa.
// Las siguiente sentencia demuestra como crear transformaciones usando CreateDecryptor.
byte[] mensajeDesencriptado = new
byte[mensajeEncriptado.Length];
// Crear una ICryptoTransform que puede ser usada para desencriptar datos
ICryptoTransform desencriptador =
algoritmo.CreateDecryptor();
// Procedemos a descifrar el mensaje
MemoryStream memoryStream = new
MemoryStream(mensajeEncriptado);
// Creamos el CryptoStream
CryptoStream cryptoStream = new CryptoStream(memoryStream,
desencriptador, CryptoStreamMode.Read);
// Decrypting data and get the count of plain text bytes.
numeroBytesDesencriptados = cryptoStream.Read(mensajeDesencriptado, 0, mensajeDesencriptado.Length);
// Liberamos recursos.
memoryStream.Close();
cryptoStream.Close();
return mensajeDesencriptado;
}
示例9: Unprotect
public byte[] Unprotect(byte[] protectedData)
{
byte[] output = null;
using (AesCryptoServiceProvider aesAlg = new AesCryptoServiceProvider())
{
aesAlg.Key = _key;
aesAlg.IV = _IV;
ICryptoTransform decryptor = aesAlg.CreateDecryptor(aesAlg.Key, aesAlg.IV);
using (MemoryStream msDecrypt = new MemoryStream(protectedData))
{
using (CryptoStream csDecrypt = new CryptoStream(msDecrypt, decryptor, CryptoStreamMode.Read))
{
byte[] buffer = new byte[8];
using (MemoryStream msOutput = new MemoryStream())
{
int read;
while ((read = csDecrypt.Read(buffer, 0, buffer.Length)) > 0)
{
msOutput.Write(buffer, 0, read);
}
output = msOutput.ToArray();
}
}
}
}
return output;
}
示例10: DecryptFile
public static void DecryptFile(string inputFile, string outputFile, string keyIV)
{
byte[] array = new byte[0x20];
byte[] buffer2 = new byte[0x10];
HexStringToByteArray(keyIV, array, 0);
HexStringToByteArray(keyIV, buffer2, 0x40);
FileStream stream = File.Open(inputFile, System.IO.FileMode.Open);
FileStream stream2 = File.Open(outputFile, System.IO.FileMode.CreateNew);
AesManaged managed = new AesManaged {
Key = array,
IV = buffer2
};
CryptoStream stream3 = new CryptoStream(stream, managed.CreateDecryptor(managed.Key, managed.IV), CryptoStreamMode.Read);
try
{
int num;
byte[] buffer = new byte[0xa000];
while ((num = stream3.Read(buffer, 0, buffer.Length)) > 0)
{
stream2.Write(buffer, 0, num);
}
}
finally
{
stream3.Close();
stream.Close();
stream2.Close();
}
}
示例11: Decrypt
public static string Decrypt(byte[] cipherTextBytes, string password, string salt = null,
string initialVector = null)
{
const int keySize = 256;
byte[] initialVectorBytes = string.IsNullOrEmpty(initialVector)
? InitVectorBytes
: Encoding.UTF8.GetBytes(initialVector);
byte[] saltValueBytes = string.IsNullOrEmpty(salt) ? SaltBytes : Encoding.UTF8.GetBytes(salt);
byte[] keyBytes = new Rfc2898DeriveBytes(password, saltValueBytes).GetBytes(keySize/8);
byte[] plainTextBytes = new byte[cipherTextBytes.Length];
using (RijndaelManaged symmetricKey = new RijndaelManaged())
{
symmetricKey.Mode = CipherMode.CBC;
using (ICryptoTransform decryptor = symmetricKey.CreateDecryptor(keyBytes, initialVectorBytes))
{
using (MemoryStream memStream = new MemoryStream(cipherTextBytes))
{
using (CryptoStream cryptoStream = new CryptoStream(memStream, decryptor, CryptoStreamMode.Read)
)
{
int byteCount = cryptoStream.Read(plainTextBytes, 0, plainTextBytes.Length);
return Encoding.UTF8.GetString(plainTextBytes, 0, byteCount);
}
}
}
}
}
示例12: Decrypt
/// <summary>
/// Decrypts specified ciphertext using Rijndael symmetric key algorithm.
/// </summary>
/// <param name="cipherText">
/// Base64-formatted ciphertext value.
/// </param>
/// <param name="passPhrase">
/// Passphrase from which a pseudo-random password will be derived. The
/// derived password will be used to generate the encryption key.
/// Passphrase can be any string. In this example we assume that this
/// passphrase is an ASCII string.
/// </param>
/// <param name="saltValue">
/// Salt value used along with passphrase to generate password. Salt can
/// be any string. In this example we assume that salt is an ASCII string.
/// </param>
/// <param name="passwordIterations">
/// Number of iterations used to generate password. One or two iterations
/// should be enough.
/// </param>
/// <param name="initVector">
/// Initialization vector (or IV). This value is required to encrypt the
/// first block of plaintext data. For RijndaelManaged class IV must be
/// exactly 16 ASCII characters long.
/// </param>
/// <param name="keySize">
/// Size of encryption key in bits. Allowed values are: 128, 192, and 256.
/// Longer keys are more secure than shorter keys.
/// </param>
/// <returns>
/// Decrypted string value.
/// </returns>
/// <remarks>
/// Most of the logic in this function is similar to the Encrypt
/// logic. In order for decryption to work, all parameters of this function
/// - except cipherText value - must match the corresponding parameters of
/// the Encrypt function which was called to generate the
/// ciphertext.
/// </remarks>
public static string Decrypt(
string cipherText,
string passPhrase,
string saltValue,
int passwordIterations,
string initVector,
int keySize)
{
byte[] initVectorBytes = System.Text.Encoding.ASCII.GetBytes(initVector);
byte[] saltValueBytes = System.Text.Encoding.ASCII.GetBytes(saltValue);
byte[] cipherTextBytes = Convert.FromBase64String(cipherText);
var password = new Rfc2898DeriveBytes(passPhrase, saltValueBytes, passwordIterations);
byte[] keyBytes = password.GetBytes(keySize / 8);
var symmetricKey = new RijndaelManaged();
symmetricKey.Mode = CipherMode.CBC;
ICryptoTransform decryptor = symmetricKey.CreateDecryptor(keyBytes, initVectorBytes);
using (var memoryStream = new MemoryStream(cipherTextBytes))
using (var cryptoStream = new CryptoStream(memoryStream, decryptor, CryptoStreamMode.Read)) {
byte[] plainTextBytes = new byte[cipherTextBytes.Length];
int decryptedByteCount = cryptoStream.Read(plainTextBytes, 0, plainTextBytes.Length);
return System.Text.Encoding.UTF8.GetString(plainTextBytes, 0, decryptedByteCount);
}
}
示例13: DecryptToBytes
internal static byte[] DecryptToBytes(byte[] cipher)
{
if ((cipher == null) || (cipher.Length <= 0))
throw new ArgumentException("Cipher Text");
try
{
byte[] plainBytes = null;
using (MemoryStream msDecrypt = new MemoryStream(cipher))
{
byte[] buffer = new byte[1024];
int readBytes = 0;
using (CryptoStream csDecrypt = new CryptoStream(msDecrypt, decryptor, CryptoStreamMode.Read))
{
using (MemoryStream utf8Memory = new MemoryStream())
{
while ((readBytes = csDecrypt.Read(buffer, 0, buffer.Length)) > 0)
{
utf8Memory.Write(buffer, 0, readBytes);
}
plainBytes = utf8Memory.ToArray();
}
}
}
return plainBytes;
}
catch (Exception ex)
{
throw new CryptoException(ex.Message);
}
}
示例14: DecryptData
/// <summary>
/// Decrypts a byte array with a password
/// </summary>
/// <param name="data">Data to decrypt</param>
/// <param name="password">Password to use</param>
/// <param name="paddingMode">Padding mode to use</param>
/// <returns>Decrypted byte array</returns>
/// <exception cref="System.ArgumentNullException">
/// data
/// or
/// password
/// </exception>
/// <exception cref="ArgumentNullException"></exception>
public static byte[] DecryptData(byte[] data, string password, PaddingMode paddingMode)
{
if (data == null || data.Length == 0)
throw new ArgumentNullException("data");
if (password == null)
throw new ArgumentNullException("password");
var pdb = new PasswordDeriveBytes(password, Encoding.UTF8.GetBytes("Salt"));
var rm = new RijndaelManaged { Padding = paddingMode };
ICryptoTransform decryptor = rm.CreateDecryptor(pdb.GetBytes(16), pdb.GetBytes(16));
pdb.Dispose();
using (var msDecrypt = new MemoryStream(data))
using (var csDecrypt = new CryptoStream(msDecrypt, decryptor, CryptoStreamMode.Read))
{
// Decrypted bytes will always be less then encrypted bytes, so length of encrypted data will be big enough for buffer.
byte[] fromEncrypt = new byte[data.Length];
// Read as many bytes as possible.
int read = csDecrypt.Read(fromEncrypt, 0, fromEncrypt.Length);
if (read < fromEncrypt.Length)
{
// Return a byte array of proper size.
byte[] clearBytes = new byte[read];
Buffer.BlockCopy(fromEncrypt, 0, clearBytes, 0, read);
return clearBytes;
}
return fromEncrypt;
}
}
示例15: Decrypt
public static string Decrypt(string cipherText, string passPhrase)
{
if (cipherText != null && cipherText != "")
{
byte[] cipherTextBytes = Convert.FromBase64String(cipherText);
using (PasswordDeriveBytes password = new PasswordDeriveBytes(passPhrase, null))
{
byte[] keyBytes = password.GetBytes(keysize / 8);
using (RijndaelManaged symmetricKey = new RijndaelManaged())
{
symmetricKey.Mode = CipherMode.CBC;
using (ICryptoTransform decryptor = symmetricKey.CreateDecryptor(keyBytes, initVectorBytes))
{
using (MemoryStream memoryStream = new MemoryStream(cipherTextBytes))
{
using (CryptoStream cryptoStream = new CryptoStream(memoryStream, decryptor, CryptoStreamMode.Read))
{
byte[] plainTextBytes = new byte[cipherTextBytes.Length];
int decryptedByteCount = cryptoStream.Read(plainTextBytes, 0, plainTextBytes.Length);
return Encoding.UTF8.GetString(plainTextBytes, 0, decryptedByteCount);
}
}
}
}
}
}
return "";
}