Skip to content

Instantly share code, notes, and snippets.

@OxiBo
Last active September 9, 2017 23:12
Show Gist options
  • Save OxiBo/6f2a18c6ebeb371198626ba75a6071c1 to your computer and use it in GitHub Desktop.
Save OxiBo/6f2a18c6ebeb371198626ba75a6071c1 to your computer and use it in GitHub Desktop.
CS50 week 2/week 8 vigenere
/**
* CS50 week 2 vigenere
*/
#include <stdio.h>
#include <cs50.h>
#include <ctype.h>
#include <string.h>
#include <stdlib.h>
int main(int argc, string argv[])
{
// make sure user gives secret key from command line
if (argc!=2)
{
printf("missing command-line argument\n");
return 1;
}
else
{
//check if characters in argv[1] are alphabedic
for (int l=0, s=strlen(argv[1]); l<s; l++)
{
if(!isalpha(argv[1][l]))
{
printf("key word can contain only alphabetic characters\n");
return 1;
}
}
printf("plaintext:");
string plaintext=get_string();
if (plaintext!=NULL&&argv[1]!=NULL)
{
printf("ciphertext:");
//iterate over the characters in the plaintext one at a time and get access to consecutive characters of argv[1] key
for (int i=0, n=strlen(plaintext), k=0; i<n; i++)
{
int cyphertext;
//check if [i] character is an alphabetic character and uppercase
if (isalpha(plaintext[i])&&isupper(plaintext[i]))
{
//find alphabetic index of uppercase character in plaintext
int alph_index=plaintext[i]%65;
//get access to j character in agrv[1] and find it's alphabetic index
int j=k%(strlen(argv[1]));
int alph_index_argv=toupper(argv[1][j])%65;
//find and print corresponding character in ciphertext in ASCII in uppercase
cyphertext=65+(alph_index+alph_index_argv)%26;
printf("%c", cyphertext);
k++;
}
else
{
//check if [i] character is alphabetic character and lowercase
if (isalpha(plaintext[i])&&islower(plaintext[i]))
{
//find alphabetic index of lowercase character in plaintext
int alph_index=plaintext[i]%97;
//get access to j character in agrv[1] and find it's alphabetic index
int j=k%(strlen(argv[1]));
int alph_index_argv=toupper(argv[1][j])%65;
//find and print corresponding character in ciphertext in ASCII in lowercase
cyphertext=97+(alph_index+alph_index_argv)%26;
printf("%c", cyphertext);
k++;
}
else
{
//print [i] character if it is not alphabetic character
printf("%c", plaintext [i]);
}
}
}
printf("\n");
return 0;
}
}
}
#program that encrypts messages using Vigenère’s cipher
def main():
import sys
import cs50
# make sure user gives secret key from command line
if len(sys.argv) != 2:
print("missing command-line argument")
sys.exit(1)
else:
key = str(sys.argv[1])
#check if characters in argv[1] are alphabetic
for char in key:
if not str.isalpha(char):
print("key word can contain only alphabetic characters")
sys.exit(1)
#get plaintext from a user
print("plaintext:", end = "")
plaintext = cs50.get_string()
if plaintext != None and str(sys.argv[1]) != None:
print("ciphertext:", end="")
k = 0
for char in plaintext:
#check if char character is an alphabetic character and uppercase
if str.isalpha(char) and str.isupper(char):
#//find alphabetical index of uppercase character in plaintext
alph_index = ord(char)%65
#//get access to j-th character in agrv[1] and find it's alphabetic index
j = k%(len(sys.argv[1]))
alph_index_argv = ord(str.upper(sys.argv[1][j]))%65
#//find and print corresponding character in ciphertext
ciphertext = chr(65 + (alph_index+alph_index_argv)%26)
print(ciphertext, end = "")
k += 1
#check if char character is alphabetic character and lowercase
elif str.isalpha(char) and str.islower(char):
#find alphabetic index of lowercase character in plaintext
alph_index = ord(char)%97
#get access to j-th character in agrv[1] and find it's alphabetical index
j = k%(len(sys.argv[1]))
alph_index_argv = ord(str.upper(sys.argv[1][j]))%65
#find and print corresponding character in ciphertext in ASCII in lowercase
ciphertext = chr(97 + (alph_index + alph_index_argv)%26)
print(ciphertext, end = "")
k += 1
else:
#print char character if it is not alphabetic character
print(char, end = "")
print()
exit(0)
if __name__ == "__main__":
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment