From e76175e5c1c2f34ad3cb69b7755e471ecc756a28 Mon Sep 17 00:00:00 2001 From: Sherman Leung Date: Tue, 13 Oct 2015 14:35:48 -0700 Subject: [PATCH] added submission for gusliu --- gusliu-assign1/caesar-cipher.txt | 1 + gusliu-assign1/caesar-plain.txt | 1 + gusliu-assign1/crypto.py | 268 ++++++++++++++++++++++++++++ gusliu-assign1/feedback.txt | 23 +++ gusliu-assign1/railfence-cipher.txt | 1 + gusliu-assign1/railfence-plain.txt | 1 + gusliu-assign1/secret_message.txt | 1 + gusliu-assign1/vigenere-cipher.txt | 1 + gusliu-assign1/vigenere-plain.txt | 1 + 9 files changed, 298 insertions(+) create mode 100644 gusliu-assign1/caesar-cipher.txt create mode 100644 gusliu-assign1/caesar-plain.txt create mode 100644 gusliu-assign1/crypto.py create mode 100644 gusliu-assign1/feedback.txt create mode 100644 gusliu-assign1/railfence-cipher.txt create mode 100644 gusliu-assign1/railfence-plain.txt create mode 100644 gusliu-assign1/secret_message.txt create mode 100644 gusliu-assign1/vigenere-cipher.txt create mode 100644 gusliu-assign1/vigenere-plain.txt diff --git a/gusliu-assign1/caesar-cipher.txt b/gusliu-assign1/caesar-cipher.txt new file mode 100644 index 0000000..77ba61d --- /dev/null +++ b/gusliu-assign1/caesar-cipher.txt @@ -0,0 +1 @@ +SBWKRQ \ No newline at end of file diff --git a/gusliu-assign1/caesar-plain.txt b/gusliu-assign1/caesar-plain.txt new file mode 100644 index 0000000..38a33ed --- /dev/null +++ b/gusliu-assign1/caesar-plain.txt @@ -0,0 +1 @@ +PYTHON \ No newline at end of file diff --git a/gusliu-assign1/crypto.py b/gusliu-assign1/crypto.py new file mode 100644 index 0000000..92fb2ab --- /dev/null +++ b/gusliu-assign1/crypto.py @@ -0,0 +1,268 @@ +""" +Assignment 1: Cryptography +Course: CS 92SI +Name: +Date: <10/7/15> + +Replace this with a description of the program. +""" + +def encrypt_caesar(plaintext): + """ + Encrypts plaintext using a Caesar cipher. + Add more implementation details here. + """ + encrypted = '' + for char in plaintext: + ascii = ord(char) + 3 + if char.isupper(): + ascii -= ord('A') + ascii %= 26 + encrypted += chr(ord('A') + ascii) + else: + ascii -= ord('a') + ascii %= 26 + encrypted += chr(ord('a') + ascii) + return encrypted + + +def decrypt_caesar(ciphertext): + """ + Decrypts a ciphertext using a Caesar cipher. + Add more implementation details here. + """ + encrypted = '' + for char in ciphertext: + ascii = ord(char) - 3 + if char.isupper(): + ascii -= ord('A') + if ascii < 0: + ascii += 26 + encrypted += chr(ord('A') + ascii) + else: + ascii -= ord('a') + if ascii < 0: + ascii += 26 + encrypted += chr(ord('a') + ascii) + return encrypted + +def get_vigenere_key(length, keyword): + repeats = length // len(keyword) + leftover = length % len(keyword) + key = keyword * repeats + keyword[:leftover] + return key + +def encrypt_vigenere(plaintext, keyword): + """ + Encrypts plaintext using a Vigenere cipher with a keyword. + Add more implementation details here. + """ + plaintext = plaintext.upper() + keyword = keyword.upper() + length = len(plaintext) + key = get_vigenere_key(length, keyword) + encrypted = '' + for i in range(length): + pchar = plaintext[i] + kchar = key[i] + ascii = (ord(pchar) + ord(kchar)) % 26 + encrypted += chr(ord('A') + ascii) + return encrypted + + + +def decrypt_vigenere(ciphertext, keyword): + """ + Decrypts ciphertext using a Vigenere cipher with a keyword. + Add more implementation details here. + """ + ciphertext = ciphertext.upper() + length = len(ciphertext) + key = get_vigenere_key(length, keyword) + decrypted = '' + for i in range(length): + cchar = ciphertext[i] + kchar = key[i] + ascii = (ord(cchar) - ord(kchar)) % 26 + decrypted += chr(ord('A') + ascii) + return decrypted + + +def encrypt_railfence(plaintext, num_rails): + """ + Encrypts plaintext using a railfence cipher. + Add more implementation details here. + """ + rows = [''] * num_rails + length = len(plaintext) + for i in range(length): + n = i // (num_rails - 1) + index = i % (num_rails - 1) + if n % 2 != 0: + index = num_rails - 1 - index + rows[index] += plaintext[i] + z = [''.join(x) for x in rows] + return ''.join(z) + + +def decrypt_railfence(ciphertext, num_rails): + """ + Encrypts plaintext using a railfence cipher. + Add more implementation details here. + """ + lengths = [0] * num_rails + rows = [''] * num_rails + length = len(ciphertext) + row = 0 + going_down = True + for i in range(length): + if row == 0: + going_down = True + if row == num_rails - 1: + going_down = False + lengths[row] += 1 + if going_down: + row += 1 + else: + row -= 1 + index = 0 + for i in range(num_rails): + rows[i] = ciphertext[index:index + lengths[i]] + index += lengths[i] + going_down = True + encrypted = '' + row = 0 + for i in range(length): + if row == 0: + going_down = True + if row == num_rails - 1: + going_down = False + encrypted += (rows[row])[0] + rows[row] = (rows[row])[1:] + if going_down: + row += 1 + else: + row -= 1 + return encrypted + + +def read_from_file(filename): + """ + Reads and returns content from a file. + Add more implementation details here. + """ + with open(filename, 'r') as f: + content = f.read() + f.closed + return content + + +def write_to_file(filename, content): + """ + Writes content to a file. + Add more implementation details here. + """ + with open(filename, 'w') as f: + f.write(content) + f.close() + + +def run_suite(): + """ + Runs a single iteration of the cryptography suite. + + Asks the user for input text from a string or file, whether to encrypt + or decrypt, what tool to use, and where to show the output. + """ + print("*Input*") + input_type = input('(F)ile or (S)tring? ').lower() + while True: + if input_type == 's': + s = input('Enter the string to encrypt: ').lower() + elif input_type == 'f': + file_name = input('Filename? ') + s = read_from_file(file_name).lower() + else: + continue + break + + s = s.replace(" ", "") + + print("*Transform*") + transform_type = input('(E)ncrypt or (D)ecrypt? ').lower() + while transform_type != 'e' and transform_type != 'd': + transform_type = input('(E)ncrypt or (D)ecrypt? ').lower() + + transform_method = input('(C)aesar, (V)igenere, or (R)ailfence? ').lower() + while transform_method != 'c' and transform_method != 'v' and transform_method != 'r': + transform_method = input('(C)aesar, (V)igenere, or (R)ailfence? ').lower() + + if transform_method == 'c': + if transform_type == 'e': + print("Encrypting " + s + " using Caesar cipher...") + output = encrypt_caesar(s) + else: + print("Decrypting " + s + " using Caesar cipher...") + output = decrypt_caesar(s) + elif transform_method == 'v': + secret_key = input('Passkey? ') + if transform_type == 'e': + print("Encrypting " + s + " using Vigenere cipher with key " + secret_key + "...") + output = encrypt_vigenere(s, secret_key) + else: + print("Decrypting " + s + " using Vigenere cipher with key " + secret_key + "...") + output = decrypt_vigenere(s, secret_key) + elif transform_method == 'r': + num_rails = input('Number of rails? ') + while not num_rails.isnumeric(): + num_rails = input('Number of rails? ') + num_rails = int(num_rails) + if transform_type == 'e': + print("Encrypting " + s + " using railfence cipher with " + num_rails + " rails...") + output = encrypt_railfence(s, num_rails) + else: + print("Decrypting " + s + " using railfence cipher with " + num_rails + " rails...") + output = decrypt_railfence(s, num_rails) + + print("*Output*") + output_type = input('(F)ile or (S)tring? ').lower() + while True: + if output_type == 's': + print(output) + elif output_type == 'f': + file_name = input('Filename? ') + print("Writing ciphertext to " + file_name) + write_to_file(file_name, output) + else: + continue + break + + + +# Do not modify code beneath this point. +def should_continue(): + """ + Asks the user whether they would like to continue. + Responses that begin with a `Y` return True. (case-insensitively) + Responses that begin with a `N` return False. (case-insensitively) + All other responses (including '') cause a reprompt. + """ + choice = input("Again (Y/N)? ").upper() + while not choice or choice[0] not in ['Y', 'N']: + choice = input("Please enter either 'Y' or 'N'. Again (Y/N)? ").upper() + return choice[0] == 'Y' + + +def main(): + """Harness for the Cryptography Suite""" + print("Welcome to the Cryptography Suite!") + run_suite() + while should_continue(): + run_suite() + print("Goodbye!") + + +if __name__ == '__main__': + """This block is run if and only if the Python script is invoked from the + command line.""" + main() diff --git a/gusliu-assign1/feedback.txt b/gusliu-assign1/feedback.txt new file mode 100644 index 0000000..1c30e92 --- /dev/null +++ b/gusliu-assign1/feedback.txt @@ -0,0 +1,23 @@ +Name: +1) How long did this assignment take you to complete? + + + +2) What has been the best part of the class so far? + + + + + +3) What can we do to make this class more enjoyable for you? + + + + + +4) What types of assignments would excite you in this class? + + + + + diff --git a/gusliu-assign1/railfence-cipher.txt b/gusliu-assign1/railfence-cipher.txt new file mode 100644 index 0000000..c1ae5da --- /dev/null +++ b/gusliu-assign1/railfence-cipher.txt @@ -0,0 +1 @@ +WECRLTEERDSOEEFEAOCAIVDEN \ No newline at end of file diff --git a/gusliu-assign1/railfence-plain.txt b/gusliu-assign1/railfence-plain.txt new file mode 100644 index 0000000..fc27f60 --- /dev/null +++ b/gusliu-assign1/railfence-plain.txt @@ -0,0 +1 @@ +WE ARE DISCOVERED. FLEE AT ONCE \ No newline at end of file diff --git a/gusliu-assign1/secret_message.txt b/gusliu-assign1/secret_message.txt new file mode 100644 index 0000000..860b90c --- /dev/null +++ b/gusliu-assign1/secret_message.txt @@ -0,0 +1 @@ +KHOORZRUOG \ No newline at end of file diff --git a/gusliu-assign1/vigenere-cipher.txt b/gusliu-assign1/vigenere-cipher.txt new file mode 100644 index 0000000..b04ec8d --- /dev/null +++ b/gusliu-assign1/vigenere-cipher.txt @@ -0,0 +1 @@ +LXFOPVEFRNHR \ No newline at end of file diff --git a/gusliu-assign1/vigenere-plain.txt b/gusliu-assign1/vigenere-plain.txt new file mode 100644 index 0000000..877099d --- /dev/null +++ b/gusliu-assign1/vigenere-plain.txt @@ -0,0 +1 @@ +Attack At Dawn! \ No newline at end of file