Last active
December 25, 2015 20:59
-
-
Save cosmo0920/7039064 to your computer and use it in GitHub Desktop.
XOR Cipher
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; | |
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