Skip to content

Instantly share code, notes, and snippets.

@nakov
Created Mar 10, 2021
Embed
What would you like to do?
C# Encrypt / Decrypt File with XOR
using System.IO;
void EncryptFile(string inputFile, string outputFile)
{
using (var fin = new FileStream(inputFile, FileMode.Open))
using (var fout = new FileStream(outputFile, FileMode.Create))
{
byte[] buffer = new byte[4096];
while (true)
{
int bytesRead = fin.Read(buffer);
if (bytesRead == 0)
break;
EncryptBytes(buffer, bytesRead);
fout.Write(buffer, 0, bytesRead);
}
}
}
const byte Secret = 183;
void EncryptBytes(byte[] buffer, int count)
{
for (int i = 0; i < count; i++)
buffer[i] = (byte)(buffer[i] ^ Secret);
}
EncryptFile("example.png", "example-encrypted.png");
EncryptFile("example-encrypted.png", "example-original.png");
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment