Last active
May 31, 2017 10:17
-
-
Save shivamg7/b47d1348944a6f10fb7212738e71ff69 to your computer and use it in GitHub Desktop.
Encoded supplied input according to vigenere crytography, key is suppiled at command line.
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 <stdio.h> | |
#include <cs50.h> | |
#include <string.h> | |
#include <ctype.h> | |
int main(int argv,char *argc[]) | |
{ | |
if(argv!=2) | |
{ | |
printf("Usage: ./vigenere k\n"); | |
return 1; | |
} | |
string key=argc[1]; | |
int i; | |
int flag = 1; | |
for(i=0; i< strlen(key) && flag == true; i++ ) | |
{ | |
if( isalpha(key[i])) | |
flag = 1; | |
else | |
flag = 0; | |
key[i]=tolower(key[i]); | |
} | |
// printf("%s\n",key); | |
if (flag==0) | |
{ printf("Error\n"); return 1; } | |
string plain; | |
char cipher[100]; | |
printf("plaintext: "); | |
plain=get_string(); | |
int plain_length=strlen(plain),key_length=strlen(key); | |
int j=0; | |
for(i=0;i<plain_length;i++) | |
{ | |
//printf("%s\n",cipher); | |
if(isalpha(plain[i])&&isupper(plain[i])) | |
{ | |
//printf("%d\n",(key[(j++)%key_length])-97); | |
cipher[i]=( ( ( plain[i] + (key[(j++)%key_length]-97) ) ) -65)%26 + 65; | |
} | |
else if(isalpha(plain[i])&&islower(plain[i])) | |
{ | |
cipher[i]=(((plain[i]+(key[(j++)%key_length]-97)))-97)%26+97; | |
} | |
else | |
{ | |
cipher[i]=plain[i]; | |
} | |
} | |
cipher[i]='\0'; | |
printf("ciphertext: %s\n",cipher); | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment