From dffcc1e6b312bb6d29a0186c0af9d5327e5d917e Mon Sep 17 00:00:00 2001 From: rahulkumaran Date: Sat, 2 Dec 2017 07:12:44 +0530 Subject: [PATCH 01/21] Added a code for Caesar Cipher --- Cryptography/caesarcipher.py | 57 ++++++++++++++++++++++++++++++++++++ 1 file changed, 57 insertions(+) create mode 100644 Cryptography/caesarcipher.py diff --git a/Cryptography/caesarcipher.py b/Cryptography/caesarcipher.py new file mode 100644 index 000000000..ef7255137 --- /dev/null +++ b/Cryptography/caesarcipher.py @@ -0,0 +1,57 @@ +""" +A general purpose function for both, encryption and decryption. +After giving the string that you want, if you decide to shift it to right, +encryption will take place by shifting characters to the right and +the decrypted string can be obtained by shifting the encrypted string to the left. +The general purpose function takes care of both the left and the right shifts. +Based on how the user uses it, he or she can encrypt and decrypt the data. +""" + + +def encrypt_and_decrypt(string,shift,val): + + ascii_vals=[ord(c) for c in string] #To get a list of ASCII Values of each character in string + if(shift == "right"): #When the text it shifted to the right + for i in range(0,len(ascii_vals)): + if(ascii_vals[i] == 32): #Spaces are not ciphered + ascii_vals[i] = 32 + else: + ascii_vals[i] = ascii_vals[i] + val + if(ascii_vals[i]>97 and ascii_vals[i]>122): #If the values in the list are greater than 97 and 122, we get it back b/w 97 and 122 + ascii_vals[i] = 97 + (ascii_vals[i]-122) -1 + elif((ascii_vals[i]>97 and ascii_vals[i]<=122) or (ascii_vals[i]>65 and ascii_vals[i]<=90)): + ascii_vals[i] = ascii_vals[i] + elif(ascii_vals[i]>65 and ascii_vals[i]>90): #If the values in the list are greater than 65 and 90, we get it back b/w 97 and 122 + ascii_vals[i] = 65 + (ascii_vals[i]-90) -1 + if(shift == "left"): #When the text it shifted to the right + for i in range(0,len(ascii_vals)): + if(ascii_vals[i] == 32): #Spaces are not ciphered + ascii_vals[i] = 32 + else: + ascii_vals[i] = ascii_vals[i] - val + print ascii_vals[i] + if(ascii_vals[i]<97 and ascii_vals[i]<122): #If the values in the list are greater than 97 and 122, we get it back b/w 97 and 122 + ascii_vals[i] = 122 + (ascii_vals[i]-97) +1 + print ascii_vals[i] + elif((ascii_vals[i]>97 and ascii_vals[i]<=122) or (ascii_vals[i]>65 and ascii_vals[i]<=90)): + ascii_vals[i] = ascii_vals[i] + elif(ascii_vals[i]<65 and ascii_vals[i]<90): #If the values in the list are greater than 65 and 90, we get it back b/w 97 and 122 + ascii_vals[i] = 90 + (ascii_vals[i]-65) +1 + + encrypt_string = ''.join(chr(i) for i in ascii_vals) # To get the encrypted string using the list of manipulated ASCII vals + print encrypt_string + + + + decrypt_string = ''.join(chr(i) for i in ascii_vals) # To get the encrypted string using the list of manipulated ASCII vals + print decrypt_string + +def caesar_cipher(): + string = raw_input("Enter text to be encrypted:") + encrypt_and_decrypt(string,shift,val) + + +if __name__=="__main__": + shift = str(raw_input("Do you want to shift to right or to left?")) + val = int(raw_input("By what value do you want to shift?")) + caesar_cipher() From 98eb1f291613ef5a12d8f82f3d0cc7383b0582ba Mon Sep 17 00:00:00 2001 From: rahulkumaran Date: Sat, 2 Dec 2017 07:35:00 +0530 Subject: [PATCH 02/21] removed a few loops --- Cryptography/caesarcipher.py | 51 +++++++++++++----------------------- 1 file changed, 18 insertions(+), 33 deletions(-) diff --git a/Cryptography/caesarcipher.py b/Cryptography/caesarcipher.py index ef7255137..138a7cfe6 100644 --- a/Cryptography/caesarcipher.py +++ b/Cryptography/caesarcipher.py @@ -8,49 +8,34 @@ """ -def encrypt_and_decrypt(string,shift,val): - +def encrypt_and_decrypt(string,shift,val): ascii_vals=[ord(c) for c in string] #To get a list of ASCII Values of each character in string if(shift == "right"): #When the text it shifted to the right for i in range(0,len(ascii_vals)): - if(ascii_vals[i] == 32): #Spaces are not ciphered - ascii_vals[i] = 32 - else: - ascii_vals[i] = ascii_vals[i] + val - if(ascii_vals[i]>97 and ascii_vals[i]>122): #If the values in the list are greater than 97 and 122, we get it back b/w 97 and 122 - ascii_vals[i] = 97 + (ascii_vals[i]-122) -1 - elif((ascii_vals[i]>97 and ascii_vals[i]<=122) or (ascii_vals[i]>65 and ascii_vals[i]<=90)): - ascii_vals[i] = ascii_vals[i] - elif(ascii_vals[i]>65 and ascii_vals[i]>90): #If the values in the list are greater than 65 and 90, we get it back b/w 97 and 122 - ascii_vals[i] = 65 + (ascii_vals[i]-90) -1 - if(shift == "left"): #When the text it shifted to the right + ascii_vals[i] = ascii_vals[i] + val + if(ascii_vals[i]>97 and ascii_vals[i]>122): #If the values in the list are greater than 97 and 122, we get it back b/w 97 and 122 + ascii_vals[i] = 97 + (ascii_vals[i]-122) -1 + elif((ascii_vals[i]>97 and ascii_vals[i]<=122) or (ascii_vals[i]>65 and ascii_vals[i]<=90)): + ascii_vals[i] = ascii_vals[i] + elif(ascii_vals[i]>65 and ascii_vals[i]>90): + ascii_vals[i] = 65 + (ascii_vals[i]-90) -1 #If the values in the list are greater than 65 and 90, we get it back b/w 97 and 122 + if(shift == "left"): #When the text it shifted to the left for i in range(0,len(ascii_vals)): - if(ascii_vals[i] == 32): #Spaces are not ciphered - ascii_vals[i] = 32 - else: - ascii_vals[i] = ascii_vals[i] - val - print ascii_vals[i] - if(ascii_vals[i]<97 and ascii_vals[i]<122): #If the values in the list are greater than 97 and 122, we get it back b/w 97 and 122 - ascii_vals[i] = 122 + (ascii_vals[i]-97) +1 - print ascii_vals[i] - elif((ascii_vals[i]>97 and ascii_vals[i]<=122) or (ascii_vals[i]>65 and ascii_vals[i]<=90)): - ascii_vals[i] = ascii_vals[i] - elif(ascii_vals[i]<65 and ascii_vals[i]<90): #If the values in the list are greater than 65 and 90, we get it back b/w 97 and 122 - ascii_vals[i] = 90 + (ascii_vals[i]-65) +1 + ascii_vals[i] = ascii_vals[i] - val + if(ascii_vals[i]<97 and ascii_vals[i]<122): #If the values in the list are greater than 97 and 122, we get it back b/w 97 and 122 + ascii_vals[i] = 122 + (ascii_vals[i]-97) +1 + elif((ascii_vals[i]>97 and ascii_vals[i]<=122) or (ascii_vals[i]>65 and ascii_vals[i]<=90)): + ascii_vals[i] = ascii_vals[i] + elif(ascii_vals[i]<65 and ascii_vals[i]<90): #If the values in the list are greater than 65 and 90, we get it back b/w 97 and 122 + ascii_vals[i] = 90 + (ascii_vals[i]-65) +1 - encrypt_string = ''.join(chr(i) for i in ascii_vals) # To get the encrypted string using the list of manipulated ASCII vals - print encrypt_string - - - - decrypt_string = ''.join(chr(i) for i in ascii_vals) # To get the encrypted string using the list of manipulated ASCII vals - print decrypt_string + encrypt_or_decrypt_string = ''.join(chr(i) for i in ascii_vals) # To get the encrypted string using the list of manipulated ASCII vals + print encrypt_or_decrypt_string def caesar_cipher(): string = raw_input("Enter text to be encrypted:") encrypt_and_decrypt(string,shift,val) - if __name__=="__main__": shift = str(raw_input("Do you want to shift to right or to left?")) val = int(raw_input("By what value do you want to shift?")) From 6ece6c4f55894eab2f9b7d948e85311d392d4990 Mon Sep 17 00:00:00 2001 From: rahulkumaran Date: Sat, 2 Dec 2017 07:44:06 +0530 Subject: [PATCH 03/21] Update caesarcipher.py --- Cryptography/caesarcipher.py | 5 +---- 1 file changed, 1 insertion(+), 4 deletions(-) diff --git a/Cryptography/caesarcipher.py b/Cryptography/caesarcipher.py index 138a7cfe6..a78ca2928 100644 --- a/Cryptography/caesarcipher.py +++ b/Cryptography/caesarcipher.py @@ -6,8 +6,6 @@ The general purpose function takes care of both the left and the right shifts. Based on how the user uses it, he or she can encrypt and decrypt the data. """ - - def encrypt_and_decrypt(string,shift,val): ascii_vals=[ord(c) for c in string] #To get a list of ASCII Values of each character in string if(shift == "right"): #When the text it shifted to the right @@ -27,8 +25,7 @@ def encrypt_and_decrypt(string,shift,val): elif((ascii_vals[i]>97 and ascii_vals[i]<=122) or (ascii_vals[i]>65 and ascii_vals[i]<=90)): ascii_vals[i] = ascii_vals[i] elif(ascii_vals[i]<65 and ascii_vals[i]<90): #If the values in the list are greater than 65 and 90, we get it back b/w 97 and 122 - ascii_vals[i] = 90 + (ascii_vals[i]-65) +1 - + ascii_vals[i] = 90 + (ascii_vals[i]-65) +1 encrypt_or_decrypt_string = ''.join(chr(i) for i in ascii_vals) # To get the encrypted string using the list of manipulated ASCII vals print encrypt_or_decrypt_string From 743965d22e4617b3810445048a6c3cc07d130f18 Mon Sep 17 00:00:00 2001 From: rahulkumaran Date: Sat, 2 Dec 2017 07:49:37 +0530 Subject: [PATCH 04/21] Update caesarcipher.py --- Cryptography/caesarcipher.py | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/Cryptography/caesarcipher.py b/Cryptography/caesarcipher.py index a78ca2928..843db6329 100644 --- a/Cryptography/caesarcipher.py +++ b/Cryptography/caesarcipher.py @@ -6,8 +6,8 @@ The general purpose function takes care of both the left and the right shifts. Based on how the user uses it, he or she can encrypt and decrypt the data. """ -def encrypt_and_decrypt(string,shift,val): - ascii_vals=[ord(c) for c in string] #To get a list of ASCII Values of each character in string +def encrypt_and_decrypt(inp,shift,val): + ascii_vals=[ord(c) for c in inp] #To get a list of ASCII Values of each character in string if(shift == "right"): #When the text it shifted to the right for i in range(0,len(ascii_vals)): ascii_vals[i] = ascii_vals[i] + val @@ -30,10 +30,10 @@ def encrypt_and_decrypt(string,shift,val): print encrypt_or_decrypt_string def caesar_cipher(): - string = raw_input("Enter text to be encrypted:") + inp = raw_input("Enter text to be encrypted:") encrypt_and_decrypt(string,shift,val) if __name__=="__main__": - shift = str(raw_input("Do you want to shift to right or to left?")) + inp = str(raw_input("Do you want to shift to right or to left?")) val = int(raw_input("By what value do you want to shift?")) caesar_cipher() From 4d5416168956080ea8a577f16eb5a688062f23a6 Mon Sep 17 00:00:00 2001 From: rahulkumaran Date: Sat, 2 Dec 2017 13:35:21 +0530 Subject: [PATCH 05/21] Delete caesarcipher.py --- Cryptography/caesarcipher.py | 39 ------------------------------------ 1 file changed, 39 deletions(-) delete mode 100644 Cryptography/caesarcipher.py diff --git a/Cryptography/caesarcipher.py b/Cryptography/caesarcipher.py deleted file mode 100644 index 843db6329..000000000 --- a/Cryptography/caesarcipher.py +++ /dev/null @@ -1,39 +0,0 @@ -""" -A general purpose function for both, encryption and decryption. -After giving the string that you want, if you decide to shift it to right, -encryption will take place by shifting characters to the right and -the decrypted string can be obtained by shifting the encrypted string to the left. -The general purpose function takes care of both the left and the right shifts. -Based on how the user uses it, he or she can encrypt and decrypt the data. -""" -def encrypt_and_decrypt(inp,shift,val): - ascii_vals=[ord(c) for c in inp] #To get a list of ASCII Values of each character in string - if(shift == "right"): #When the text it shifted to the right - for i in range(0,len(ascii_vals)): - ascii_vals[i] = ascii_vals[i] + val - if(ascii_vals[i]>97 and ascii_vals[i]>122): #If the values in the list are greater than 97 and 122, we get it back b/w 97 and 122 - ascii_vals[i] = 97 + (ascii_vals[i]-122) -1 - elif((ascii_vals[i]>97 and ascii_vals[i]<=122) or (ascii_vals[i]>65 and ascii_vals[i]<=90)): - ascii_vals[i] = ascii_vals[i] - elif(ascii_vals[i]>65 and ascii_vals[i]>90): - ascii_vals[i] = 65 + (ascii_vals[i]-90) -1 #If the values in the list are greater than 65 and 90, we get it back b/w 97 and 122 - if(shift == "left"): #When the text it shifted to the left - for i in range(0,len(ascii_vals)): - ascii_vals[i] = ascii_vals[i] - val - if(ascii_vals[i]<97 and ascii_vals[i]<122): #If the values in the list are greater than 97 and 122, we get it back b/w 97 and 122 - ascii_vals[i] = 122 + (ascii_vals[i]-97) +1 - elif((ascii_vals[i]>97 and ascii_vals[i]<=122) or (ascii_vals[i]>65 and ascii_vals[i]<=90)): - ascii_vals[i] = ascii_vals[i] - elif(ascii_vals[i]<65 and ascii_vals[i]<90): #If the values in the list are greater than 65 and 90, we get it back b/w 97 and 122 - ascii_vals[i] = 90 + (ascii_vals[i]-65) +1 - encrypt_or_decrypt_string = ''.join(chr(i) for i in ascii_vals) # To get the encrypted string using the list of manipulated ASCII vals - print encrypt_or_decrypt_string - -def caesar_cipher(): - inp = raw_input("Enter text to be encrypted:") - encrypt_and_decrypt(string,shift,val) - -if __name__=="__main__": - inp = str(raw_input("Do you want to shift to right or to left?")) - val = int(raw_input("By what value do you want to shift?")) - caesar_cipher() From 65070fa6c55d913b4c22b5e077a0b291a08c641d Mon Sep 17 00:00:00 2001 From: rahulkumaran Date: Sat, 2 Dec 2017 13:41:05 +0530 Subject: [PATCH 06/21] Add files via upload --- Cryptography/caesarcipheropt.py | 42 +++++++++++++++++++++++++++++++++ 1 file changed, 42 insertions(+) create mode 100644 Cryptography/caesarcipheropt.py diff --git a/Cryptography/caesarcipheropt.py b/Cryptography/caesarcipheropt.py new file mode 100644 index 000000000..028354d9d --- /dev/null +++ b/Cryptography/caesarcipheropt.py @@ -0,0 +1,42 @@ +""" +A general purpose function for both, encryption and decryption. +After giving the string that you want, if you decide to shift it to right, +encryption will take place by shifting characters to the right and +the decrypted string can be obtained by shifting the encrypted string to the left. +The general purpose function takes care of both the left and the right shifts. +Based on how the user uses it, he or she can encrypt and decrypt the data. +""" + + +def encrypt(message,key): + ascii_vals=[ord(c) for c in message] #To get a list of ASCII Values of each character in string #When the text it shifted to the right + for i in range(0,len(ascii_vals)): + ascii_vals[i] = ascii_vals[i] + key + if(ascii_vals[i]>97 and ascii_vals[i]>122): #If the values in the list are greater than 97 and 122, we get it back b/w 97 and 122 + ascii_vals[i] = 97 + (ascii_vals[i]-122) -1 + elif((ascii_vals[i]>97 and ascii_vals[i]<=122) or (ascii_vals[i]>65 and ascii_vals[i]<=90)): + ascii_vals[i] = ascii_vals[i] + elif(ascii_vals[i]>65 and ascii_vals[i]>90): + ascii_vals[i] = 65 + (ascii_vals[i]-90) -1 #If the values in the list are greater than 65 and 90, we get it back b/w 97 and 122 +def decrypt(message,key): + ascii_vals=[ord(c) for c in message] #When the text it shifted to the right + for i in range(0,len(ascii_vals)): + ascii_vals[i] = ascii_vals[i] - key + if(ascii_vals[i]<97 and ascii_vals[i]<122): #If the values in the list are greater than 97 and 122, we get it back b/w 97 and 122 + ascii_vals[i] = 122 + (ascii_vals[i]-97) +1 + elif((ascii_vals[i]>97 and ascii_vals[i]<=122) or (ascii_vals[i]>65 and ascii_vals[i]<=90)): + ascii_vals[i] = ascii_vals[i] + elif(ascii_vals[i]<65 and ascii_vals[i]<90): #If the values in the list are greater than 65 and 90, we get it back b/w 97 and 122 + ascii_vals[i] = 90 + (ascii_vals[i]-65) +1 + + decrypt_string = ''.join(chr(i) for i in ascii_vals) # To get the encrypted string using the list of manipulated ASCII vals + print decrypt_string + +def caesar_cipher(): + message = raw_input("Enter text to be encrypted:") + encrypt(message,key) + decrypt(message,key) + +if __name__=="__main__": + key = int(raw_input("By what value do you want to shift?")) + caesar_cipher() From 6c8d15e456d44e32de0a0ef1e35e352f40546b76 Mon Sep 17 00:00:00 2001 From: rahulkumaran Date: Sat, 2 Dec 2017 13:47:25 +0530 Subject: [PATCH 07/21] Update caesarcipheropt.py --- Cryptography/caesarcipheropt.py | 13 ++++++++----- 1 file changed, 8 insertions(+), 5 deletions(-) diff --git a/Cryptography/caesarcipheropt.py b/Cryptography/caesarcipheropt.py index 028354d9d..6aee21009 100644 --- a/Cryptography/caesarcipheropt.py +++ b/Cryptography/caesarcipheropt.py @@ -9,8 +9,7 @@ def encrypt(message,key): - ascii_vals=[ord(c) for c in message] #To get a list of ASCII Values of each character in string #When the text it shifted to the right - for i in range(0,len(ascii_vals)): + ascii_vals=[ord(c) for c in message] #To get a list of ASCII Values of each character in string ascii_vals[i] = ascii_vals[i] + key if(ascii_vals[i]>97 and ascii_vals[i]>122): #If the values in the list are greater than 97 and 122, we get it back b/w 97 and 122 ascii_vals[i] = 97 + (ascii_vals[i]-122) -1 @@ -18,6 +17,8 @@ def encrypt(message,key): ascii_vals[i] = ascii_vals[i] elif(ascii_vals[i]>65 and ascii_vals[i]>90): ascii_vals[i] = 65 + (ascii_vals[i]-90) -1 #If the values in the list are greater than 65 and 90, we get it back b/w 97 and 122 + encrypt_string = ''.join(chr(i) for i in ascii_vals) # To get the encrypted string using the list of manipulated ASCII vals + return encrypt_string def decrypt(message,key): ascii_vals=[ord(c) for c in message] #When the text it shifted to the right for i in range(0,len(ascii_vals)): @@ -30,12 +31,14 @@ def decrypt(message,key): ascii_vals[i] = 90 + (ascii_vals[i]-65) +1 decrypt_string = ''.join(chr(i) for i in ascii_vals) # To get the encrypted string using the list of manipulated ASCII vals - print decrypt_string + return decrypt_string def caesar_cipher(): message = raw_input("Enter text to be encrypted:") - encrypt(message,key) - decrypt(message,key) + encrypted_string = encrypt(message,key) + decrypted_string = decrypt(message,key) + print encrypted_string + print decrypted_string if __name__=="__main__": key = int(raw_input("By what value do you want to shift?")) From e1417a7dac9c01dbd8354ab140c5dab46a2d616b Mon Sep 17 00:00:00 2001 From: rahulkumaran Date: Sat, 2 Dec 2017 13:56:06 +0530 Subject: [PATCH 08/21] Update caesarcipheropt.py --- Cryptography/caesarcipheropt.py | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/Cryptography/caesarcipheropt.py b/Cryptography/caesarcipheropt.py index 6aee21009..72c597298 100644 --- a/Cryptography/caesarcipheropt.py +++ b/Cryptography/caesarcipheropt.py @@ -9,7 +9,8 @@ def encrypt(message,key): - ascii_vals=[ord(c) for c in message] #To get a list of ASCII Values of each character in string + ascii_vals=[ord(c) for c in message] #To get a list of ASCII Values of each character in string + for i in range(0,len(ascii_vals)): ascii_vals[i] = ascii_vals[i] + key if(ascii_vals[i]>97 and ascii_vals[i]>122): #If the values in the list are greater than 97 and 122, we get it back b/w 97 and 122 ascii_vals[i] = 97 + (ascii_vals[i]-122) -1 From 6d4c36fc3c801f070c3104549e1a7740e564543b Mon Sep 17 00:00:00 2001 From: rahulkumaran Date: Sat, 2 Dec 2017 14:03:44 +0530 Subject: [PATCH 09/21] Delete caesarcipheropt.py --- Cryptography/caesarcipheropt.py | 46 --------------------------------- 1 file changed, 46 deletions(-) delete mode 100644 Cryptography/caesarcipheropt.py diff --git a/Cryptography/caesarcipheropt.py b/Cryptography/caesarcipheropt.py deleted file mode 100644 index 72c597298..000000000 --- a/Cryptography/caesarcipheropt.py +++ /dev/null @@ -1,46 +0,0 @@ -""" -A general purpose function for both, encryption and decryption. -After giving the string that you want, if you decide to shift it to right, -encryption will take place by shifting characters to the right and -the decrypted string can be obtained by shifting the encrypted string to the left. -The general purpose function takes care of both the left and the right shifts. -Based on how the user uses it, he or she can encrypt and decrypt the data. -""" - - -def encrypt(message,key): - ascii_vals=[ord(c) for c in message] #To get a list of ASCII Values of each character in string - for i in range(0,len(ascii_vals)): - ascii_vals[i] = ascii_vals[i] + key - if(ascii_vals[i]>97 and ascii_vals[i]>122): #If the values in the list are greater than 97 and 122, we get it back b/w 97 and 122 - ascii_vals[i] = 97 + (ascii_vals[i]-122) -1 - elif((ascii_vals[i]>97 and ascii_vals[i]<=122) or (ascii_vals[i]>65 and ascii_vals[i]<=90)): - ascii_vals[i] = ascii_vals[i] - elif(ascii_vals[i]>65 and ascii_vals[i]>90): - ascii_vals[i] = 65 + (ascii_vals[i]-90) -1 #If the values in the list are greater than 65 and 90, we get it back b/w 97 and 122 - encrypt_string = ''.join(chr(i) for i in ascii_vals) # To get the encrypted string using the list of manipulated ASCII vals - return encrypt_string -def decrypt(message,key): - ascii_vals=[ord(c) for c in message] #When the text it shifted to the right - for i in range(0,len(ascii_vals)): - ascii_vals[i] = ascii_vals[i] - key - if(ascii_vals[i]<97 and ascii_vals[i]<122): #If the values in the list are greater than 97 and 122, we get it back b/w 97 and 122 - ascii_vals[i] = 122 + (ascii_vals[i]-97) +1 - elif((ascii_vals[i]>97 and ascii_vals[i]<=122) or (ascii_vals[i]>65 and ascii_vals[i]<=90)): - ascii_vals[i] = ascii_vals[i] - elif(ascii_vals[i]<65 and ascii_vals[i]<90): #If the values in the list are greater than 65 and 90, we get it back b/w 97 and 122 - ascii_vals[i] = 90 + (ascii_vals[i]-65) +1 - - decrypt_string = ''.join(chr(i) for i in ascii_vals) # To get the encrypted string using the list of manipulated ASCII vals - return decrypt_string - -def caesar_cipher(): - message = raw_input("Enter text to be encrypted:") - encrypted_string = encrypt(message,key) - decrypted_string = decrypt(message,key) - print encrypted_string - print decrypted_string - -if __name__=="__main__": - key = int(raw_input("By what value do you want to shift?")) - caesar_cipher() From 35f90b82db3ae9ce7b715022f5cc3a7752319a92 Mon Sep 17 00:00:00 2001 From: rahulkumaran Date: Sat, 2 Dec 2017 14:04:00 +0530 Subject: [PATCH 10/21] Add files via upload --- Cryptography/caesarcipher1.py | 45 +++++++++++++++++++++++++++++++++++ 1 file changed, 45 insertions(+) create mode 100644 Cryptography/caesarcipher1.py diff --git a/Cryptography/caesarcipher1.py b/Cryptography/caesarcipher1.py new file mode 100644 index 000000000..b840c8fa7 --- /dev/null +++ b/Cryptography/caesarcipher1.py @@ -0,0 +1,45 @@ +""" +A general purpose function for both, encryption and decryption. +After giving the string that you want, if you decide to shift it to right, +encryption will take place by shifting characters to the right and +the decrypted string can be obtained by shifting the encrypted string to the left. +The general purpose function takes care of both the left and the right shifts. +Based on how the user uses it, he or she can encrypt and decrypt the data. +""" + + +def encrypt(message,key): + ascii_vals=[ord(c) for c in message] #To get a list of ASCII Values of each character in string + ascii_vals[i] = ascii_vals[i] + key + if(ascii_vals[i]>97 and ascii_vals[i]>122): #If the values in the list are greater than 97 and 122, we get it back b/w 97 and 122 + ascii_vals[i] = 97 + (ascii_vals[i]-122) -1 + elif((ascii_vals[i]>97 and ascii_vals[i]<=122) or (ascii_vals[i]>65 and ascii_vals[i]<=90)): + ascii_vals[i] = ascii_vals[i] + elif(ascii_vals[i]>65 and ascii_vals[i]>90): + ascii_vals[i] = 65 + (ascii_vals[i]-90) -1 #If the values in the list are greater than 65 and 90, we get it back b/w 97 and 122 + encrypt_string = ''.join(chr(i) for i in ascii_vals) # To get the encrypted string using the list of manipulated ASCII vals + return encrypt_string +def decrypt(message,key): + ascii_vals=[ord(c) for c in message] #When the text it shifted to the right + for i in range(0,len(ascii_vals)): + ascii_vals[i] = ascii_vals[i] - key + if(ascii_vals[i]<97 and ascii_vals[i]<122): #If the values in the list are greater than 97 and 122, we get it back b/w 97 and 122 + ascii_vals[i] = 122 + (ascii_vals[i]-97) +1 + elif((ascii_vals[i]>97 and ascii_vals[i]<=122) or (ascii_vals[i]>65 and ascii_vals[i]<=90)): + ascii_vals[i] = ascii_vals[i] + elif(ascii_vals[i]<65 and ascii_vals[i]<90): #If the values in the list are greater than 65 and 90, we get it back b/w 97 and 122 + ascii_vals[i] = 90 + (ascii_vals[i]-65) +1 + + decrypt_string = ''.join(chr(i) for i in ascii_vals) # To get the encrypted string using the list of manipulated ASCII vals + return decrypt_string + +def caesar_cipher(): + message = raw_input("Enter text to be encrypted:") + encrypted_string = encrypt(message,key) + decrypted_string = decrypt(message,key) + print encrypted_string + print decrypted_string + +if __name__=="__main__": + key = int(raw_input("By what value do you want to shift?")) +caesar_cipher() From d31a2907990de13c59fa865ccfcd3b8c3c4c2531 Mon Sep 17 00:00:00 2001 From: rahulkumaran Date: Sat, 2 Dec 2017 14:05:48 +0530 Subject: [PATCH 11/21] Update caesarcipher1.py --- Cryptography/caesarcipher1.py | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/Cryptography/caesarcipher1.py b/Cryptography/caesarcipher1.py index b840c8fa7..12d8dce69 100644 --- a/Cryptography/caesarcipher1.py +++ b/Cryptography/caesarcipher1.py @@ -9,7 +9,8 @@ def encrypt(message,key): - ascii_vals=[ord(c) for c in message] #To get a list of ASCII Values of each character in string + ascii_vals=[ord(c) for c in message] #To get a list of ASCII Values of each character in string + for i in range(0,len(ascii_vals)): ascii_vals[i] = ascii_vals[i] + key if(ascii_vals[i]>97 and ascii_vals[i]>122): #If the values in the list are greater than 97 and 122, we get it back b/w 97 and 122 ascii_vals[i] = 97 + (ascii_vals[i]-122) -1 From 770238ad5f9a60a9265c1710c700c71777654ada Mon Sep 17 00:00:00 2001 From: rahulkumaran Date: Sat, 2 Dec 2017 14:09:48 +0530 Subject: [PATCH 12/21] Update caesarcipher1.py --- Cryptography/caesarcipher1.py | 8 ++------ 1 file changed, 2 insertions(+), 6 deletions(-) diff --git a/Cryptography/caesarcipher1.py b/Cryptography/caesarcipher1.py index 12d8dce69..ec5adac47 100644 --- a/Cryptography/caesarcipher1.py +++ b/Cryptography/caesarcipher1.py @@ -1,16 +1,12 @@ """ -A general purpose function for both, encryption and decryption. -After giving the string that you want, if you decide to shift it to right, -encryption will take place by shifting characters to the right and +A caesar cipher function for both, encryption and decryption. +After giving the string that you want, encryption will take place by shifting characters to the right and the decrypted string can be obtained by shifting the encrypted string to the left. -The general purpose function takes care of both the left and the right shifts. -Based on how the user uses it, he or she can encrypt and decrypt the data. """ def encrypt(message,key): ascii_vals=[ord(c) for c in message] #To get a list of ASCII Values of each character in string - for i in range(0,len(ascii_vals)): ascii_vals[i] = ascii_vals[i] + key if(ascii_vals[i]>97 and ascii_vals[i]>122): #If the values in the list are greater than 97 and 122, we get it back b/w 97 and 122 ascii_vals[i] = 97 + (ascii_vals[i]-122) -1 From eddd0a6382451583803a7dfe629b78a1ad9185c8 Mon Sep 17 00:00:00 2001 From: rahulkumaran Date: Sat, 2 Dec 2017 14:17:09 +0530 Subject: [PATCH 13/21] Update caesarcipher1.py --- Cryptography/caesarcipher1.py | 3 +++ 1 file changed, 3 insertions(+) diff --git a/Cryptography/caesarcipher1.py b/Cryptography/caesarcipher1.py index ec5adac47..0a16f7f10 100644 --- a/Cryptography/caesarcipher1.py +++ b/Cryptography/caesarcipher1.py @@ -7,6 +7,8 @@ def encrypt(message,key): ascii_vals=[ord(c) for c in message] #To get a list of ASCII Values of each character in string + i=0 + while(i97 and ascii_vals[i]>122): #If the values in the list are greater than 97 and 122, we get it back b/w 97 and 122 ascii_vals[i] = 97 + (ascii_vals[i]-122) -1 @@ -14,6 +16,7 @@ def encrypt(message,key): ascii_vals[i] = ascii_vals[i] elif(ascii_vals[i]>65 and ascii_vals[i]>90): ascii_vals[i] = 65 + (ascii_vals[i]-90) -1 #If the values in the list are greater than 65 and 90, we get it back b/w 97 and 122 + i+=1 encrypt_string = ''.join(chr(i) for i in ascii_vals) # To get the encrypted string using the list of manipulated ASCII vals return encrypt_string def decrypt(message,key): From 9637e875d15137c483f283d0668b0e046ead548d Mon Sep 17 00:00:00 2001 From: rahulkumaran Date: Sat, 2 Dec 2017 16:11:02 +0530 Subject: [PATCH 14/21] Delete caesarcipher1.py --- Cryptography/caesarcipher1.py | 45 ----------------------------------- 1 file changed, 45 deletions(-) delete mode 100644 Cryptography/caesarcipher1.py diff --git a/Cryptography/caesarcipher1.py b/Cryptography/caesarcipher1.py deleted file mode 100644 index 0a16f7f10..000000000 --- a/Cryptography/caesarcipher1.py +++ /dev/null @@ -1,45 +0,0 @@ -""" -A caesar cipher function for both, encryption and decryption. -After giving the string that you want, encryption will take place by shifting characters to the right and -the decrypted string can be obtained by shifting the encrypted string to the left. -""" - - -def encrypt(message,key): - ascii_vals=[ord(c) for c in message] #To get a list of ASCII Values of each character in string - i=0 - while(i97 and ascii_vals[i]>122): #If the values in the list are greater than 97 and 122, we get it back b/w 97 and 122 - ascii_vals[i] = 97 + (ascii_vals[i]-122) -1 - elif((ascii_vals[i]>97 and ascii_vals[i]<=122) or (ascii_vals[i]>65 and ascii_vals[i]<=90)): - ascii_vals[i] = ascii_vals[i] - elif(ascii_vals[i]>65 and ascii_vals[i]>90): - ascii_vals[i] = 65 + (ascii_vals[i]-90) -1 #If the values in the list are greater than 65 and 90, we get it back b/w 97 and 122 - i+=1 - encrypt_string = ''.join(chr(i) for i in ascii_vals) # To get the encrypted string using the list of manipulated ASCII vals - return encrypt_string -def decrypt(message,key): - ascii_vals=[ord(c) for c in message] #When the text it shifted to the right - for i in range(0,len(ascii_vals)): - ascii_vals[i] = ascii_vals[i] - key - if(ascii_vals[i]<97 and ascii_vals[i]<122): #If the values in the list are greater than 97 and 122, we get it back b/w 97 and 122 - ascii_vals[i] = 122 + (ascii_vals[i]-97) +1 - elif((ascii_vals[i]>97 and ascii_vals[i]<=122) or (ascii_vals[i]>65 and ascii_vals[i]<=90)): - ascii_vals[i] = ascii_vals[i] - elif(ascii_vals[i]<65 and ascii_vals[i]<90): #If the values in the list are greater than 65 and 90, we get it back b/w 97 and 122 - ascii_vals[i] = 90 + (ascii_vals[i]-65) +1 - - decrypt_string = ''.join(chr(i) for i in ascii_vals) # To get the encrypted string using the list of manipulated ASCII vals - return decrypt_string - -def caesar_cipher(): - message = raw_input("Enter text to be encrypted:") - encrypted_string = encrypt(message,key) - decrypted_string = decrypt(message,key) - print encrypted_string - print decrypted_string - -if __name__=="__main__": - key = int(raw_input("By what value do you want to shift?")) -caesar_cipher() From ebe422176490038255d070f312f85248cef226b2 Mon Sep 17 00:00:00 2001 From: rahulkumaran Date: Sat, 2 Dec 2017 16:11:22 +0530 Subject: [PATCH 15/21] Add files via upload --- Cryptography/Caesarcipher.py | 34 ++++++++++++++++++++++++++++++++++ 1 file changed, 34 insertions(+) create mode 100644 Cryptography/Caesarcipher.py diff --git a/Cryptography/Caesarcipher.py b/Cryptography/Caesarcipher.py new file mode 100644 index 000000000..426df1e7f --- /dev/null +++ b/Cryptography/Caesarcipher.py @@ -0,0 +1,34 @@ +""" +A Caesar Cipher function for both, encryption and decryption. +After giving the string that you want, encryption will take place by shifting +characters to the right and the decrypted string can be obtained by shifting +the encrypted string to the left. +The cipher is ready for Capital Letters only +""" + +def encrypt(message,key): + ascii_vals_enc=[ord(c) for c in message] #To get a list of ASCII Values of each character in string + for i in range(0,len(ascii_vals_enc)): + if(ascii_vals_enc[i]>=65 and ascii_vals_enc[i]<=90): + ascii_vals_enc[i] = 65 + (ascii_vals_enc[i] + key - 91)%26 #If the values in the list are greater than 65 and 90, we get it back b/w 65 and 90 + encrypt_string = ''.join(chr(i) for i in ascii_vals_enc) # To get the encrypted string using the list of manipulated ASCII vals + return encrypt_string + +def decrypt(message,key): + ascii_vals_dec=[ord(c) for c in message] + for i in range(0,len(ascii_vals_dec)): + if(ascii_vals_dec[i]>=65 and ascii_vals_dec[i]<=90): + ascii_vals_dec[i] = 90 - (90 - ascii_vals_dec[i] + key )%26 + decrypt_string = ''.join(chr(i) for i in ascii_vals_dec) # To get the decrypted string using the list of manipulated ASCII vals + return decrypt_string + +def caesar_cipher(): + message = raw_input("Enter text to be encrypted:") + encrypt_string = encrypt(message,key) + decrypt_string = decrypt(encrypt_string,key) + print encrypt_string + print decrypt_string + +if __name__=="__main__": + key = int(raw_input("By what value do you want to shift?")) + caesar_cipher() From f38609fb8a148cc1ffd1d551f8a4b5d0fe9c5de0 Mon Sep 17 00:00:00 2001 From: rahulkumaran Date: Sat, 2 Dec 2017 16:21:30 +0530 Subject: [PATCH 16/21] Cleared whitespaces --- Cryptography/Caesarcipher.py | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/Cryptography/Caesarcipher.py b/Cryptography/Caesarcipher.py index 426df1e7f..9451e2fae 100644 --- a/Cryptography/Caesarcipher.py +++ b/Cryptography/Caesarcipher.py @@ -1,20 +1,20 @@ """ A Caesar Cipher function for both, encryption and decryption. After giving the string that you want, encryption will take place by shifting -characters to the right and the decrypted string can be obtained by shifting +characters to the right and the decrypted string can be obtained by shifting the encrypted string to the left. The cipher is ready for Capital Letters only """ -def encrypt(message,key): - ascii_vals_enc=[ord(c) for c in message] #To get a list of ASCII Values of each character in string +def encrypt(message,key): + ascii_vals_enc=[ord(c) for c in message] #To get a list of ASCII Values of each character in string for i in range(0,len(ascii_vals_enc)): if(ascii_vals_enc[i]>=65 and ascii_vals_enc[i]<=90): ascii_vals_enc[i] = 65 + (ascii_vals_enc[i] + key - 91)%26 #If the values in the list are greater than 65 and 90, we get it back b/w 65 and 90 encrypt_string = ''.join(chr(i) for i in ascii_vals_enc) # To get the encrypted string using the list of manipulated ASCII vals return encrypt_string - -def decrypt(message,key): + +def decrypt(message,key): ascii_vals_dec=[ord(c) for c in message] for i in range(0,len(ascii_vals_dec)): if(ascii_vals_dec[i]>=65 and ascii_vals_dec[i]<=90): @@ -24,7 +24,7 @@ def decrypt(message,key): def caesar_cipher(): message = raw_input("Enter text to be encrypted:") - encrypt_string = encrypt(message,key) + encrypt_string = encrypt(message,key) decrypt_string = decrypt(encrypt_string,key) print encrypt_string print decrypt_string From 24c9192da38140bc24c6fc816c7bba7e73f69fc9 Mon Sep 17 00:00:00 2001 From: rahulkumaran Date: Sat, 2 Dec 2017 16:26:55 +0530 Subject: [PATCH 17/21] Update Caesarcipher.py --- Cryptography/Caesarcipher.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/Cryptography/Caesarcipher.py b/Cryptography/Caesarcipher.py index 9451e2fae..77224c09d 100644 --- a/Cryptography/Caesarcipher.py +++ b/Cryptography/Caesarcipher.py @@ -21,14 +21,14 @@ def decrypt(message,key): ascii_vals_dec[i] = 90 - (90 - ascii_vals_dec[i] + key )%26 decrypt_string = ''.join(chr(i) for i in ascii_vals_dec) # To get the decrypted string using the list of manipulated ASCII vals return decrypt_string - + def caesar_cipher(): message = raw_input("Enter text to be encrypted:") encrypt_string = encrypt(message,key) decrypt_string = decrypt(encrypt_string,key) print encrypt_string print decrypt_string - + if __name__=="__main__": key = int(raw_input("By what value do you want to shift?")) caesar_cipher() From 4370066e085976a09a87d7c7afb3b50645621062 Mon Sep 17 00:00:00 2001 From: rahulkumaran Date: Sat, 2 Dec 2017 17:14:01 +0530 Subject: [PATCH 18/21] Code for Shell Sort --- sort/shellsort.py | 29 +++++++++++++++++++++++++++++ 1 file changed, 29 insertions(+) create mode 100644 sort/shellsort.py diff --git a/sort/shellsort.py b/sort/shellsort.py new file mode 100644 index 000000000..9c0011d6c --- /dev/null +++ b/sort/shellsort.py @@ -0,0 +1,29 @@ +""" +The algorithm uses a procedure called Shell Sort +to sort the lists. It breaks the list to smaller sublists +and each sublist is further sorted by insertion sort. +The sublists are made using a gap and elements at gap i +is often selected and forms a sublist. +""" + +def shellSort(test): + sublistcount = len(test)//2 #Finds gap to create sublists + while(sublistcount > 0): + for startposition in range(sublistcount): + gapInsertionSort(test,startposition,sublistcount) + print("After increments of size",sublistcount,"The list is",test) + sublistcount = sublistcount // 2 + +def gapInsertionSort(test,start,gap): + for i in range(start+gap,len(test),gap): + currentvalue = test[i] + position = i + while(position>=gap and test[position-gap]>currentvalue): + test[position]=test[position-gap] + position = position-gap + test[position]=currentvalue + +if __name__=="__main__": + test = [99,109,29,89,44,98,20,55,31] + shellSort(test) + print test From 8794cbcf3c0072fe390dc26387c7a11d14407f7e Mon Sep 17 00:00:00 2001 From: rahulkumaran Date: Sat, 2 Dec 2017 20:48:32 +0530 Subject: [PATCH 19/21] Create bit manipulation --- bit manipulation | 1 + 1 file changed, 1 insertion(+) create mode 100644 bit manipulation diff --git a/bit manipulation b/bit manipulation new file mode 100644 index 000000000..a2129b584 --- /dev/null +++ b/bit manipulation @@ -0,0 +1 @@ +Algorithms to do bit manipulations in order to reduce the amount of space some variable takes. From dc1a13aaedcf560753c3daf7b401c3bbe4774ab7 Mon Sep 17 00:00:00 2001 From: rahulkumaran Date: Sat, 2 Dec 2017 20:53:12 +0530 Subject: [PATCH 20/21] Delete bit manipulation --- bit manipulation | 1 - 1 file changed, 1 deletion(-) delete mode 100644 bit manipulation diff --git a/bit manipulation b/bit manipulation deleted file mode 100644 index a2129b584..000000000 --- a/bit manipulation +++ /dev/null @@ -1 +0,0 @@ -Algorithms to do bit manipulations in order to reduce the amount of space some variable takes. From a730e7d54e822e24ab329aa97e641b405aee0155 Mon Sep 17 00:00:00 2001 From: rahulkumaran Date: Sat, 2 Dec 2017 20:54:23 +0530 Subject: [PATCH 21/21] Code for bit manipulations --- bitmanip.c | 50 ++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 50 insertions(+) create mode 100644 bitmanip.c diff --git a/bitmanip.c b/bitmanip.c new file mode 100644 index 000000000..2f376f732 --- /dev/null +++ b/bitmanip.c @@ -0,0 +1,50 @@ +#include +#define baseyear 2000 + +void main() +{ + short int date; + short int temp,retrieve; + + + printf("Enter Date:\n"); + scanf("%d",&temp); + date=temp; + + printf("Enter Month:\n"); + scanf("%d",&temp); + date=date|(temp<<5); + + /*printf("%d\n",date); + printf("%d %d",temp,retrieve);*/ + + printf("Enter Year:\n"); + scanf("%d",&temp); + date=date|(temp-baseyear)<<9; + + printf("The total date information stored in 2 bytes has been encoded as follows: %d\n",date); + + + retrieve=(date<<11); + retrieve=retrieve>>11; + if(retrieve<0) + printf("The date is : %d\n",32+retrieve); + else + printf("The date is : %d\n",retrieve); + + + retrieve=date<<7; + retrieve=retrieve>>12; + if(retrieve<0) + printf("The month is : %d\n",16+retrieve); + else + printf("The month is : %d\n",retrieve); + + + retrieve=date>>9; + if(retrieve<0) + printf("The year is : %d\n",128+retrieve+baseyear); + else + printf("The year is : %d\n",retrieve+baseyear); + +}