-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBasic_Text_Encryption_and_Decryption.py
More file actions
74 lines (58 loc) · 2.55 KB
/
Basic_Text_Encryption_and_Decryption.py
File metadata and controls
74 lines (58 loc) · 2.55 KB
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
def encrypt(msg, n):
encrypted_msg = ""
for char in msg:
if char.isalpha():
base = ord('a') if char.islower() else ord('A')
shifted = chr((ord(char) - base + n) % 26 + base)
encrypted_msg += shifted
else:
encrypted_msg += char
return encrypted_msg
def decrypt(encrypted_msg, n):
return encrypt(encrypted_msg, -n)
def encrypt_file(input_file, output_file, n):
try:
with open(input_file, 'r') as file:
content = file.read()
encrypted_content = encrypt(content, n)
with open(output_file, 'w') as file:
file.write(encrypted_content)
print(f"Encryption successful! Encrypted content saved to {output_file}.")
except FileNotFoundError:
print(f"File {input_file} not found!")
except PermissionError:
print(f"Permission denied for {input_file}!")
def decrypt_file(input_file, output_file, n):
try:
with open(input_file, 'r') as file:
content = file.read()
decrypted_content = decrypt(content, n)
with open(output_file, 'w') as file:
file.write(decrypted_content)
print(f"Decryption successful! Decrypted content saved to {output_file}.")
except FileNotFoundError:
print(f"File {input_file} not found!")
except PermissionError:
print(f"Permission denied for {input_file}!")
def letter_frequency(encrypted_msg):
frequency = {}
for char in encrypted_msg:
if char.isalpha():
frequency[char] = frequency.get(char, 0) + 1
return frequency
message = input("Enter the message to encrypt: ")
key = int(input("Enter the key (shift value): "))
encrypted_message = encrypt(message, key)
print(f"Encrypted message: {encrypted_message}")
decrypted_message = decrypt(encrypted_message, key)
print(f"Decrypted message: {decrypted_message}")
input_file_name_encrypt = input("Enter the full path of the input file to encrypt: ")
output_file_name_encrypt = input("Enter the full path of the output file for encryption: ")
encrypt_file(input_file_name_encrypt, output_file_name_encrypt, key)
input_file_name_decrypt = input("Enter the full path of the input file to decrypt: ")
output_file_name_decrypt = input("Enter the full path of the output file for decryption: ")
decrypt_file(input_file_name_decrypt, output_file_name_decrypt, key)
freq = letter_frequency(encrypted_message)
print("Frequency of each letter in the encrypted message:")
for char, count in freq.items():
print(f"{char}: {count}")