0% found this document useful (0 votes)
5 views

algorithm_pseudo_codes

Pseudocode for the ciphers such as Affine Cipher, Vigenere Cipher and OTP Algorithm.

Uploaded by

Abu Musa Sakib
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
5 views

algorithm_pseudo_codes

Pseudocode for the ciphers such as Affine Cipher, Vigenere Cipher and OTP Algorithm.

Uploaded by

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

Caesar Cipher Algorithm

Constants:
● alphabet = ' ABCDEFGHIJKLMNOPQRSTUVWXYZ' (including space)

● key = 3 (the number of positions each letter is shifted)


Functions:
1. Caesar Encrypt
● Input: plain_text (string)

● Output: cipher_text (string)


● Steps:
1. Initialize an empty string cipher_text.
2. Convert plain_text to uppercase.
3. For each character c in plain_text:
1. Find the index of c in alphabet.
2. Calculate the new index as (index + key) % len(alphabet).
3. Append the character at the new index in alphabet to
cipher_text.
4. Return cipher_text.
2. Caesar Decrypt
● Input: cipher_text (string)

● Output: plain_text (string)


● Steps:
1. Initialize an empty string plain_text.
2. For each character c in cipher_text:
1. Find the index of c in alphabet.
2. Calculate the original index as (index - key) % len(alphabet).
3. Append the character at the original index in alphabet to
plain_text.
3. Return plain_text.
Main Program:
1. Define the message m as 'Welcome to the cyber security course'.
2. Encrypt m using caeser_encrypt and store the result in encrypted.
3. Print encrypted.
4. Decrypt encrypted using caeser_decrypt and store the result in decrypted.
5. Print decrypted.
Explanation:
● Encrypt Function: Converts each character of the input text to its corresponding
encrypted character by shifting its position in the alphabet by a fixed key.
● Decrypt Function: Converts each character of the encrypted text back to its original
character by shifting its position in the alphabet backwards by the same key.
● Main Program: Demonstrates the use of the encryption and decryption functions by
encrypting a sample message and then decrypting it to verify the correctness of the
operations.
Brute Force Attack Algorithm for
Caesar Cipher
Constants:
● alphabet = ' ABCDEFGHIJKLMNOPQRSTUVWXYZ' (including space)
Functions:
1. Crack Caesar Cipher
● Input: cipher_text (string)

● Output: All possible decrypted messages for each key (printed)


● Steps:
1. Loop over each possible key value from 0 to length of alphabet - 1:
1. Initialize an empty string plain_text.
2. For each character c in cipher_text:
1. Find the index of c in alphabet.
2. Calculate the original index as (index - key) % length of
alphabet.
3. Append the character at the original index in alphabet
to plain_text.
3. Print the possible decrypted message with the current key.
Main Program:
1. Define the encrypted message encrypted as
'ZHOFRPHCWRCWKHCFRPSXWHUCDQGCFAEHUCVHFXULWACFRXUVH'.
2. Call the function crack_caeser with encrypted as the argument.
Explanation:
● Crack Caesar Cipher Function: Attempts to decrypt the given cipher_text using every
possible key (from 0 to length of alphabet - 1). For each key, it shifts each character
in the cipher_text backwards by the key's value and reconstructs the plain_text. It
prints the resulting plain_text for each key.
● Main Program: Provides an encrypted message and calls the function to
demonstrate the brute force attack, which outputs all potential decryptions.

Frequency Analysis Algorithm for


Caesar Cipher
Constants:
● LETTERS = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ'
Functions:
1. Frequency Analysis
● Input: text (string)

● Output: letter_frequencies (dictionary mapping each letter to its frequency)


● Steps:
1. Convert text to uppercase.
2. Initialize a dictionary letter_frequencies with keys from LETTERS and
values set to 0.
3. For each character letter in text:
1. If letter is in LETTERS, increment its count in
letter_frequencies.
4. Return letter_frequencies.
2. Plot Distribution
● Input: frequencies (dictionary mapping each letter to its frequency)

