0% found this document useful (0 votes)
8 views6 pages

Untitled Document

The document discusses the Affine Cipher, a type of encryption that enhances the classic Caesar cipher by incorporating multiplication and shifting operations using two keys, 'a' and 'b'. It provides an overview of the encryption and decryption processes, including the mathematical functions involved, and presents a Python implementation for file encryption and decryption. The conclusion emphasizes the importance of encryption in securing data and adapting to technological advancements and emerging threats.

Uploaded by

Saif Madre
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
8 views6 pages

Untitled Document

The document discusses the Affine Cipher, a type of encryption that enhances the classic Caesar cipher by incorporating multiplication and shifting operations using two keys, 'a' and 'b'. It provides an overview of the encryption and decryption processes, including the mathematical functions involved, and presents a Python implementation for file encryption and decryption. The conclusion emphasizes the importance of encryption in securing data and adapting to technological advancements and emerging threats.

Uploaded by

Saif Madre
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 6

Affine Cipher

Saif Madre
Department of Computer Engineering
M.H Saboo Siddik College of
Engineering
Mumbai, India
[email protected]

Abstract
Encryption is the process of converting plaintext into ciphertext to protect information from
unauthorized access, ensuring confidentiality, integrity, and authenticity. By using mathematical
algorithms and cryptographic keys, encryption secures sensitive data during storage and transmission.
Symmetric encryption, such as AES, uses a single key for both encryption and decryption, while
asymmetric encryption, like RSA, employs a public-private key pair. Modern encryption is a
cornerstone of cybersecurity, enabling secure communication, protecting personal and financial
information, and safeguarding critical systems. Despite its robustness, the evolving landscape of
cryptographic attacks and computational advancements, such as quantum computing, presents
ongoing challenges, driving research into post-quantum encryption methods. Encryption remains a
critical tool for maintaining privacy and security in the digital age.

I. INTRODUCTION

Introduction

A product cipher is a cryptographic method combining two or more transformations to encrypt the
data. It's designed to deliver a higher level of security compared to ciphers that use only one
transformation technique. Transformations can be substitutional (like changing characters or bits) and
permutational (like rearranging the order).

To turn data from plaintext into ciphertext, product ciphers carry out multiple rounds of substitutions
and permutations, each round using a different subkey derived from the main key. It results in
securely encrypted data that’s very difficult to unencrypt without the proper key.

II. Affine Cipher

Affine Cipher is the development of Classic Caesar cipher that multiplies plain text before shifting
operation. Affine cipher has two parameters namely key a, and b. Key ‘a’ is multiplies the plain text.
It must have an inverse with Great Common Divisor (GCD) from the number of characters. The
Problems of Affine Cipher Algorithm is the number of characters available and the least possible of
key ‘a’ that can be used, so it is considered to be a classic technique that is easy to hack. Generally,
Affine Cipher is used to encrypt the plaintext of character ‘A to Z’ so that the number of keys ‘a’ is
only 12 digits from 26 digits (n) is available.
Encryption
It uses modular arithmetic to transform the integer that each plaintext letter corresponds to into
another integer that correspond to a ciphertext letter. The encryption function for a single letter is
E ( x ) = ( a x + b ) mod m
modulus m: size of the alphabet
a and b: key of the cipher.
a must be chosen such that a and m are coprime.

Decryption
In deciphering the ciphertext, we must perform the opposite (or inverse) functions on the ciphertext to
retrieve the plaintext. Once again, the first step is to convert each of the ciphertext letters into their
integer values. The decryption function is
D ( x ) = a^-1 ( x - b ) mod m
a^-1 : modular multiplicative inverse of a modulo m. i.e., it satisfies the equation
1 = a a^-1 mod m .

III. Solved Example

import random

class AffineCipher:

def _init_(self):

self.current_a = None

self.current_b = None

def generate_keys(self):

"""Generate random values for a and b"""

self.current_a = random.randint(1, 25) # Random value between 1 and 25

self.current_b = random.randint(0, 25) # Random value between 0 and 25

def encrypt_char(self, char, a, b):

"""Encrypt a single lowercase character"""

if char.isalpha() and char.islower():

x = ord(char) - ord('a')

encrypted_pos = (a * x + b) % 26
return chr(encrypted_pos + ord('a'))

return char

def decrypt_char(self, char, a, b):

"""Decrypt a single lowercase character"""

if char.isalpha() and char.islower():

y = ord(char) - ord('a')

for x in range(26):

if (a * x + b) % 26 == y:

return chr(x + ord('a'))

return char

def process_file(self, filename, mode='encrypt'):

try:

if mode == 'encrypt':

self.generate_keys()

elif self.current_a is None or self.current_b is None:

print("No keys available. Encrypt a file first.")

return False

with open(filename, 'r') as file:

content = file.read().lower()

if mode == 'encrypt':

processed_content = ''.join(self.encrypt_char(c, self.current_a, self.current_b) for c in


content)

print(f"\nFile encrypted successfully!")

print(f"Keys used: a={self.current_a}, b={self.current_b}")

print("Keep these keys safe for decryption!")

else:
processed_content = ''.join(self.decrypt_char(c, self.current_a, self.current_b) for c in
content)

print(f"\nFile decrypted successfully!")

with open(filename, 'w') as file:

file.write(processed_content)

return True

except Exception as e:

print(f"Error processing file: {str(e)}")

return False

def main():

cipher = AffineCipher()

while True:

print("\nAffine Cipher - File Encryption/Decryption")

print("1. Encrypt (Generate new keys)")

print("2. Decrypt (Use current keys)")

print("3. View current keys")

print("4. Exit")

choice = input("\nEnter your choice (1-4): ").strip()

if choice == '4':

print("Exiting program...")

break

if choice == '3':

if cipher.current_a is None:

print("No keys generated yet.")

else:

print(f"\nCurrent Keys: a={cipher.current_a}, b={cipher.current_b}")

continue
if choice not in ['1', '2']:

print("Invalid choice. Please try again.")

continue

filename = input("Enter the filename to process: ").strip()

mode = 'encrypt' if choice == '1' else 'decrypt'

cipher.process_file(filename, mode)

if _name_ == "_main_":

main()

Output:
IV. CONCLUSION

Encryption is an indispensable tool in safeguarding data and ensuring secure communication in


today's digital world. By converting plaintext into an unreadable format, it protects sensitive
information from unauthorized access, fostering trust and privacy. With the advancement of
technology, encryption methods have evolved to address emerging threats, including quantum
computing, ensuring resilience against future challenges. As cybersecurity remains a growing
concern, the continued development and adoption of robust encryption techniques are critical to
maintaining the confidentiality, integrity, and authenticity of information. Encryption will remain a
cornerstone of digital security, empowering individuals and organizations to navigate the complexities
of the modern information age with confidence.

IV. REFERENCES
[1] Product cipher definition
https://nordvpn.com/cybersecurity/glossary/product-cipher

[2] Modification Affine Cipher Transform Digraph to Squared the value of ‘n’ in Text Security
https://ieeexplore.ieee.org/document/9230503

[3] Implementation of Affine Cipher


https://www.geeksforgeeks.org/implementation-affine-cipher/?ref=ml_lbp

You might also like