Created
March 23, 2016 13:28
-
-
Save petehouston/211319fda5796d4b0d05 to your computer and use it in GitHub Desktop.
XOR encrypt
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
/** | |
* $ gcc -o program xor_encrypt.c | |
* $ ./program | |
*/ | |
#include <stdio.h> | |
#include <stdlib.h> | |
#include <string.h> | |
#define LOG printf | |
char *xor_encrypt(const char * message, const char * key) { | |
size_t messagelen = strlen(message); | |
size_t keylen = strlen(key); | |
char * encrypted = malloc((messagelen + 1)* sizeof(char)); | |
int i; | |
for(i = 0; i < messagelen; i++) { | |
encrypted[i] = message[i] ^ key[i % keylen]; | |
while (encrypted[i] >90) encrypted[i]-=10; | |
while (encrypted[i] <65) encrypted[i]+=10; | |
} | |
encrypted[messagelen] = '\0'; | |
return encrypted; | |
} | |
void test_case_01() { | |
LOG("---Running: test_case_01\n"); | |
const char *message = "CFCUUSHISIFRIJF"; | |
const char *key = "IJGJGJVGDIFTEESDGIHJBGDARTJIIHHGIQBJAHFDQGHYWUSWVCJETDEGHYHFHDDHYCGIEVCJCATXXSHEGHBAGDIETQGHYDFTHI"; | |
const char *expected = "FHJGDAFJIFFBHAG"; | |
char *actual = xor_encrypt(message, key); | |
LOG("Expect: %s\n", expected); | |
LOG("Actual: %s\n", actual); | |
if(strcmp(expected, actual) == 0) { | |
LOG("Passed\n"); // Passed actually | |
} else { | |
LOG("Failed\n"); | |
} | |
free(actual); | |
} | |
int main(int argc, char **argv) | |
{ | |
test_case_01(); | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
hello my friend and to decrypter ?