c - some problem with byte xor -
i have make binary xor on 2 byte buffers. 1 encoding key, other encoded value.
at moment code looks this:
byte key[]={"somekey"} byte value[]={"somevalue"}; for(i= 0; < valllength; i++) { valbuf[i] = value[i] ^ key[i % keylength]; } this not work me. doing wrong?
alright, terrible, felt writing easy code change.
#include <stdio.h> char key[] = "this fancy key."; char text[] = "encrypt extremely long plaintext."; #define key_len (sizeof(key)) #define txt_len (sizeof(text)) void crypt(char *key, char *plaintext, char *ciphertext, int keylen, int ptlen) { int idx; (idx = 0; idx < ptlen; idx++) { ciphertext[idx] = plaintext[idx] ^ key[idx % keylen]; } } int main() { printf("plaintext before:\n\t%s\n", text); crypt(key, text, text, key_len, txt_len); printf("plaintext after:\n\t%s\n", text); crypt(key, text, text, key_len, txt_len); printf("plaintext after after:\n\t%s\n", text); return 0; } this question's destined closure anyway, can't hurt post here.
Comments
Post a Comment