本文整理汇总了C#中CryptoStream.Read方法的典型用法代码示例。如果您正苦于以下问题:C# CryptoStream.Read方法的具体用法?C# CryptoStream.Read怎么用?C# CryptoStream.Read使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类CryptoStream
的用法示例。
在下文中一共展示了CryptoStream.Read方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: Decrypt
protected string Decrypt(string cipherText, string passPhrase, string saltValue, string hashAlgorithm, int passwordIterations, string initVector, int keySize)
{
byte[] initVectorBytes = Encoding.ASCII.GetBytes(initVector);
byte[] saltValueBytes = Encoding.ASCII.GetBytes(saltValue);
byte[] cipherTextBytes = Convert.FromBase64String(cipherText);
PasswordDeriveBytes password = new PasswordDeriveBytes(passPhrase, saltValueBytes, hashAlgorithm, passwordIterations);
byte[] keyBytes = password.GetBytes(keySize / 8);
RijndaelManaged symmetricKey = new RijndaelManaged();
symmetricKey.Mode = CipherMode.CBC;
ICryptoTransform decryptor = symmetricKey.CreateDecryptor(keyBytes, initVectorBytes);
MemoryStream memoryStream = new MemoryStream(cipherTextBytes);
CryptoStream cryptoStream = new CryptoStream(memoryStream, decryptor, CryptoStreamMode.Read);
byte[] plainTextBytes = new byte[cipherTextBytes.Length];
int decryptedByteCount = cryptoStream.Read(plainTextBytes, 0, plainTextBytes.Length);
memoryStream.Close();
cryptoStream.Close();
string plainText = Encoding.UTF8.GetString(plainTextBytes, 0, decryptedByteCount);
StringWriter writer = new StringWriter();
HttpContext.Current.Server.UrlDecode(plainText, writer);
return writer.ToString();
}
示例2: DecryptRijndael
public static string DecryptRijndael(string encryptedString)
{
byte[] encrypted;
byte[] fromEncrypted;
UTF8Encoding utf8Converter = new UTF8Encoding();
encrypted = Convert.FromBase64String(encryptedString);
RijndaelManaged myRijndael = new RijndaelManaged();
ICryptoTransform decryptor = myRijndael.CreateDecryptor(Key, IV);
MemoryStream ms = new MemoryStream(encrypted);
CryptoStream cs = new CryptoStream(ms, decryptor, CryptoStreamMode.Read);
fromEncrypted = new byte[encrypted.Length];
cs.Read(fromEncrypted, 0, fromEncrypted.Length);
string decryptedString = utf8Converter.GetString(fromEncrypted);
int indexNull = decryptedString.IndexOf("\0");
if (indexNull > 0)
{
decryptedString = decryptedString.Substring(0, indexNull);
}
return decryptedString;
}
示例3: DecryptString
protected 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);
}
}
示例4: DecryptStream
/// <summary>
/// Decrypt byte array
/// </summary>
/// <param name="dataStream">encrypted data array</param>
/// <param name="password">password</param>
/// <returns>unencrypted data array</returns>
private static byte[] DecryptStream(byte[] dataStream, string password)
{
SqlPipe pipe = SqlContext.Pipe;
//the decrypter
RijndaelManaged cryptic = new RijndaelManaged
{
Key = Encoding.ASCII.GetBytes(password),
IV = Encoding.ASCII.GetBytes("1qazxsw23edcvfr4"),
Padding = PaddingMode.ISO10126,
};
//Get a decryptor that uses the same key and IV as the encryptor used.
ICryptoTransform decryptor = cryptic.CreateDecryptor();
//Now decrypt encrypted data stream
MemoryStream msDecrypt = new MemoryStream(dataStream);
CryptoStream csDecrypt = new CryptoStream(msDecrypt, decryptor, CryptoStreamMode.Read);
byte[] fromEncrypt = new byte[dataStream.Length];
//Read the data out of the crypto stream.
try
{
csDecrypt.Read(fromEncrypt, 0, fromEncrypt.Length);
}
catch (Exception e)
{
pipe.Send("Failed to decrypt data");
pipe.Send(e.Message);
throw;
}
return fromEncrypt;
}
示例5: 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);
}
示例6: Decrypt
public static string Decrypt(string cipherText, string passPhrase)
{
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))
{
try
{
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);
}
}
catch
{
return "ERROR";
}
}
}
}
}
}
示例7: EncryptData
private static void EncryptData(String inName, String outName, byte[] desKey, byte[] desIV)
{
FileStream fs = new FileStream(inName, FileMode.Open, FileAccess.Read);
// Create an instance of the Rijndael cipher
SymmetricAlgorithm aes = Rijndael.Create();
// set the key to be the derivedKey computed above
aes.Key = desKey;
// set the IV to be all zeros
aes.IV = desIV; // arrays are zero-initialized
// now wrap an encryption transform around the filestream
CryptoStream stream1 = new CryptoStream(fs, aes.CreateEncryptor(), CryptoStreamMode.Read);
// The result of reading from stream1 is ciphertext, but we want it
// base64-encoded, so wrap another transform around it
CryptoStream stream2 = new CryptoStream(stream1, new ToBase64Transform(), CryptoStreamMode.Read);
FileStream fsout = new FileStream(outName, FileMode.OpenOrCreate);
byte[] buffer = new byte[1024];
int bytesRead;
do {
bytesRead = stream2.Read(buffer,0,1024);
fsout.Write(buffer,0,bytesRead);
} while (bytesRead > 0);
fsout.Flush();
fsout.Close();
}
示例8: Decrypt
/// <summary>
/// Decrypts a Base64 encoded string previously generated with a specific crypt class, returns a string
/// </summary>
/// <param name="cipherText">A base64 encoded string containing encryption information</param>
/// <param name="passPhrase">The passphrase used to encrypt the inputed text</param>
/// <returns></returns>
public string Decrypt(string cipherText, string passPhrase)
{
try
{
var ciphertextS = DecodeFrom64(cipherText);
var ciphersplit = Regex.Split(ciphertextS, "-");
var passsalt = Convert.FromBase64String(ciphersplit[1]);
var initVectorBytes = Convert.FromBase64String(ciphersplit[0]);
var cipherTextBytes = Convert.FromBase64String(ciphersplit[2]);
var password = new PasswordDeriveBytes(passPhrase, passsalt, "SHA512", 100);
var keyBytes = password.GetBytes(256/8);
var symmetricKey = new RijndaelManaged();
symmetricKey.Mode = CipherMode.CBC;
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];
var decryptedByteCount = cryptoStream.Read(plainTextBytes, 0, plainTextBytes.Length);
memoryStream.Close();
cryptoStream.Close();
return Encoding.UTF8.GetString(plainTextBytes, 0, decryptedByteCount);
}
catch (Exception m)
{
return "error";
}
}
示例9: Decrypt
/// <summary>
/// Decrypts a previously encrypted string.
/// </summary>
/// <param name="inputText">The encrypted string to decrypt.</param>
/// <returns>A decrypted string.</returns>
public static string Decrypt(string inputText)
{
string decrypted = null;
try
{
RijndaelManaged rijndaelCipher = new RijndaelManaged();
byte[] encryptedData = Convert.FromBase64String(inputText);
PasswordDeriveBytes secretKey = new PasswordDeriveBytes(ENCRYPTION_KEY, SALT);
using (ICryptoTransform decryptor = rijndaelCipher.CreateDecryptor(secretKey.GetBytes(32), secretKey.GetBytes(16)))
{
using (MemoryStream memoryStream = new MemoryStream(encryptedData))
{
using (CryptoStream cryptoStream = new CryptoStream(memoryStream, decryptor, CryptoStreamMode.Read))
{
byte[] plainText = new byte[encryptedData.Length];
int decryptedCount = cryptoStream.Read(plainText, 0, plainText.Length);
decrypted = Encoding.Unicode.GetString(plainText, 0, decryptedCount);
}
}
}
}
catch (Exception)
{
}
return decrypted;
}
示例10: Decrypt
static byte[] Decrypt(byte[] asm)
{
byte[] dat;
byte[] iv;
byte[] key;
using (BinaryReader rdr = new BinaryReader(new MemoryStream(asm)))
{
dat = rdr.ReadBytes(rdr.ReadInt32());
iv = rdr.ReadBytes(rdr.ReadInt32());
key = rdr.ReadBytes(rdr.ReadInt32());
}
int key0 = Mutation.Key0I;
for (int j = 0; j < key.Length; j += 4)
{
key[j + 0] ^= (byte)((key0 & 0x000000ff) >> 0);
key[j + 1] ^= (byte)((key0 & 0x0000ff00) >> 8);
key[j + 2] ^= (byte)((key0 & 0x00ff0000) >> 16);
key[j + 3] ^= (byte)((key0 & 0xff000000) >> 24);
}
RijndaelManaged rijn = new RijndaelManaged();
using (var s = new CryptoStream(new MemoryStream(dat), rijn.CreateDecryptor(key, iv), CryptoStreamMode.Read))
{
byte[] l = new byte[4];
s.Read(l, 0, 4);
uint len = BitConverter.ToUInt32(l, 0);
LzmaDecoder decoder = new LzmaDecoder();
byte[] prop = new byte[5];
s.Read(prop, 0, 5);
decoder.SetDecoderProperties(prop);
long outSize = 0;
for (int i = 0; i < 8; i++)
{
int v = s.ReadByte();
if (v < 0)
throw (new Exception("Can't Read 1"));
outSize |= ((long)(byte)v) << (8 * i);
}
byte[] ret = new byte[outSize];
long compressedSize = len - 13;
decoder.Code(s, new MemoryStream(ret, true), compressedSize, outSize);
return ret;
}
}
示例11: Test
static Boolean Test(CipherMode md)
{
Byte[] PlainText = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15};
Byte[] Key = {1, 1, 1, 1, 1, 1, 1, 1,2,2,2,2,2,2,2,2};
Byte[] IV = {100, 101, 102, 103, 104, 105, 106, 107, 108, 109, 110, 111, 112, 113, 114, 115};
Console.WriteLine("Encrypting the following bytes:");
PrintByteArray(PlainText);
RijndaelManaged des = new RijndaelManaged();
des.Mode = md;
// des.FeedbackSize = 0;
// des.Padding = PaddingMode.PKCS7;
Console.WriteLine("DES default key size = " + des.KeySize);
ICryptoTransform sse = des.CreateEncryptor(Key, IV);
Console.WriteLine("SSE mode = " + des.Mode);
MemoryStream ms = new MemoryStream();
CryptoStream cs = new CryptoStream(ms, sse, CryptoStreamMode.Write);
cs.Write(PlainText,0,PlainText.Length);
cs.FlushFinalBlock();
byte[] CipherText = ms.ToArray();
cs.Close();
Console.WriteLine("Cyphertext:");
PrintByteArray(CipherText);
Console.WriteLine("Decrypting...");
// RijndaelManaged des = new RijndaelManaged();
// des.Mode = CipherMode.ECB;
// des.FeedbackSize = 0;
ICryptoTransform ssd = des.CreateDecryptor(Key, IV);
Console.WriteLine("SSD mode = " + des.Mode);
cs = new CryptoStream(new MemoryStream(CipherText), ssd, CryptoStreamMode.Read);
byte[] NewPlainText = new byte[PlainText.Length];
cs.Read(NewPlainText,0,PlainText.Length);
PrintByteArray(NewPlainText);
if (!Compare(PlainText, NewPlainText)) {
Console.WriteLine("ERROR: roundtrip failed");
return false;
}
return true;
}
示例12: Test
static Boolean Test()
{
Byte[] PlainText = {0, 1, 2, 3, 4, 5, 6, 7}; //, 8, 9, 10, 11, 12, 13, 14, 15};
Byte[] Key = {1, 1, 1, 1, 1, 1, 1, 2};
Byte[] IV = {1, 1, 1, 1, 1, 1, 1, 1};
Console.WriteLine("Encrypting the following bytes:");
PrintByteArray(PlainText);
DESCryptoServiceProvider des = new DESCryptoServiceProvider();
des.Mode = CipherMode.ECB;
// des.FeedbackSize = 0;
des.Padding = PaddingMode.None;
Console.WriteLine("DES default key size = " + des.KeySize);
ICryptoTransform sse = des.CreateEncryptor(Key, IV);
Console.WriteLine("SSE mode = " + des.Mode);
MemoryStream ms = new MemoryStream();
CryptoStream cs = new CryptoStream(ms, sse, CryptoStreamMode.Write);
cs.Write(PlainText,0,PlainText.Length);
cs.FlushFinalBlock();
byte[] CipherText = ms.ToArray();
cs.Close();
Console.WriteLine("Cyphertext:");
PrintByteArray(CipherText);
Console.WriteLine("Decrypting...");
// DESCryptoServiceProvider des = new DESCryptoServiceProvider();
// des.Mode = CipherMode.ECB;
// des.FeedbackSize = 0;
ICryptoTransform ssd = des.CreateDecryptor(Key, IV);
Console.WriteLine("SSD mode = " + des.Mode);
cs = new CryptoStream(new MemoryStream(CipherText), ssd, CryptoStreamMode.Read);
byte[] NewPlainText = new byte[8];
cs.Read(NewPlainText,0,8);
PrintByteArray(NewPlainText);
if (!Compare(PlainText, NewPlainText)) {
Console.WriteLine("ERROR: roundtrip failed");
return false;
}
return true;
}
示例13: Decrypt
public static string Decrypt(string encryptedText)
{
byte[] cipherTextBytes = Convert.FromBase64String(encryptedText);
byte[] keyBytes = new Rfc2898DeriveBytes(PasswordHash, Encoding.ASCII.GetBytes(SaltKey)).GetBytes(256 / 8);
var symmetricKey = new RijndaelManaged() { Mode = CipherMode.CBC, Padding = PaddingMode.None };
var decryptor = symmetricKey.CreateDecryptor(keyBytes, Encoding.ASCII.GetBytes(VIKey));
var memoryStream = new MemoryStream(cipherTextBytes);
var cryptoStream = new CryptoStream(memoryStream, decryptor, CryptoStreamMode.Read);
byte[] plainTextBytes = new byte[cipherTextBytes.Length];
int decryptedByteCount = cryptoStream.Read(plainTextBytes, 0, plainTextBytes.Length);
memoryStream.Close();
cryptoStream.Close();
return Encoding.UTF8.GetString(plainTextBytes, 0, decryptedByteCount).TrimEnd("\0".ToCharArray());
}
示例14: Decrypt
public string Decrypt(string cipherText, string passPhrase)
{
byte[] initVectorBytes = Encoding.ASCII.GetBytes(initVector);
byte[] cipherTextBytes = Convert.FromBase64String(cipherText);
PasswordDeriveBytes password = new PasswordDeriveBytes(passPhrase, null);
byte[] keyBytes = password.GetBytes(keysize / 8);
RijndaelManaged symmetricKey = new RijndaelManaged();
symmetricKey.Mode = CipherMode.CBC;
ICryptoTransform decryptor = symmetricKey.CreateDecryptor(keyBytes, initVectorBytes);
MemoryStream memoryStream = new MemoryStream(cipherTextBytes);
CryptoStream cryptoStream = new CryptoStream(memoryStream, decryptor, CryptoStreamMode.Read);
byte[] plainTextBytes = new byte[cipherTextBytes.Length];
int decryptedByteCount = cryptoStream.Read(plainTextBytes, 0, plainTextBytes.Length);
memoryStream.Close();
cryptoStream.Close();
return Encoding.UTF8.GetString(plainTextBytes, 0, decryptedByteCount);
}
示例15: AESDecrypt
/// <summary>
/// AES解密
/// </summary>
/// <param name="cipherText">密文字符串</param>
/// <returns>返回解密后的明文字符串</returns>
public static byte[] AESDecrypt(byte[] showText)
{
byte[] cipherText = showText;
SymmetricAlgorithm des = Rijndael.Create();
des.Key = Encoding.UTF8.GetBytes(Key);
des.IV = _key1;
byte[] decryptBytes = new byte[cipherText.Length];
using (MemoryStream ms = new MemoryStream(cipherText))
{
using (CryptoStream cs = new CryptoStream(ms, des.CreateDecryptor(), CryptoStreamMode.Read))
{
cs.Read(decryptBytes, 0, decryptBytes.Length);
cs.Close();
ms.Close();
}
}
return decryptBytes; ///将字符串后尾的'\0'去掉
}