언어/c언어

xor 암호 기법

파아랑새 2017. 5. 13. 20:29

# include <stdio.h>

# include <stdlib.h>

# include <string.h>

# define SIZE 30

typedef struct XOR

{

char PlainText[SIZE];

char CipherText[SIZE];

char DecryptText[SIZE];

int xorNumber;

}XOR;

XOR* CreatNode();

void __WritePlainText__(XOR** x);

void __Encrypt_XOR__(XOR** x);

void __Decrypt_XOR__(XOR** x);


int main(void) {

XOR* x = NULL;

x = CreatNode();

if (x == NULL)

{

exit(1);

}

// data Initiallize

memset(x->PlainText, 0, sizeof(char)*SIZE);

memset(x->CipherText, 0, sizeof(char)*SIZE);

memset(x->DecryptText, 0, sizeof(char)*SIZE);

x->xorNumber = 7;


// function call

__WritePlainText__(&x);

__Encrypt_XOR__(&x);

__Decrypt_XOR__(&x);

return 0;

}


XOR* CreatNode()

{

XOR* newNode = (XOR*)malloc(sizeof(XOR));

return newNode;

}


void __WritePlainText__(XOR** x)

{

strcpy((**x).PlainText, "Hello World");

printf("plainText => %s \n", (*x)->PlainText);

}


void __Encrypt_XOR__(XOR** x)

{

unsigned int i; // index

char ascii = '\0';

for (i = 0; i < strlen((*x)->PlainText); i++)

{

(*x)->CipherText[i] = (char)

 ((*x)->PlainText[i] ^ (*x)->xorNumber);

}

printf("cipherText => %s \n", (*x)->CipherText);

}


void __Decrypt_XOR__(XOR** x)

{

unsigned int i; // index

char ascii = '\0';

for (i = 0; i < strlen((*x)->CipherText); i++)

{

(*x)->DecryptText[i] = (char)

((*x)->CipherText[i] ^ (*x)->xorNumber);

}

if (!strcmp((*x)->PlainText, (*x)->DecryptText))

{

printf("DecryptText => %s \n", (*x)->DecryptText);

}

}