-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcaesarCipher.py
108 lines (81 loc) · 3.63 KB
/
caesarCipher.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
#!/usr/bin/python
###=====================================================================###
### Author: Avinash Ghadshi ###
### Language Support: Python 2.7 ###
### Repository: https://github.com/avinash-ghadshi/cryptographyScripts ###
### Details: This script demonstrates the working of Caesor Cipher ###
### Usage: ###
### python caesarCipher.py --help ###
### python caesarCipher.py -t 'Hello World' -k 3 -a 1 ###
### python caesarCipher.py --text 'Khoor Zruog' --key 3 --action 2 ###
###=====================================================================###
import optparse
import re, sys
def get_input(parser):
parser.add_option("-t", "--text", dest="text", help="text string to convert")
parser.add_option("-k", "--key", dest="key", help="key must be integer and between 1 to 25")
parser.add_option("-a", "--action", dest="action", help="1: encryption, 2: Decryption")
(options, aurgumets) = parser.parse_args()
if not options.text or not options.key or not options.action:
parser.error("[-] Please specify all options, use --help for more info.")
return options
def initialize():
global smallA, smallZ, capsA, capsZ
smallA = ord('a')
smallZ = ord('z')
capsA = ord('A')
capsZ = ord('Z')
#print(str(smallA)+"\t"+str(smallZ)+"\t"+str(capsA)+"\t"+str(capsZ))
def encrypt(options):
if re.match(r'^[a-zA-Z\s]+$', options.text) == None:
print("[-] Text should not contains numbers or special characters other than space/tab.")
sys.exit()
aEncryptedText = list()
key = int(options.key)
for x in options.text:
if ord(x) == 9 or ord(x) == 32:
aEncryptedText.append(x)
elif ord(x) >= smallA and ord(x) <= smallZ:
if ord(x) + key > smallZ:
aEncryptedText.append(chr(ord(x) + key - 26 ))
else:
aEncryptedText.append(chr(ord(x) + key))
elif ord(x) >= capsA and ord(x) <= capsZ:
if ord(x) + key > capsZ:
aEncryptedText.append(chr(ord(x) + key - 26 ))
else:
aEncryptedText.append(chr(ord(x) + key))
sEncryptedText = ''.join(aEncryptedText)
print("[+] Plain Text = "+options.text)
print("[+] Encrypted Text = "+sEncryptedText)
def decrypt(options):
if re.match(r'^[a-zA-Z\s]+$', options.text) == None:
print("[-] Text should not contains numbers or special characters other than space/tab.")
sys.exit()
aDecryptedText = list()
key = int(options.key)
for x in options.text:
if ord(x) == 9 or ord(x) == 32:
aDecryptedText.append(x)
elif ord(x) >= smallA and ord(x) <= smallZ:
if ord(x) - key < smallA:
aDecryptedText.append(chr(ord(x) - key + 26 ))
else:
aDecryptedText.append(chr(ord(x) - key))
elif ord(x) >= capsA and ord(x) <= capsZ:
if ord(x) - key < capsA:
aDecryptedText.append(chr(ord(x) - key + 26 ))
else:
aDecryptedText.append(chr(ord(x) - key))
sDecryptedText = ''.join(aDecryptedText)
print("[+] Encrypted Text = "+options.text)
print("[+] Plain Text = "+sDecryptedText)
parser = optparse.OptionParser()
options = get_input(parser)
initialize()
if options.action == "1":
encrypt(options)
elif options.action == "2":
decrypt(options)
else:
parser.error("[-] Please specify proper action, use --help for more info.")