● Output: A bar chart of letter frequencies


● Steps:
1. Create a bar chart using the keys and values of frequencies.
2. Display the chart.
3. Caesar Crack
● Input: cipher_text (string)

● Output: Print the possible key value based on frequency analysis


● Steps:
1. Perform frequency analysis on cipher_text to get freq.
2. Sort freq by frequency in descending order.
3. Determine the possible key by finding the difference in positions
between the most frequent letter in cipher_text and the letter 'E'.
4. Print the possible key value.
Main Program:
1. Define plain_text as "Welcome to the cyber security course".
2. Perform frequency analysis on plain_text and print the sorted frequency
distribution.
3. Define cipher_text as "ZHOFRPHCWRCWKHCFAEHUCVHFXULWACFRXUVH".
4. Perform frequency analysis on cipher_text and print the sorted frequency
distribution.
5. Use caeser_crack function to determine and print the possible key value for
cipher_text.

Affine Cipher Pseudo-code


1. Extended Euclidean Algorithm for Finding Modular Inverse
Function: egcd(a, b)
● Input: Two integers a and b

● Output: gcd, x, and y such that gcd is the greatest common divisor of a and b, and
a*x + b*y = gcd
1. Initialize x = 0, y = 1, u = 1, v = 0
2. While a is not 0:
● Compute q = b // a and r = b % a

● Update m = x - u * q and n = y - v * q
● Set b = a, a = r, x = u, y = v, u = m, v = n
3. Return gcd = b, x, and y
Function: modinv(a, m)
● Input: Integer a and modulus m

● Output: Modular inverse of a modulo m, or None if it does not exist


1. Call egcd(a, m) to get gcd, x, and y
2. If gcd is not 1, return None (modular inverse does not exist)
3. Otherwise, return x % m
2. Affine Cipher Encryption
Function: affine_encrypt(text, key)
● Input: Plaintext text and key key where key is a list [a, b]

● Output: Encrypted ciphertext


1. Initialize encrypted_text as an empty list
2. For each character t in text:
● Convert t to uppercase and remove spaces

● Compute the numeric value of t as P = ord(t) - ord('A')


● Compute the encrypted value C = (a * P + b) % 26
● Convert C back to a character and append to encrypted_text
3. Return the concatenation of encrypted_text
3. Affine Cipher Decryption
Function: affine_decrypt(cipher, key)
● Input: Ciphertext cipher and key key where key is a list [a, b]

● Output: Decrypted plaintext


1. Initialize result as an empty list
2. Compute the modular inverse of a using modinv(a, 26) and store it in mi
3. For each character c in cipher:
● Compute the numeric value of c as C = ord(c) - ord('A')

● Compute the decrypted value P = (mi * (C - b)) % 26


● Convert P back to a character and append to result
4. Return the concatenation of result
4. Driver Code
Function: main()
● Purpose: Test the encryption and decryption functions
1. Declare the plaintext text as 'SECURITY'
2. Declare the key key as [3, 7]
3. Call affine_encrypt(text, key) to get the encrypted text and store it in
affine_encrypted_text
4. Print affine_encrypted_text
5. Call affine_decrypt(affine_encrypted_text, key) to get the decrypted text and print
it
Main Execution:
● If the script is run directly, call main()
Summary
This pseudo-code outlines the process for encrypting and decrypting text using the Affine
Cipher, including finding the modular inverse using the Extended Euclidean Algorithm. The
steps are described in words, detailing the logic behind each function and its purpose.

Vigenere Cipher :

ALPHABET = ' ABCDEFGHIJKLMNOPQRSTUVWXYZ'

FUNCTION vigenere_encrypt(plain_text, vigenere_key)


cipher_text = ''
plain_text = TO_UPPERCASE(plain_text)
vigenere_key = TO_UPPERCASE(vigenere_key)
key_index = 0

FOR EACH character IN plain_text


index = FIND_INDEX(ALPHABET, character)
key = FIND_INDEX(ALPHABET, vigenere_key[key_index])
index = (index + key) MOD LENGTH(ALPHABET)
cipher_text = cipher_text + ALPHABET[index]
key_index = key_index + 1

IF key_index == LENGTH(vigenere_key)
key_index = 0
END IF
END FOR

RETURN cipher_text
END FUNCTION

FUNCTION vigenere_decrypt(cipher_text, vigenere_key)


plain_text = ''
cipher_text = TO_UPPERCASE(cipher_text)
vigenere_key = TO_UPPERCASE(vigenere_key)
key_index = 0

FOR EACH character IN cipher_text


index = FIND_INDEX(ALPHABET, character)
key = FIND_INDEX(ALPHABET, vigenere_key[key_index])
index = (index - key) MOD LENGTH(ALPHABET)
plain_text = plain_text + ALPHABET[index]
key_index = key_index + 1

IF key_index == LENGTH(vigenere_key)
key_index = 0
END IF
END FOR

RETURN plain_text
END FUNCTION

FUNCTION MAIN
text = 'cryptography is quite important in the cryptocurrency'
encrypted = vigenere_encrypt(text, 'ANIMAL')
PRINT("The encrypted message is: " + encrypted)
PRINT("And the decrypted message is: " + vigenere_decrypt(encrypted, 'ANIMAL'))
END FUNCTION

OTP Algorithm :
# Define the alphabet including space
ALPHABET = ' ABCDEFGHIJKLMNOPQRSTUVWXYZ'

# Function to encrypt the text using the OTP


FUNCTION encrypt(text, key)
SET text TO uppercase(text)
SET cipher_text TO empty string

FOR each character in text with index


SET key_index TO key[index]
SET char_index TO FIND the index of character in ALPHABET

# Encrypt the character by adding char_index and key_index modulo length of


ALPHABET
SET encrypted_char_index TO (char_index + key_index) MOD length of ALPHABET

# Convert the encrypted index back to a character and append to cipher_text


APPEND ALPHABET[encrypted_char_index] TO cipher_text

RETURN cipher_text

# Function to decrypt the text using the OTP


FUNCTION decrypt(cipher_text, key)
SET plain TO empty string

FOR each character in cipher_text with index


SET key_index TO key[index]
SET char_index TO FIND the index of character in ALPHABET
# Decrypt the character by subtracting key_index from char_index modulo length of
ALPHABET
SET plain_char_index TO (char_index - key_index) MOD length of ALPHABET

# Convert the decrypted index back to a character and append to plain


APPEND ALPHABET[plain_char_index] TO plain

RETURN plain

# Function to generate a random sequence of key values


FUNCTION random_sequence(text)
SET random TO empty list

FOR each character in text


APPEND a random integer between 0 and length of ALPHABET - 1 TO random

RETURN random

# Function to perform frequency analysis on the text


FUNCTION frequency_analysis(text)
SET text TO uppercase(text)
SET letter_frequencies TO dictionary with keys from ALPHABET and values set to 0

FOR each character in text


IF character is in ALPHABET
INCREMENT letter_frequencies[character] BY 1

RETURN letter_frequencies

# Function to plot the frequency distribution


FUNCTION plot_distribution(frequencies)
PLOT a bar chart of frequencies keys and values
DISPLAY the plot

# Main program execution


FUNCTION main()
SET message TO 'This is a very long text The text does not contain special characters and
other symbols This is just alphabet letters Only for demo purpose to understand the
frequency analysis cracking mechanism'
SET seq TO random_sequence(message)

PRINT "Original message: " + uppercase(message)


SET cipher TO encrypt(message, seq)
PRINT "Encrypted message: " + cipher
SET decrypted_text TO decrypt(cipher, seq)
PRINT "Decrypted message: " + decrypted_text
CALL plot_distribution(frequency_analysis(cipher))

CALL main()

You might also like