Created
March 10, 2021 17:50
Star
You must be signed in to star a gist
C# Encrypt / Decrypt File with XOR
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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