Created
March 10, 2021 17:50
-
-
Save nakov/1d39c4513cff83b8a735d7dc883dfe18 to your computer and use it in GitHub Desktop.
C# Encrypt / Decrypt File with XOR
This file contains hidden or 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