2017-04-12 5 views
-2

나는 혼란 스럽다. 동일한 프로그램에서 암호화와 암호 해독을 수행하는 C의 XOR 암호화 프로그램이 있지만 암호화와 암호 해독이 별도로 필요하다.C에서 XOR 암호화를 구현하여 클라이언트에서 서버로 데이터를 전송하는 방법

그런 다음 클라이언트가 암호화/XOR (암호화 된 서버)에 데이터/문자열 메시지를 보내고 서버가 암호화 된 문자열 메시지를 C 프로그램에서 수신해야하는 코드를 원합니다.

#include<stdio.h> 
#include<string.h> 

void encryptDecrypt(char *input, char *output) 
{ 
char key[] = {'K', 'C', 'Q'}; //Can be any chars, and any size array 
int i; 
for(i = 0; i < strlen(input); i++) { 
    output[i] = input[i]^key[i % (sizeof(key)/sizeof(char))]; 
} 
} 

int main() 
{ 
char baseStr[1000]; 

printf("Enter string:"); 
fgets(baseStr, sizeof baseStr, stdin); 
char encrypted[strlen(baseStr)]; 
encryptDecrypt(baseStr, encrypted); 
printf("Encrypted:%s\n\n\n", encrypted); 
char decrypted[strlen(baseStr)]; 
encryptDecrypt(encrypted, decrypted); 
printf("Decrypted:%s\n\n\n", decrypted); 
} 
+3

정확하게 묻는 것은 무엇입니까? –

+0

xor 암호화를 사용하여 클라이언트에서 서버로 메시지를 전송하기위한 코드가 필요합니다. – Megha

+0

나만의 암호화를 만들지 마십시오. 클라이언트 서버 통신을 위해 https 만 사용하면 모든 것이 암호화됩니다. [ "Schneier 's Law"] (https://www.schneier.com/blog/archives/2011/04/schneiers_law.html) : 가장 단조롭지 않은 아마추어에서부터 최고의 암호 작성자에 이르기까지 누구나 자신이 할 수있는 알고리즘을 만들 수 있습니다 '휴식. – zaph

답변

0

에 한번이 문제에 대한 내 생각으로 정렬 :

이 내 코드입니다. 코드를 약간 편집했습니다 :

#include<stdio.h> 
#include<string.h> 

void encryptDecrypt(char *input, char *output) 
{ 
    char key[] = { 'K', 'C', 'Q' }; //Can be any chars, and any size array 
    int i; 
    for (i = 0; i < strlen(input); i++) { 
     output[i] = input[i]^key[i % (sizeof(key)/sizeof(char))]; 
    } 
    output[strlen(input)] = '\0'; 
} 
void serverDecryptedText(char* encryptedText, char* decryptedNormalText) 
{ 
    /*Assumed: Server got encryptedText*/ 
    encryptDecrypt(encryptedText, decryptedNormalText); 
    /*You will get original text in decryptedNormalText*/ 
} 
void sendToServerWithEncryption(char* normalText, char* encryptedText) 
{ 
    encryptDecrypt(normalText, encryptedText); 
    /*Implement code to send encryptedText to server here */ 
} 
int main() 
{ 
    char baseStr[1000]; 
    printf("Enter string:"); 
    scanf("%s",baseStr); 

    char *encrypted = (char*)malloc(strlen(baseStr)+1); 
    char *decrypted = (char*)malloc(strlen(baseStr) + 1); 

    sendToServerWithEncryption(baseStr, encrypted); 

    printf("Encrypted:%s\n\n\n", encrypted); 

    serverDecryptedText(encrypted, decrypted); 

    printf("Decrypted:%s\n\n\n", decrypted); 
    return 0; 
} 
+0

단순한 계획에는 실제 보안이 없습니다. – zaph

+0

예, 그 힌트. 보안 체계를 구현해야합니다. –

관련 문제