Last active
September 19, 2016 12:49
-
-
Save RickyNotaro/856c98ab5fd25a131200 to your computer and use it in GitHub Desktop.
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
/* | |
* | |
* Vigenere.c for Cs50 | |
* By Ricky Notaro | |
* ci = (pi + kj) % 26 | |
*/ | |
#include <stdio.h> | |
#include <stdlib.h> | |
#include <cs50.h> | |
#include <string.h> | |
#include <ctype.h> | |
int main(int input, string argv[]) | |
{ | |
// check if the number of argv is ok | |
if (input == 2) | |
{ | |
// set the arg to passkey | |
string passkey = argv[1]; | |
// This loop check if the pass key is a-z or A-Z | |
for (int currentchar = 0, n = strlen(passkey); currentchar < n; currentchar++) | |
{ | |
if (!isalpha(passkey[currentchar])) | |
{ | |
printf("<passkey> must only contain letters A-Z and a-z.\n"); | |
return 1; | |
} | |
} | |
// Get plaintext to encrypt | |
string plaintext = GetString(); | |
// Begin of the encryption process | |
int i = 0; | |
int l = strlen(passkey); | |
for (int currentchar = 0,n = strlen(plaintext); currentchar < n; currentchar++) | |
{ | |
// if the current char is alpha the char will be encrpted | |
if (isalpha(plaintext[currentchar])) | |
{ | |
if (i < l) | |
{ | |
i++; | |
} | |
else | |
{ | |
i = 1; | |
} | |
int a = plaintext[currentchar]; | |
char b; | |
int c; | |
if (isupper(a)) | |
{ | |
b = 'A'; | |
} | |
else | |
{ | |
b = 'a'; | |
} | |
plaintext[currentchar] = ((((plaintext[currentchar] + tolower((passkey[i-1])) + 7) - b) % 26)+b); | |
} | |
printf("%c",plaintext[currentchar]); | |
} | |
printf("\n"); | |
} | |
else | |
{ | |
printf("Usage: vigenere <passkey>\n"); | |
return 1; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment