Skip to content

Nursca/ceaser-cipher-program

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

1 Commit
 
 
 
 
 
 

Repository files navigation

Caesar Cipher - JavaScript Implementation

Overview

The Caesar Cipher is one of the simplest and most famous encryption techniques. It shifts each letter in the plaintext by a fixed number of positions down the alphabet.

Key Concepts:

  • Shift (Key): The number of positions to move each letter (typically 1-25)
  • Plaintext: The original, unencrypted message
  • Ciphertext: The encrypted message
  • Symmetry: The same shift is used for both encryption and decryption (but in opposite directions)

How It Works

Encryption Process:

For each character in the message:

  1. If it's a letter, shift it by the key amount
  2. If the shift goes past 'Z' (or 'z'), wrap around to the beginning of the alphabet
  3. If it's not a letter (numbers, spaces, punctuation), leave it unchanged

Example with Shift = 3:

A → D
B → E
C → F
...
X → A
Y → B
Z → C

Decryption Process:

Simply reverse the process by shifting backwards by the same amount.

Algorithm Explanation

CharCode Approach:

The implementation uses ASCII character codes:

  • Uppercase 'A' = 65, 'Z' = 90
  • Lowercase 'a' = 97, 'z' = 122

Step-by-step for encryption:

  1. Get the character's position in the alphabet: position = char.charCodeAt(0) - 'A'.charCodeAt(0)

    • This converts 'A' → 0, 'B' → 1, ..., 'Z' → 25
  2. Add the shift: newPosition = (position + shift) % 26

    • Using modulo (%) ensures wrapping (e.g., 25 + 3 = 28 % 26 = 2)
  3. Convert back to character: String.fromCharCode(newPosition + 'A'.charCodeAt(0))

    • This adds the code back to get the actual character

Function Reference

encrypt(message, shift)

  • Parameters:
    • message (string): Text to encrypt
    • shift (number): Positions to shift (1-25, but any number works due to modulo)
  • Returns: Encrypted text
  • Example: encrypt('Hello', 3)'Khoor'

decrypt(encrypted, shift)

  • Parameters:
    • encrypted (string): Encrypted text
    • shift (number): Original shift value
  • Returns: Decrypted text
  • Example: decrypt('Khoor', 3)'Hello'

bruteForceDecrypt(encrypted)

  • Parameters:
    • encrypted (string): Encrypted text with unknown shift
  • Returns: Array of 26 objects with all possible decryptions
  • Example: Try all shifts automatically to find the correct one

Security Notes

⚠️ Important: The Caesar Cipher is NOT secure for real-world use:

  • Only 26 possible shifts, easy to crack with brute force
  • Pattern analysis can reveal the shift
  • Modern encryption uses complex algorithms (AES, RSA, etc.)

Use this only for:

  • Learning cryptography concepts
  • CTF (Capture The Flag) challenges
  • Simple educational purposes

Running the Examples

node demo.js

This will show various examples of encryption, decryption, and brute force attacks.

About

No description, website, or topics provided.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

 
 
 

Contributors