holy_spirit
New Member
- Reaction score
- 25
XOR Crypt
Today I faced a new way of encryption
was I applying for a new job, there was a set of questions I have to solve
between them, there was a crypting question, and I thought it could be solve be using a basic way of adding some integer values to the ASCII of current char for specific text (string)
after submitting my answers, I met the supervisor who told my that I failed with that question (crypting one) and just told me, why you did not use XOR !
some working and I found what XOR encrypt is
0 XOR 0 = 0
0 XOR 1 = 1
1 XOR 1 = 0
1 XOR 0 = 1
if we applied 1 XOR 6 then
(encrypt) 6 with 1
110 (6)
001 (1)
---
111 (7)
(decrypt encrypted) 6 with 1
111 (7)
001 (1)
---
110 (6)
C# Code
It might help you somehow !
Today I faced a new way of encryption
was I applying for a new job, there was a set of questions I have to solve
between them, there was a crypting question, and I thought it could be solve be using a basic way of adding some integer values to the ASCII of current char for specific text (string)
after submitting my answers, I met the supervisor who told my that I failed with that question (crypting one) and just told me, why you did not use XOR !
some working and I found what XOR encrypt is
0 XOR 0 = 0
0 XOR 1 = 1
1 XOR 1 = 0
1 XOR 0 = 1
if we applied 1 XOR 6 then
(encrypt) 6 with 1
110 (6)
001 (1)
---
111 (7)
(decrypt encrypted) 6 with 1
111 (7)
001 (1)
---
110 (6)
C# Code
Code:
public static string XORCrypt(string toCrypt, string password)
{
string encrepted = "";
int j = 0;
for (int i = 0; i < toCrypt.Length; i++)
{
if(j==password.Length)
j=0;
encrepted += (Char)(toCrypt[i] ^ password[j++]);//Using XOR bitwise operation
}
return encrepted;
}
public static string XORDecrypt(string Encrypted, string password)
{
string decrypted = "";
int j = 0;
for (int i = 0; i < Encrypted.Length; i++)
{
if (j == password.Length)
j = 0;
decrypted += (Char)(Encrypted[i] ^ password[j++]);
}
return decrypted;
}
It might help you somehow !