How to encrypt a file using c#?

Asked

Viewed 536 times

1

I need to create a function to encrypt files using the Caeser method without using the ready-made functions of the C#library. My code does not work for large files the computer hangs:

public static void cesar(string origem, string destino, int chave)
    {
        int cifra;
        byte[] textopuroEmBinario = File.ReadAllBytes(origem);
        byte[] textocriptografadoEmBinario = new byte[textopuroEmBinario.Length];
        BinaryWriter bw = new BinaryWriter(File.OpenWrite(destino));
        int i = 0;

            foreach (byte b in textopuroEmBinario)
            {
                    if (b >= 97 && b <= 122){
                    cifra = b + chave; 
                    if (cifra > 122)
                    {
                        cifra = cifra - 26;
                    }
                    textocriptografadoEmBinario[i++] = (byte)cifra;

                }
                else
                {
                    textocriptografadoEmBinario[i++] = b;
                }

            }

            bw.Write(textocriptografadoEmBinario);
            bw.Dispose();
        }
  • I don’t understand, you’re trying to create a Caesar cipher using binary?

  • I am trying to encrypt any type of file using Caesar’s cipher. The program works but only for small files. When colo large files the program locks the computer probably due to vector size.

  • What size file causes your program to lock?

  • I don’t know the file size, but it’s good to be aware of the fact that the framework limits a program to the use of 2GB of memory. I recommend that instead of trying to write everything in memory use a buffer with default size and when it is full burn disk.

No answers

Browser other questions tagged

You are not signed in. Login or sign up in order to post.