Vigenere Cipher

 

Vigenère Cipher

Definition:

  • The Vigenère cipher is a method for encrypting alphabetic text.
  • It's a polyalphabetic substitution cipher, meaning it uses multiple Caesar ciphers with varying shifts.
  • Encryption relies on a keyword, which determines the shift amount for each letter of the plaintext.

Example:

  • Plaintext: ATTACK AT DAWN
  • Key: SECRET

Encryption process:

  1. Match each letter of the plaintext with the corresponding letter of the key (repeated if necessary).
    • ATTACK AT DAWN
    • SECRETSECRETS
  2. Use the Vigenère square to find the cipher text letter. The row index is the plaintext letter, and the column index is determined by the key letter.
    • Ciphertext: LXTFNL LPNFNLP

Applications (Historical):

  • The Vigenère cipher was once considered a very secure method due to its complex encryption.
  • It was used for military communication in various conflicts, including the American Civil War.

Limitations:

  • The Vigenère cipher is vulnerable to Kasiski Examination, which can reveal the key length.
  • With the key length known, cryptanalysis techniques can be applied to break the cipher.

Related Algorithms:

  • Caesar Cipher: A basic substitution cipher with a single shift value for the entire message.
  • Autokey Cipher: A variant of the Vigenère cipher where the key is derived from the plaintext itself.

Note: In modern cryptography, the Vigenère cipher is not considered secure due to its vulnerabilities. More robust encryption algorithms are used for secure communication.


Python code:

def vigenere_cipher(text, key, mode='encrypt'):

    result = []

    key = [ord(k) - 65 for k in key.upper()]

    key_length = len(key)

 

    for i, char in enumerate(text):

        if char.isalpha():

            shift = key[i % key_length]

            shift_base = 65 if char.isupper() else 97

            shift = shift if mode == 'encrypt' else -shift

            result.append(chr((ord(char) - shift_base + shift) % 26 + shift_base))

        else:

            result.append(char)

 

    return ''.join(result)

 

# Usage

message = "HELLO, WORLD!"

key = "KEY"

 

encrypted_message = vigenere_cipher(message, key, 'encrypt')

decrypted_message = vigenere_cipher(encrypted_message, key, 'decrypt')

 

print(f"Encrypted: {encrypted_message}")

print(f"Decrypted: {decrypted_message}")


Also see:
Ceaser Cipher

Ceaser Cipher

Ceaser Cipher

Definition

  • A substitution cipher that shifts each letter in the plaintext by a fixed number of positions down or up the alphabet.
  • Named after Julius Caesar, who used it for his private correspondence.
  • Simple and widely known encryption technique.

Application

  • Historically used for military and personal communication.
  • Modern usage includes educational purposes to introduce cryptography concepts.
  • Used in simple puzzles and games.
  • Not suitable for securing sensitive information due to vulnerability to brute-force attacks.

Examples

  1. Encryption Example:
    • Plaintext: "HELLO"
    • Shift: 3
    • Ciphertext: "KHOOR"
  2. Decryption Example:
    • Ciphertext: "KHOOR"
    • Shift: 3
    • Plaintext: "HELLO"
  3. Handling Non-Alphabet Characters:
    • Plaintext: "HELLO, WORLD!"
    • Shift: 3
    • Ciphertext: "KHOOR, ZRUOG!"

Related Algorithms 

  1. ROT13:
    • A specific case of the Caesar Cipher with a shift of 13.
    • Applying ROT13 twice returns the original text.
  2. Atbash Cipher:
    • Each letter in the alphabet is mapped to its reverse (e.g., 'A' becomes 'Z').
  3. Vigenère Cipher:
    • Uses a keyword to apply multiple Caesar Ciphers based on the letters of the keyword.
    • Provides better security by varying the shift value.
  4. Affine Cipher:
    • Combines the Caesar Cipher with a multiplicative step.
    • Uses a mathematical function of the form E(x)=(ax+b) mod  m to transform each letter.


Python code :

def caesar_cipher(text, shift, mode='encrypt'):

    if mode == 'decrypt':

        shift = -shift

    result = ''

    for char in text:

        if char.isalpha():

            shift_base = 65 if char.isupper() else 97

            result += chr((ord(char) - shift_base + shift) % 26 + shift_base)

        else:

            result += char

    return result

 

# Usage

message = "Hello, World!"

shift = 3

 

encrypted_message = caesar_cipher(message, shift, 'encrypt')

decrypted_message = caesar_cipher(encrypted_message, shift, 'decrypt')

 

print(f"Encrypted: {encrypted_message}")

print(f"Decrypted: {decrypted_message}")




Apptitude - Number System

 Concepts

  • Multiples and Factors
  • Total no. of factors of a composite number
  • LCM & HCF
  • Co-prime Numbers
  • Unit Digit
  • Highest power of P in n!
  • Concepts of Remainder
  • Divisibility

Co-Prime Numbers: 

Co-prime numbers are two numbers that have no other common factor than one. A set of co-prime numbers should consist of at minimum two numbers. Co-prime numbers, for example, {4 and 7}, {5, 7, 9} and 9, have just 1 as their greatest common factor. Co-prime numbers do not always have to be prime numbers.

Highest Common Factors (H.C.F) or greatest common Divisor (G.C.D) 
 two or more than two numbers is the greatest number that divides each one of them exactly. 
 Eg: H.C.F of 8 & 20 is 4 

Lowest Common Multiple (L.C.M): 
 The least number which is exactly divisible by each of them given numbers is called their lowest common multiple (L.C.M) 
 Eg: L.C.M of 8 & 20 is 40

Relation between L.C.M and H.C.F. of two Numbers:
 LCM X HCF = Product of the two numbers 

Compiler Design Part-3

 Parsers

Generation of Parse Tree Using:

  • Top down Approach  - Which production to use
  • Bottom up Approach - When to reduce
Classification of Parsers:


Compiler Design Part-2

 Topic Covers

Grammar:

A phrase structure grammar is (N,T,P,S) where,

  • N - Non terminal
  • T - Terminal
  • N intersection T = phi
  • S - start symbol
  • P- Production rules
Type 0: / unstricted grammar
Type 1: Length Increasing grammar- Context Sensitve Grammar
Type 2: CFG
Type 3: Regular Grammar




Derivation of CFG:

  • Left Derivation
  • Right Derivation
  • Parse tree Derivation

Left Recursion-

  • A production of grammar is said to have left recursion if the leftmost variable of its RHS is same as variable of its LHS.
  • A grammar containing a production having left recursion is called as Left Recursive Grammar.

Example- 

S → Sa / 

(Left Recursive Grammar)

 

  • Left recursion is considered to be a problematic situation for Top down parsers.
  • Therefore, left recursion has to be eliminated from the grammar.

Ambiguity in CFG:

Possible questions to ask
Whether the grammar is predective parsing or not
Check Whether the grammar is ambiguos or not
Problem due to Ambiguity
Associativity Property Violation
Precedence property Violation

Determine Associativity and precedence of the operators

NOTE:
  • For unambiguous grammars, Leftmost derivation and Rightmost derivation represents the same parse tree.
  • For ambiguous grammars, Leftmost derivation and Rightmost derivation represents different parse trees.

Recursion in CFG

Left recursion
Right Recursion
Elimination of Left Recursion
Problems with Left Recursion
Conversion of left to right Recursion

Non-Deterministic CFG
Elimination of Non-determination


Popular Post

MindMaps

Featured post

Question 1: Reverse Words in a String III

  def reverseWords(s: str) -> str: words = s.split() return ' '.join(word[::-1] for word in words)