Skip to content

Commit e361a41

Browse files
authored
Merge pull request #330 from shreyan-naskar/master
Encoder and Decoder for .txt files
2 parents 105e27a + 5a0ad09 commit e361a41

File tree

2 files changed

+91
-0
lines changed

2 files changed

+91
-0
lines changed
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
# ENCRYPT AND DECRYPT DATA IN A TEXT FILE IN PYTHON
2+
3+
- Menu driven script.
4+
- Enables user to store data using personalised encryption key.
5+
- Allowed user to decrypt and view data given correct key.
Lines changed: 86 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,86 @@
1+
Pwd_Key = 4 #modify to set personalised Key
2+
3+
#Function to Encode the user given data
4+
def Cipher(Text) :
5+
6+
Cipher_Text = ''
7+
for i in Text:
8+
a = chr(ord(i) + Pwd_Key )
9+
Cipher_Text += a
10+
11+
T = Cipher_Text + '\n'
12+
return T
13+
14+
#Function to store the encrypted data.
15+
def Entry(Data):
16+
file = open('Data.txt', 'a')
17+
file.write(Data)
18+
print('\nEntered data has been successfully encrypted and recorded..!!!\n')
19+
file.close()
20+
'''
21+
22+
print(En_Data)
23+
'''
24+
#Function to display the encrypted data.
25+
def Extract():
26+
27+
En_Records,De_Records = [],[]
28+
29+
file = open('Data.txt', 'r')
30+
x = file.readlines()
31+
32+
for i in x:
33+
En_Records.append(i)
34+
35+
for i in range(0,len(En_Records)):
36+
En_Text = En_Records[i]
37+
De_Text = ""
38+
39+
for j in range(0, len(En_Text)):
40+
De_Text += (chr(ord(En_Text[j]) - Pwd_Key))
41+
42+
De_Records.append(De_Text)
43+
44+
file.close()
45+
46+
return De_Records
47+
48+
49+
while(True):
50+
print(" 1 -> Enter Data ")
51+
print(" 2 -> Display Stored Data ")
52+
print(" 0 -> Exit \n")
53+
54+
opt = int(input("Enter the option : "))
55+
print("\n")
56+
57+
if opt == 1 :
58+
Data = input("Enter your data :\n")
59+
En_Data = Cipher(Data)
60+
Entry(En_Data)
61+
62+
elif opt == 2 :
63+
user_key = int(input("Enter the Key to decrypt : "))
64+
65+
if user_key == Pwd_Key :
66+
L = Extract()
67+
for i in range(0,len(L)):
68+
print(L[i][:-1])
69+
print("\n")
70+
71+
else :
72+
print("Wrong key !! ")
73+
74+
elif opt == 0 :
75+
exit()
76+
77+
else :
78+
print("Enter valid option !!")
79+
80+
81+
82+
83+
84+
85+
86+

0 commit comments

Comments
 (0)