Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commitb277253

Browse files
committed
add implementing affine cipher tutorial
1 parent2d39d8c commitb277253

File tree

4 files changed

+50
-0
lines changed

4 files changed

+50
-0
lines changed

‎README.md‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,7 @@ This is a repository of all the tutorials of [The Python Code](https://www.thepy
5151
-[How to Crack the Caesar Cipher in Python](https://thepythoncode.com/article/how-to-crack-caesar-cipher-in-python). ([code](ethical-hacking/caesar-cipher))
5252
-[How to Lock PDFs in Python](https://thepythoncode.com/article/lock-pdfs-in-python).[(code)](ethical-hacking/pdf-locker)
5353
-[How to Create a Custom Wordlist in Python](https://thepythoncode.com/article/make-a-wordlist-generator-in-python). ([code](ethical-hacking/bruteforce-wordlist-generator))
54+
-[How to Implement the Affine Cipher in Python](https://thepythoncode.com/article/how-to-implement-affine-cipher-in-python). ([code](ethical-hacking/implement-affine-cipher))
5455

5556
-###[Machine Learning](https://www.thepythoncode.com/topic/machine-learning)
5657
-###[Natural Language Processing](https://www.thepythoncode.com/topic/nlp)
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
#[How to Implement the Affine Cipher in Python](https://thepythoncode.com/article/how-to-implement-affine-cipher-in-python)
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
# Import necessary libraries.
2+
importstring
3+
fromcoloramaimportinit,Fore
4+
5+
# Initialise colorama.
6+
init()
7+
8+
9+
# Function to perform Affine Cipher encryption.
10+
defaffine_encryption(plaintext,a,b):
11+
# Define the uppercase alphabet.
12+
alphabet=string.ascii_uppercase
13+
# Get the length of the alphabet
14+
m=len(alphabet)
15+
# Initialize an empty string to store the ciphertext.
16+
ciphertext=''
17+
18+
# Iterate through each character in the plaintext.
19+
forcharinplaintext:
20+
# Check if the character is in the alphabet.
21+
ifcharinalphabet:
22+
# If it's an alphabet letter, encrypt it.
23+
# Find the index of the character in the alphabet.
24+
p=alphabet.index(char)
25+
# Apply the encryption formula: (a * p + b) mod m.
26+
c= (a*p+b)%m
27+
# Append the encrypted character to the ciphertext.
28+
ciphertext+=alphabet[c]
29+
else:
30+
# If the character is not in the alphabet, keep it unchanged.
31+
ciphertext+=char
32+
33+
# Return the encrypted ciphertext.
34+
returnciphertext
35+
36+
37+
# Define the plaintext and key components.
38+
plaintext=input(f"{Fore.GREEN}[?] Enter text to encrypt: ")
39+
a=3
40+
b=10
41+
42+
# Call the affine_encrypt function with the specified parameters.
43+
encrypted_text=affine_encryption(plaintext,a,b)
44+
45+
# Print the original plaintext, the key components, and the encrypted text.
46+
print(f"{Fore.MAGENTA}[+] Plaintext:{plaintext}")
47+
print(f"{Fore.GREEN}[+] Encrypted Text:{encrypted_text}")
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
colorama

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp