Skip to content

Instantly share code, notes, and snippets.

@cosmo0920
Last active December 25, 2015 20:59
Show Gist options
  • Save cosmo0920/7039064 to your computer and use it in GitHub Desktop.
Save cosmo0920/7039064 to your computer and use it in GitHub Desktop.
XOR Cipher
using System;
using System.Text;
class program
{
static void Main(){
string key = (string)"\xde\xad\xbe\xef\01";
string str = "hogehoge", base64str, cryptedstr, decryptedstr;
byte[] bs;
cryptedstr = XORCrypt(str,key);
bs = Encoding.UTF8.GetBytes(cryptedstr);
base64str = Convert.ToBase64String(bs);
Console.WriteLine("crypted: {0}", cryptedstr);
Console.WriteLine("base64: {0}", base64str);
decryptedstr = XORDecrypt(cryptedstr,key);
Console.WriteLine("raw: {0}", decryptedstr);
}
public static string XORCrypt(string toCrypt, string key)
{
string encrepted = "";
int j = 0;
for (int i = 0; i < toCrypt.Length; i++)
{
if(j==key.Length)
j=0;
encrepted += (Char)(toCrypt[i] ^ key[j++]);
}
return encrepted;
}
public static string XORDecrypt(string Encrypted, string key)
{
string decrypted = "";
int j = 0;
for (int i = 0; i < Encrypted.Length; i++)
{
if (j == key.Length)
j = 0;
decrypted += (Char)(Encrypted[i] ^ key[j++]);
}
return decrypted;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment