Skip to main content

Understanding the Secret Cipher Algorithm

· 9 min read
Anand Raja
Senior Software Engineer

Introduction

Have you ever wondered how a simple password like "pass" or "pass123" can transform the entire alphabet into a completely scrambled substitution cipher? In this article, we'll dive deep into the mathematical foundations and algorithmic processes that power our Secret Cipher tool.

The Core Challenge

Creating a substitution cipher requires mapping each letter of the alphabet to a different letter. The key requirements are:

  1. Deterministic: The same secret key must always produce the same alphabet mapping
  2. Complex: The mapping should be unpredictable and not follow simple patterns
  3. Reversible: We must be able to decode using the same key
  4. Unique: Different keys should produce different mappings

The Algorithm Pipeline

Our cipher algorithm consists of three main stages:

Let's explore each stage in detail.


Stage 1: Seed Generation

The first step is converting our text-based secret key into a numeric seed value.

The Mathematical Formula

For a string S=s1s2s3...snS = s_1s_2s_3...s_n where each sis_i is a character, we compute:

seed=i=1n((seedi15)seedi1+code(si))\text{seed} = \left| \sum_{i=1}^{n} \left( (\text{seed}_{i-1} \ll 5) - \text{seed}_{i-1} + \text{code}(s_i) \right) \right|

Where:

  • code(si)\text{code}(s_i) is the ASCII/Unicode value of character sis_i
  • 5\ll 5 represents a left bit shift by 5 positions (equivalent to multiplying by 32)
  • The absolute value ensures a positive seed

Example: Converting "pass" to a Seed

Result: The secret key "pass" generates seed = 3433489

Example: Converting "pass123" to a Seed

Following the same process:

StepCharacterCodeCalculationSeed Value
1p1120 → 112112
2a97112 → 35693569
3s1153569 → 110754110754
4s115110754 → 34334893433489
51493433489 → 109871901109871901
6250109871901 → 35159008823515900882
73513515900882 → 112508828275112508828275

Result: The secret key "pass123" generates seed = 112508828275

Note: The addition of "123" creates a dramatically different seed value, leading to a completely different alphabet shuffling.


Stage 2: Seeded Random Number Generator

With our seed in hand, we need a way to generate consistent pseudo-random numbers. We use a Linear Congruential Generator (LCG), a classic algorithm for generating sequences of pseudo-random numbers.

The LCG Formula

The LCG generates a sequence using the recurrence relation:

Xn+1=(aXn+c)modmX_{n+1} = (aX_n + c) \bmod m

Where:

  • X0X_0 = our seed value
  • a=9301a = 9301 (multiplier)
  • c=49297c = 49297 (increment)
  • m=233280m = 233280 (modulus)

The random number in the range [0, 1) is then: Rn=XnmR_n = \frac{X_n}{m}

Why These Constants?

The values 9301, 49297, and 233280 are carefully chosen to satisfy the Hull-Dobell Theorem, which ensures the LCG has a full period (generates all possible values before repeating).

Example Sequence for "pass" (seed: 3433489)

Let's calculate the first few values:

  1. X1=(9301×3433489+49297)mod233280=97377X_1 = (9301 \times 3433489 + 49297) \bmod 233280 = 97377

    • R1=97377/233280=0.4172...R_1 = 97377 / 233280 = 0.4172...
  2. X2=(9301×97377+49297)mod233280=208434X_2 = (9301 \times 97377 + 49297) \bmod 233280 = 208434

    • R2=208434/233280=0.8934...R_2 = 208434 / 233280 = 0.8934...
  3. X3=(9301×208434+49297)mod233280=29297X_3 = (9301 \times 208434 + 49297) \bmod 233280 = 29297

    • R3=29297/233280=0.1256...R_3 = 29297 / 233280 = 0.1256...

Stage 3: Fisher-Yates Shuffle Algorithm

Now comes the magic: using our seeded random numbers to shuffle the alphabet. We use the Fisher-Yates shuffle algorithm, which guarantees every permutation has an equal probability.

The Algorithm

For an array of length nn:

for i=n1 down to 1:j=random()×(i+1)swap(array[i],array[j])\begin{aligned} &\text{for } i = n-1 \text{ down to } 1: \\ &\quad j = \lfloor \text{random}() \times (i+1) \rfloor \\ &\quad \text{swap}(\text{array}[i], \text{array}[j]) \end{aligned}

Visual Example: Shuffling with "pass"

Let's see how the alphabet abcdefghijklmnopqrstuvwxyz gets shuffled:

Complete Alphabet Mapping for "pass"

Using our seed-generated random sequence, here's the complete transformation:

Position0123456789101112
Originalabcdefghijklm
Shuffledwqxrpmugozdev
Position13141516171819202122232425
Originalnopqrstuvwxyz
Shuffledcbtaifjnhskyl

Encoding Example:

  • "HELLO" → "GPEEB"
  • "WORLD" → "SBIEP"

Complete Alphabet Mapping for "pass123"

With a different seed, we get a completely different shuffle:

Position0123456789101112
Originalabcdefghijklm
Shuffledmskrqjoctgywe
Position13141516171819202122232425
Originalnopqrstuvwxyz
Shuffledxnizbhuvpldaf

Encoding Example:

  • "HELLO" → "CQWWN"
  • "WORLD" → "LNBWR"

Comparing the Results

Notice how the same input "HELLO" produces completely different cipher texts:

  • With "pass": GPEEB
  • With "pass123": CQWWN

This demonstrates the sensitivity of our algorithm to the secret key!


Mathematical Properties

Entropy and Randomness

The quality of our shuffle can be measured by its entropy. For a perfect shuffle of 26 letters:

H=log2(26!)88.38 bitsH = \log_2(26!) \approx 88.38 \text{ bits}

This means there are 26!4.03×102626! \approx 4.03 \times 10^{26} possible alphabet permutations!

Period Length

Our LCG has a period of 233,280, which is sufficient for shuffling 26 positions. Each call to the random generator produces a unique value within one complete shuffle cycle.

Collision Probability

The probability that two different keys produce the same alphabet mapping is:

P(collision)=126!2.48×1027P(\text{collision}) = \frac{1}{26!} \approx 2.48 \times 10^{-27}

Essentially impossible for practical purposes!


Security Considerations

While this cipher is fun and educational, it's important to understand its limitations:

  1. Frequency Analysis: The cipher is vulnerable to frequency analysis since letter frequencies are preserved
  2. Known Plaintext Attack: If an attacker knows both plaintext and ciphertext, they can deduce the mapping
  3. Brute Force: With modern computing, testing many keys is feasible
  4. No Authentication: The cipher doesn't verify message integrity

Recommendation: This cipher is perfect for learning, fun messages, and understanding cryptography basics. For serious encryption needs, use modern algorithms like AES-256.


Implementation Efficiency

Time Complexity

  • Seed Generation: O(n)O(n) where nn is the key length
  • LCG Generation: O(1)O(1) per call
  • Fisher-Yates Shuffle: O(m)O(m) where mm is alphabet size (26)
  • Encoding/Decoding: O(k)O(k) where kk is message length

Overall: O(n+k)O(n + k) - linear time complexity!

Space Complexity

  • Alphabet storage: O(m)=O(26)=O(1)O(m) = O(26) = O(1) (constant)
  • Message processing: O(k)O(k) for input/output

Overall: O(k)O(k) - linear space complexity!


Conclusion

Our Secret Cipher algorithm combines classical cryptographic techniques with modern programming concepts:

  1. String Hashing converts arbitrary text keys into numeric seeds
  2. Linear Congruential Generator provides deterministic pseudo-randomness
  3. Fisher-Yates Shuffle ensures perfect permutation distribution

The result is a simple yet elegant substitution cipher that transforms "HELLO WORLD" into seemingly random text, all controlled by a secret key you choose.

Try it yourself with different keys and see how dramatically the output changes. Remember: the same key always produces the same mapping, making encoding and decoding perfectly reversible!


Further Reading