File encryption / decryption line by line?

I'm new to encryption, and I'm trying to get a step-by-step cipher to work; I need to be able to add encrypted lines to a file when I go during application launch, and not just one large massive encryption - save everything. Although I have a beast with him. Here is my code, shamelessly stolen after several unsuccessful attempts:

class Encryption { private static readonly byte[] SALT = new byte[] { 0x26, 0xdc, 0xff, 0x00, 0xad, 0xed, 0x7a, 0xee, 0xc5, 0xfe, 0x07, 0xaf, 0x4d, 0x08, 0x22, 0x3c }; public static byte[] Encrypt(byte[] plain, string password) { MemoryStream memoryStream; CryptoStream cryptoStream; Rijndael rijndael = Rijndael.Create(); Rfc2898DeriveBytes pdb = new Rfc2898DeriveBytes(password, SALT); rijndael.Key = pdb.GetBytes(32); rijndael.IV = pdb.GetBytes(16); memoryStream = new MemoryStream(); cryptoStream = new CryptoStream(memoryStream, rijndael.CreateEncryptor(), CryptoStreamMode.Write); cryptoStream.Write(plain, 0, plain.Length); cryptoStream.FlushFinalBlock(); cryptoStream.Close(); return memoryStream.ToArray(); } public static byte[] Decrypt(byte[] cipher, string password) { MemoryStream memoryStream; CryptoStream cryptoStream; Rijndael rijndael = Rijndael.Create(); Rfc2898DeriveBytes pdb = new Rfc2898DeriveBytes(password, SALT); rijndael.Key = pdb.GetBytes(32); rijndael.IV = pdb.GetBytes(16); memoryStream = new MemoryStream(); cryptoStream = new CryptoStream(memoryStream, rijndael.CreateDecryptor(), CryptoStreamMode.Write); cryptoStream.Write(cipher, 0, cipher.Length); cryptoStream.FlushFinalBlock(); cryptoStream.Close(); return memoryStream.ToArray(); } } 

And here is a dummy function showing how I am trying to do this:

        private void EncryptFile (string filepath, string outputPath, string password)
         {
             FileInfo fileInfo = new FileInfo (filepath);
             string filename = fileInfo.Name;

             string fullpath = outputPath + "\\" + filename;

             BinaryWriter writer = new BinaryWriter (File.OpenWrite (fullpath), Encoding.ASCII);

             /// Two methods that I've attempted here:
             /// 1. The desired method: encrypt line by line - I assumed I'd be able to generate
             /// multiple blocks of data and decrypt them later.  This isn't working

             // string [] lines = File.ReadAllLines (filepath);

             /// 2. Just read the whole thing and encrypt and write it in one swoop.

             string line = File.ReadAllText (filepath);

             // foreach (string line in lines)
             {
                 byte [] bytes = Encoding.ASCII.GetBytes (line);
                 byte [] encoded = Encryption.Encrypt (bytes, password);

                 writer.Write (encoded);
                 writer.Flush ();
             }

             writer.Close ();
         }



         private void DecryptFile (string filepath, string outputPath, string password)
         {
             FileInfo fileInfo = new FileInfo (filepath);
             string filename = fileInfo.Name;
             string fullpath = outputPath + "\\" + filename;

             StreamWriter writer = new StreamWriter (fullpath, false, Encoding.UTF8);

             byte [] bytes = File.ReadAllBytes (filepath);

             /// Here is the method that working at the moment for decrypting;  just
             /// grab all the data and decrypt it on one swoop.

             byte [] decrypted = Encryption.Decrypt (bytes, password);

             string s = Encoding.ASCII.GetString (decrypted);

             writer.Write (s);
             writer.Flush ();


             /// I've tried a number of things here to decrypt line by line,
             /// none of which work.  This crashes with an issue about the padding
             /// being invalid.  

             / *
             int index = 0;
             int count = 32;

             while (index 

I'm not quite sure what I should do more. I wandered around, obsessing about things and browsing examples on the Internet, but they all seem to encrypt the entire file or simply encrypt some of the data and do nothing with it, except to immediately decrypt it again. How should I process line by line?

+4
source share
1 answer

Instead of programming this for you, I will give you a circuit that you can implement.

If you have encryption on each line, I assume that you also want to decrypt each line. Note that β€œstring” is a rather inconvenient term for a computer. It's just a bunch of characters that end with some kind of line terminator. Characters themselves are encoded using special character-encoding .

In addition, I will make the following assumptions:

  • ciphertext should be presented in lines, which means that bytes should be converted from binary to characters (using encoding );
  • you want one key to be used while remaining secure;
  • you do not need integrity protection or authentication of ciphertext, only confidentiality.

Now the idea is simple:

  • create one key using the key derivation function based on the password;
  • open the text file for reading (using the correct character-encoding );
  • read the string and convert the string to bytes again (e.g. using UTF-8);
  • create an output stream that creates an array of bytes at the bottom;
  • create a random IV and write it to an array of bytes (IV is always the size of one block);
  • create a crypto stream and connect it to the same output stream;
  • encrypt the encoded string;
  • base 64 encodes encrypted bytes and ensures that it remains on the same line;
  • write the encoded, encrypted string to a new text file.

To do the opposite, cancel the process, although IV should be extracted from bytes after decoding base 64, and, of course, the key must be calculated using the same method that is used during encryption.

+4
source

Source: https://habr.com/ru/post/1441124/


All Articles