Answer the question
In order to leave comments, you need to log in
Decrypting a file into a string - how to implement it?
I need to encrypt and decrypt a file with a key, probably using AES.
But I need to decrypt it without writing to disk, i.e. the result of the decryption must be written to a string (or to a TextBox if in WPF).
How would you do it?
For example, there is this code:
public static void aesDecryptFile(string inputFile, string outputFile, string skey)
{
RijndaelManaged aes = new RijndaelManaged();
try
{
PasswordDeriveBytes derivedPassword = new PasswordDeriveBytes(skey, Encoding.ASCII.GetBytes("o1q"), "SHA1", 1);
byte[] keyBytes = derivedPassword.GetBytes(256 / 8);
byte[] initialVectorBytes = Encoding.ASCII.GetBytes("OFRqsfcn*aze01xY");
byte[] key = Encoding.ASCII.GetBytes(skey);
using (FileStream fsCrypt = new FileStream(inputFile, FileMode.Open))
{
using (FileStream fsOut = new FileStream(outputFile, FileMode.Create))
{
using (CryptoStream cs = new CryptoStream(fsCrypt, aes.CreateDecryptor(keyBytes, initialVectorBytes), CryptoStreamMode.Read))
{
int data;
while ((data = cs.ReadByte()) != -1)
{
fsOut.WriteByte((byte)data);
}
aes.Clear();
}
}
}
}
catch (Exception ex)
{
Console.WriteLine(ex.Message);
aes.Clear();
}
}
Answer the question
In order to leave comments, you need to log in
Read the file into a string and decrypt it, there is nothing complicated about it.
Replace FileStreams with MemoryStreams on top of buffers, and do whatever you want with buffers. That's why the flow abstraction exists, it's a sin not to use it.
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question