Skip to content

Instantly share code, notes, and snippets.

@touatily
Created May 2, 2021 09:55
Show Gist options
  • Save touatily/3af14ed032c407fd3e2bb2dbe3bdc154 to your computer and use it in GitHub Desktop.
Save touatily/3af14ed032c407fd3e2bb2dbe3bdc154 to your computer and use it in GitHub Desktop.
Implemnetation of vignere cipher
#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);
}
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