Vote count:
0
I'm using this function to Decrypt a string (or better a Bytes[])
static string DecryptStringFromBytes(byte[] cipherText, byte[] Key, byte[] IV)
{
// Check arguments.
if (cipherText == null || cipherText.Length <= 0)
throw new ArgumentNullException("cipherText");
if (Key == null || Key.Length <= 0)
throw new ArgumentNullException("Key");
if (IV == null || IV.Length <= 0)
throw new ArgumentNullException("Key");
// Declare the string used to hold
// the decrypted text.
string plaintext = null;
// Create an Rijndael object
// with the specified key and IV.
using (Rijndael rijAlg = Rijndael.Create())
{
rijAlg.Key = Key;
rijAlg.IV = IV;
// Create a decrytor to perform the stream transform.
ICryptoTransform decryptor = rijAlg.CreateDecryptor(rijAlg.Key, rijAlg.IV);
// Create the streams used for decryption.
using (MemoryStream msDecrypt = new MemoryStream(cipherText))
{
using (CryptoStream csDecrypt = new CryptoStream(msDecrypt, decryptor, CryptoStreamMode.Read))
{
using (StreamReader srDecrypt = new StreamReader(csDecrypt))
{
// Read the decrypted bytes from the decrypting stream
// and place them in a string.
plaintext = srDecrypt.ReadToEnd();
}
}
}
}
return plaintext;
}
And i call it with this code:
private void button3_Click(object sender, EventArgs e)
{
using (Rijndael myRijndael = Rijndael.Create())
{
using (MemoryStream msEncrypted = new MemoryStream())
using (FileStream file = new FileStream("file.bin", FileMode.Open, FileAccess.Read))
{
byte[] bytes = new byte[file.Length];
file.Read(bytes, 0, (int)file.Length);
msEncrypted.Write(bytes, 0, (int)file.Length);
string roundtrip = DecryptStringFromBytes(bytes, myRijndael.Key, myRijndael.IV);
textBox2.Text = roundtrip;
}
}
}
But it throw an exeption: plaintext = srDecrypt.ReadToEnd();
An unhandled exception of type 'System.Security.Cryptography.CryptographicException' occurred in mscorlib.dll
asked 34 secs ago
Read a file and decrypt it to memory stream object
Aucun commentaire:
Enregistrer un commentaire