Created
May 2, 2021 09:55
-
-
Save touatily/3af14ed032c407fd3e2bb2dbe3bdc154 to your computer and use it in GitHub Desktop.
Implemnetation of vignere cipher
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
#include "vigenere.h" | |
void vigenereEnc(const char * text, const char * key, char * ciphertext){ | |
unsigned int i, size = strlen(key); | |
for(i = 0; text[i] != '\0'; i++){ | |
if( (text[i] >= 'a') && (text[i] <= 'z') ){ | |
int rang = (text[i] + key[i % size] - 2 * 'a') % 26; | |
ciphertext[i] = 'a' + rang; | |
} | |
else if( (text[i] >= 'A') && (text[i] <= 'Z') ){ | |
int rang = (text[i] + key[i % size] - 'a' - 'A') % 26; | |
ciphertext[i] = 'A' + rang; | |
} | |
else | |
ciphertext[i] = text[i]; | |
} | |
ciphertext[i] = '\0'; | |
} | |
void vigenereDec(const char * ciphertext, const char * key, char * text){ | |
unsigned int i, size = strlen(key); | |
char keytemp[size + 1]; | |
for(i = 0; key[i] != 0; i++){ | |
int rang = (26 - (key[i] - 'a')) % 26; | |
keytemp[i] = rang + 'a'; | |
} | |
keytemp[i] = '\0'; | |
vigenereEnc(ciphertext, keytemp, text); | |
} |
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
void vigenereEnc(const char * text, const char * key, char * ciphertext); | |
void vigenereDec(const char * ciphertext, const char * key, char * text); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment