Introduction
In this project, you will learn how to implement the Affine Encryption algorithm in Python. The Affine Cipher is a type of substitution cipher that combines the characteristics of the shift cipher and the multiplier cipher. It uses a cryptographic function to encrypt one letter per letter, providing a simple yet effective way to encrypt and decrypt text.
👀 Preview
Sample 1: welcome - > oclsaqc
Sample 2: welcome - > oclsaqc
Sample 3: Qrwe - > Qroc
Sample 4: None - > None
🎯 Tasks
In this project, you will learn:
- How to implement the affine encryption function in Python
- How to test the affine encryption function with sample inputs
- How to encrypt user input using the affine encryption function
🏆 Achievements
After completing this project, you will be able to:
- Understand the basic principles of the Affine Cipher
- Implement the affine encryption algorithm in Python
- Encrypt and decrypt text using the affine encryption function
- Test the affine encryption function with various inputs
Implement the Affine Encryption Function
In this step, you will learn how to implement the affine encryption function in Python. Follow the steps below to complete this step:
- Open the
affine.pyfile in your text editor. - Inside the
affine_encryption(text)function, add the following code:
def affine_encryption(text: str) -> str:
if text is None or text == "":
return None
a = 5
b = 8
m = 26
encryption_text = ""
for char in text:
if "A" <= char <= "Z" or "a" <= char <= "z":
char = char.lower()
x = ord(char) - ord("a")
y = (a * x + b) % m
encryption_text += chr(y + ord("a"))
else:
encryption_text += char
return encryption_text
Explanation:
- The function first checks if the input
textisNoneor an empty string. If so, it returnsNone. - The function then sets the values of
a,b, andmas required by the affine encryption formula. - It iterates through each character in the input
text. - If the character is a half-width English letter (between 'A' and 'Z', or 'a' and 'z'), it converts the character to lowercase, calculates the corresponding numerical value
x, applies the affine encryption formulay = (a * x + b) % m, and appends the encrypted character to theencryption_textstring. - If the character is not a half-width English letter, it is added to the
encryption_textstring without any modification. - Finally, the function returns the
encryption_textstring.
- Save the
affine.pyfile.
Test the Affine Encryption Function
In this step, you will test the affine_encryption() function with the provided sample inputs.
- In the
affine.pyfile, add the following code at the end of the file:
if __name__ == "__main__":
## Sample 1
text = "welcome"
encryption_text = affine_encryption(text)
print(f"Sample 1: {text} -> {encryption_text}")
## Sample 2
text = " welcome"
encryption_text = affine_encryption(text)
print(f"Sample 2: {text} -> {encryption_text}")
## Sample 3
text = " Qrwe"
encryption_text = affine_encryption(text)
print(f"Sample 3: {text} -> {encryption_text}")
## Sample 4
text = None
encryption_text = affine_encryption(text)
print(f"Sample 4: {text} -> {encryption_text}")
- Save the
affine.pyfile. - Open a terminal or command prompt, navigate to the directory containing the
affine.pyfile, and run the following command:
python3 affine.py
This will execute the affine_encryption() function with the provided sample inputs and print the results.
Verify that the output matches the expected results:
Sample 1: welcome -> oclsaqc
Sample 2: welcome -> oclsaqc
Sample 3: Qrwe -> Qroc
Sample 4: None -> None
If the output matches the expected results, your affine_encryption() function is working correctly.
Encrypt User Input
In this step, you will modify the affine.py file to allow the user to input a text and encrypt it using the affine_encryption() function.
- In the
affine.pyfile, replace the existing code at the end of the file (i.e.,if __name__ == "__main__":and the portion that follows) with the following code:
if __name__ == "__main__":
text = input("Enter the text to encrypt: ")
encryption_text = affine_encryption(text)
if encryption_text is None:
print("Error: Input text is empty.")
else:
print(f"Encrypted text: {encryption_text}")
Explanation:
- The code prompts the user to enter the text to be encrypted.
- It then calls the
affine_encryption()function with the user's input and stores the result in theencryption_textvariable. - If the
encryption_textisNone(which means the input text was empty), it prints an error message. - Otherwise, it prints the encrypted text.
- Save the
affine.pyfile. - Open a terminal or command prompt, navigate to the directory containing the
affine.pyfile, and run the following command:
python3 affine.py
- When prompted, enter a text to be encrypted, and observe the output.
Congratulations! You have completed the implementation of the affine encryption function and tested it with various inputs.
Summary
Congratulations! You have completed this project. You can practice more labs in LabEx to improve your skills.



