-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathScript.py
160 lines (123 loc) · 4.6 KB
/
Script.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
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
# Symmetric Cryptographic Encryption and Decryption in Python.
# done by @Sri_Programmer
# Python v3.7.0
__author__ = 'Sri Manikanta Palakollu'
# imports
import sys
import os
captial_letter = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ' # [A-Z]
small_letter = 'abcdefghijklmnopqrstuvwxyz' # [a-z]
special_characters = "[@_!#$%^&*()<>?/|\} {~:+-,.';]"
key = 6
def Encryption(filename): # Encrypts the file
print('Enter the data into the file to Encrypt :')
print('Press CTRL+D After giving the input to be encrypted :)')
user_input = sys.stdin.readlines() # CTRL + D to break the input
encrypted_file_name = original_information_filename + "_ciphertext"
# Creating a new file object to store the data into the file
try:
encrypt_file = open(original_information_filename,'w+')
for data in user_input: # writing the data into the file
encrypt_file.write(data)
encrypt_file = open(original_information_filename,'r')
cipher_text = open(encrypted_file_name,'w+')
for line in encrypt_file:
Encrypted_value = ''
for character in line:
if character.isupper() == True:
ele_position = captial_letter.find(character)
ele_position = (ele_position + key)%26
Encrypted_value += captial_letter[ele_position]
elif character.islower() == True:
ele_position = small_letter.find(character)
ele_position = (ele_position + key)%26
Encrypted_value += small_letter[ele_position]
elif character.isnumeric() == True:
new_val = (int(character) + key)%10
Encrypted_value += str(new_val)
else:
ele_position = special_characters.find(character)
ele_position = (ele_position + key)%30
Encrypted_value += special_characters[ele_position]
cipher_text.write(Encrypted_value)
cipher_text.write('\n')
except (FileNotFoundError, IOError):
print('Required files not found.')
sys.exit(0)
finally:
encrypt_file.close()
cipher_text.close()
print('Do you want to keep the original data file [y/n]')
while True:
result = input()
if (result == 'y' or result == 'Y'):
sys.exit(0)
elif (result == 'n' or result == 'N'):
os.remove(original_information_filename)
break
else:
print('Enter a valid choice:')
def Decryption(encrypted_file): # produces the original result
try:
encrypted_file_object = open(encrypted_file,'r')
except (FileNotFoundError,IOError):
print('File with name {} is not found'.format(encrypted_file))
sys.exit(0)
decrypted_file = input('Enter the Decryption filename to store the data:') # Decrypted file as output
decrypted_file_object = open(decrypted_file,'w+')
try:
for line in encrypted_file_object: # To read data from the file
Decryted_value = ''
line = line[:-2]
for character in line:
if character.isupper() == True:
ele_position = captial_letter.find(character)
ele_position = (ele_position - key)%26
Decryted_value += captial_letter[ele_position]
elif character.islower() == True:
ele_position = small_letter.find(character)
ele_position = (ele_position - key)%26
Decryted_value += small_letter[ele_position]
elif character.isnumeric() == True:
new_val = (int(character) - key)%10
Decryted_value += str(new_val)
else:
ele_position = special_characters.find(character)
ele_position = (ele_position - key)%30
Decryted_value += special_characters[ele_position]
decrypted_file_object.writelines(Decryted_value)
decrypted_file_object.write('\n')
except Exception:
print('Some problem Ciphertext unable to handle.')
finally:
encrypted_file_object.close()
decrypted_file_object.close()
# Program Start point
if __name__ == "__main__":
while True:
print('Enter your choice:')
print('1. Encryption')
print('2. Decryption')
choice = int(input('Select your Choice:'))
if(choice == 1):
print(''' ___ _ _
| __|_ _ __ _ _ _ _ _ __| |_(_)___ _ _
| _|| ' \/ _| '_| || | '_ \ _| / _ \ ' \
|___|_||_\__|_| \_, | .__/\__|_\___/_||_|
|__/|_|
''')
original_information_filename = input('Enter the filename to store Original information:')
Encryption(original_information_filename)
break
elif(choice == 2):
print(''' ___ _ _
| \ ___ __ _ _ _ _ _ __| |_(_)___ _ _
| |) / -_) _| '_| || | '_ \ _| / _ \ ' \
|___/\___\__|_| \_, | .__/\__|_\___/_||_|
|__/|_|
''')
encrypted_file = input('Enter the Ciphertext file name with Extension:') # Encrypted file as input
Decryption(encrypted_file)
break
else:
print('Please Enter a valid choice:')