Dec 03, 2024 | 658 words | 7 min read
8.3.2. Task 2#
Learning Objectives:#
Implement and apply classic encryption techniques such as the Atbash cipher.
Utilize file I/O functions in Python for reading and writing text data.
Develop an understanding of the concept of data encryption and its applications.
Introduction#
Encryption remains a cornerstone of secure communication, both historically and in the modern digital world. By learning to implement classical encryption techniques such as the Atbash cipher, students can grasp foundational concepts of data security. Python, with its simplicity and robustness, serves as an excellent tool for exploring these encryption methods.
What is a Cipher?#
A cipher is a method for performing encryption or decryption – a series of well-defined steps that can be followed as a procedure. An algorithm, called a cipher algorithm, is used to transform the text to make it unrecognizable (encrypt it). The same or a different algorithm may be used to return the text to its original form (decrypt it).
How the Atbash Cipher Works#
The Atbash cipher is a very specific case of a substitution cipher where the letters of the alphabet are reversed. In other words, the first letter becomes the last letter, the second letter becomes the second to last letter, and so on.
For the English alphabet, this would mean:
A is substituted with Z
B is substituted with Y
C is substituted with X
… and so forth up to Z being substituted with A.
For digits, it similarly reverses the order:
0 is substituted with 9
1 with 8
2 with 7
… continuing in this pattern.
Task Instructions#
Develop a Python program that asks the user for the name of a text file,
applies the Atbash cipher to generate encrypted text from the content of the named file,
and then writes the encrypted text to atbash_cipher.txt
. You can use
secret.txt
and empty.txt
as a sample input files for testing
your program.
Your program should include a function named atbash_cipher
that takes a plaintext
string as its only argument and returns an encrypted text string. The function should
apply the Atbash cipher to the input text, reversing the alphabet and numeric characters
according to the cipher rules provide above.
Save the flowchart of your program as a separate page in py4_ind_username.pdf. Save your Python file as py4_ind_2_username.py.
Hints on Slicing Syntax
In Python, the line reversed_alphabets = alphabets[::-1]
uses slicing to
reverse the string alphabets
. Let’s break down the concept step-by-step:
Python Slicing Syntax
Slicing in Python is a way to extract portions of a sequence (like a string,
list, or tuple). The syntax is: sequence[start:stop:step]
Where:
start
: The starting index of the slice (inclusive).stop
: The ending index of the slice (exclusive).step
: The step size for slicing.
How Slicing Works in alphabets[::-1]
In the expression alphabets[::-1]
:
alphabets
: This is the original string,"ABCDEFGHIJKLMNOPQRSTUVWXYZ"
.start
: Omitted, which means the slicing starts from the beginning.stop
: Also omitted, which means the slicing goes until the end of the string.step
: Set to-1
, meaning the slicing is done in reverse order.
Breaking It Down
When step
is -1
:
The slicing starts at the last character (
'Z'
) and proceeds backward to the first character ('A'
).The result is a new string with characters reversed:
"ZYXWVUTSRQPONMLKJIHGFEDCBA"
.
Example
Let’s illustrate this with a smaller example:
Suppose we have a string
alphabets = "ABC"
.If you apply
alphabets[::-1]
, the result will be"CBA"
.
Why This Works
The [::-1]
slicing method is a concise way to reverse sequences in Python
without writing a loop. This technique works for any sequence type (strings, lists,
tuples) because slicing is a standard operation available to these data structures.
Conclusion
Using [::-1]
is an efficient way to reverse a sequence. For the Atbash cipher, this
approach provides an easy way to create a reversed version of the alphabet, which is
then used to map each character directly to its reversed counterpart.
Sample Output#
Test cases for the Atbash cipher script with file handling. Use the values in Table 8.7 below to test your program.
Case |
filename |
---|---|
1 |
secret.txt |
2 |
empty.txt |
Ensure your program’s output matches the provided samples exactly. This includes all characters, white space, and punctuation. In the samples, user input is highlighted like this for clarity, but your program should not highlight user input in this way.
Case 1 Sample Output
$ python3 py4_ind_2_username.py Enter the name of the file: secret.txt The encrypted message is: 'R zn z sziwdliprmt VMTI86699 hgfwvmg!' Encryption completed. Check the output files for results.
1R zn z sziwdliprmt VMTI86699 hgfwvmg!
Case 2 Sample Output
$ python3 py4_ind_2_username.py Enter the name of the file: empty.txt The encrypted message is: '' Encryption completed. Check the output files for results.
1