0

I'm trying to encrypt and then decrypt a file when needed, and read the file for use of the program. Still learning cryptography and I looked at several examples and tried this.

I have managed to en- and decrypt string but sadly that did not work in this case.

Cryptostream gives an IV does not match block size error on encrypt. Could not even have tested decrypting but that is most likely off too.

So question is: How can I encrypt, decrypt and read a .txt file?

private void EncryptFile(string resultFile)
    {
        string password = @"test"; 

        UnicodeEncoding UE = new UnicodeEncoding();
        byte[] key = UE.GetBytes(password);

        FileStream fsCrypt = File.Create(resultFile);

        RijndaelManaged rmCrypto = new RijndaelManaged();

        CryptoStream cs = new CryptoStream(fsCrypt, rmCrypto.CreateEncryptor(key, key),CryptoStreamMode.Write);

        StreamWriter sWriter = new StreamWriter(cs);

        sWriter.WriteLine(resultFile);

        sWriter.Close();
        cs.Close();
        fsCrypt.Close();
    }

    private void DecryptFile(string resultFile)
    {

        string password = @"test";

        UnicodeEncoding UE = new UnicodeEncoding();
        byte[] key = UE.GetBytes(password);

        FileStream fsCrypt = new FileStream(resultFile, FileMode.Open);

        RijndaelManaged rmCrypto = new RijndaelManaged();

        CryptoStream cs = new CryptoStream(fsCrypt, rmCrypto.CreateDecryptor(key, key), CryptoStreamMode.Read);

        StreamWriter sWriter = new StreamWriter(cs);

        sWriter.WriteLine(resultFile);

        sWriter.Close();
        cs.Close();
        fsCrypt.Close();
    }
Metzger
  • 93
  • 1
  • 13
  • If your Encryption is intended locally (so no transfer to other computers) you could try the DPAPI of .net: https://learn.microsoft.com/en-us/dotnet/standard/security/how-to-use-data-protection – dba Jul 22 '20 at 10:42
  • Mainly for local but it must be secure in case of transfer. I tried something like but a lot simpler that locked the file or something I forget. But yeah I did transfer it to another machine and got it opened. – Metzger Jul 22 '20 at 10:46
  • IV needs to be 16 bytes. Try following : byte[] key = UE.GetBytes(password); byte[] iv = new byte[16]; Array.Copy(key, iv, (key.Length >= 16)? 16 : key.Length); Then use : rmCrypto.CreateDecryptor(key, iv) – jdweng Jul 22 '20 at 11:24
  • I'll test this out. Thanks. – Metzger Jul 22 '20 at 11:49
  • Where in the code do you try to encrypt a file? – President James K. Polk Jul 22 '20 at 11:50
  • The string resultFile is the file location. – Metzger Jul 22 '20 at 11:57
  • Tried setting the key/IV as suggested did not help sadly =/ – Metzger Jul 24 '20 at 08:38

1 Answers1

1

I would recommend using AesManaged instead of RijndaelManaged, mostly because the former follows a defined standard.

Aes/rijndael have specified sizes for the key and initialization vector. You should not use bytes from a password directly. The correct way is to run the password thru a key derivation function see keyderivationalgorithmprovider, to ensure you have a key of the correct size and of good quality. Or use a randomly generated binary key of the correct size.

The second problem is the Initialization vector, this also need to be of a the correct size and should be random. But the IV is not secret and will usually be attached to the encrypted message, so using the key as the IV will not be secure.

The best option is probably to use the key and IV from the encryption class. See microsofts example for more details.

JonasH
  • 28,608
  • 2
  • 10
  • 23
  • Could not get any of the found examples of AES to do anything I wanted(encrypt, decrypt and read from encrypted .txt file) sadly. One problems was that you can encrypt strings yes but you cannot save them to a text file as they are bytes. If you convert them to string it is not the same and decryption will not work. I tried encrypting each line of the txt file then put it in array string to save them to the txt file once done reading it. – Metzger Jul 24 '20 at 08:47
  • If you want to store encrypted data as text, use [Base64 encoding](https://learn.microsoft.com/en-us/dotnet/api/system.convert.tobase64string?view=netcore-3.1) to convert the byte array to a string and Convert.FromBase64String to convert back to a byte array . – JonasH Jul 24 '20 at 12:12