Last active
February 8, 2017 13:35
-
-
Save DorukUlucay/0144758dda61df662f26 to your computer and use it in GitHub Desktop.
cipher decipher sample
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
public string Cipher(string RawData, int Key) | |
{ | |
StringBuilder CipheredText = new StringBuilder(); | |
byte[] RawBytes = UnicodeEncoding.UTF8.GetBytes(RawData); | |
int keyLen = Key.ToString().Length; | |
int keyPos = 0; | |
foreach (byte rawByte in RawBytes) | |
{ | |
keyPos = keyPos == keyLen ? 0 : keyPos; | |
int cipheredByte = rawByte + int.Parse(Key.ToString()[keyPos++].ToString()); | |
CipheredText.Append(cipheredByte.ToString()); | |
CipheredText.Append("-"); | |
} | |
return CipheredText.ToString(); //ciphered value | |
} | |
public string DeCipher(string EncryptedData, int Key) | |
{ | |
char[] seperator = { | |
'-' | |
}; | |
string[] splitted = EncryptedData.Split(seperator, StringSplitOptions.RemoveEmptyEntries); | |
List<byte> BYTES = new List<byte>(); | |
int keyLen = Key.ToString().Length; | |
int keyPos = 0; | |
foreach (string s in splitted) | |
{ | |
keyPos = keyPos == keyLen ? 0 : keyPos; | |
int x = int.Parse(s) - int.Parse(Key.ToString()[keyPos++].ToString()); | |
BYTES.Add(byte.Parse(x.ToString())); | |
} | |
byte[] ENDBYTES = new byte[BYTES.Count]; | |
int counter = 0; | |
foreach (byte item in BYTES) | |
{ | |
ENDBYTES[counter++] = item; | |
} | |
return UTF8Encoding.UTF8.GetString(ENDBYTES); // deciphered data | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment