From aa4db0ae3b83b0d0c576e140d21b98562521b93c Mon Sep 17 00:00:00 2001 From: Sharanpai Date: Sat, 7 Oct 2017 02:59:20 +0530 Subject: [PATCH 1/2] Added a script that encryts or derypts a message, using the caesar cipher --- bin/caesar_cipher.py | 54 ++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 54 insertions(+) create mode 100644 bin/caesar_cipher.py diff --git a/bin/caesar_cipher.py b/bin/caesar_cipher.py new file mode 100644 index 0000000..4a183a0 --- /dev/null +++ b/bin/caesar_cipher.py @@ -0,0 +1,54 @@ +key = 'abcdefghijklmnopqrstuvwxyz' + +def encrypt(n, plaintext): + """Encrypt the string and return the ciphertext""" + result = '' + + for l in plaintext.lower(): + try: + i = (key.index(l) + n) % 26 + result += key[i] + except ValueError: + result += l + + return result.lower() + + +def decrypt(n, ciphertext): + """Decrypt the string and return the plaintext""" + result = '' + + for l in ciphertext: + try: + i = (key.index(l) - n) % 26 + result += key[i] + except ValueError: + result += l + + return result + + +def show_result(plaintext, n): + """Generate a resulting cipher with elements shown""" + encrypted = encrypt(n, plaintext) + decrypted = decrypt(n, encrypted) + + print ('Rotation: %s' % n) + print ('Plaintext: %s' % plaintext) + print ('Encrytped: %s' % encrypted) + print ('Decrytped: %s' % decrypted) +print("Encrypt or decrypt?") +ans = input() +ans = ans.lower() +print("Enter message") +k = input() +print("Enter rotation number") +nn = int(input()) +if ans == 'encrypt': + ret = encrypt(nn,k) + print ('Encrytped: %s' % ret) +else: + ret = decrypt(nn,k) + print ('Decrytped: %s' % ret) + +#show_result(k,int(nn)) \ No newline at end of file From c73395c3a87e850e281e48bc2ecd2806a3e6e89d Mon Sep 17 00:00:00 2001 From: Sharan Pai Date: Sat, 7 Oct 2017 03:16:22 +0530 Subject: [PATCH 2/2] Update caesar_cipher.py --- bin/caesar_cipher.py | 12 ------------ 1 file changed, 12 deletions(-) diff --git a/bin/caesar_cipher.py b/bin/caesar_cipher.py index 4a183a0..df9fc40 100644 --- a/bin/caesar_cipher.py +++ b/bin/caesar_cipher.py @@ -27,16 +27,6 @@ def decrypt(n, ciphertext): return result - -def show_result(plaintext, n): - """Generate a resulting cipher with elements shown""" - encrypted = encrypt(n, plaintext) - decrypted = decrypt(n, encrypted) - - print ('Rotation: %s' % n) - print ('Plaintext: %s' % plaintext) - print ('Encrytped: %s' % encrypted) - print ('Decrytped: %s' % decrypted) print("Encrypt or decrypt?") ans = input() ans = ans.lower() @@ -50,5 +40,3 @@ def show_result(plaintext, n): else: ret = decrypt(nn,k) print ('Decrytped: %s' % ret) - -#show_result(k,int(nn)) \ No newline at end of file