I have a simple windows form. Two buttons: one encrypts the file in any way, saves the file and at the end of the extension appends .crypt; the other one decrypts. An Internet search gave only encryption of string strings. Help in the implementation of the code.

  • Encrypting strings will be enough - ishidex2
  • @Duoxx how can I encrypt for example .jpeg? - Bodybuilder Leshy
  • Each file is binary, draw conclusions - ishidex2
  • Look here - Denis Mochalov

1 answer 1

For the simplest case, you can use the XOR cipher. The same button1 will be used for encryption and decryption. Encryption and decryption function:

byte[] Crypt(byte[] bytes) { for (int i = 0; i < bytes.Length; i++) bytes[i] ^= 1; return bytes; } 

The function to get the new file name:

 string GetNewFileName(string FileName) { return FileName.EndsWith(".crypt") ? FileName.Remove(FileName.LastIndexOf(".crypt")) : FileName + ".crypt"; } 

Example of use in the program:

 private void button1_Click(object sender, EventArgs e) { byte[] MyFile = File.ReadAllBytes(MyFilePath); byte[] NewFile = Crypt(MyFile); string NewFileName = GetNewFileName(MyFileName); File.WriteAllBytes(NewFileName, NewFile); }