Project 6: The Secret Message Encoder (Matrix Algebra)

Build a simple encoder/decoder that uses matrices to scramble and recover messages.


Project Overview

Attribute Value
Difficulty Level 2: Intermediate
Time Estimate Weekend
Main Language Python
Alternative Languages JavaScript, C++
Knowledge Area Matrix algebra
Tools CLI or simple UI
Main Book “Linear Algebra and Its Applications” by Gilbert Strang

What you’ll build: A tool that encodes text as numbers, multiplies by a matrix, and decodes it back.

Why it teaches math: Matrix multiplication stops being abstract when it directly scrambles information.

Core challenges you’ll face:

  • Converting letters to numeric vectors
  • Ensuring the matrix is invertible
  • Handling padding for fixed block sizes

Real World Outcome

You will encode a message into scrambled numbers and then decode it back to the original text.

Example Output:

$ python matrix_cipher.py --encode "HELLO"
Encoded vector: [72, 5, 12, 12, 15]
Cipher blocks: [[...], [...]]

$ python matrix_cipher.py --decode cipher.txt
Decoded message: HELLO

Verification steps:

  • Encode and decode multiple messages
  • Verify decoding fails if the key matrix is wrong

The Core Question You’re Answering

“How does a matrix transform data in a reversible way?”

This is linear algebra with a real consequence.


Concepts You Must Understand First

Stop and research these before coding:

  1. Matrix multiplication
    • How does a matrix transform a vector?
    • Book Reference: “Linear Algebra and Its Applications” by Gilbert Strang, Ch. 2
  2. Matrix invertibility
    • What makes a matrix invertible?
    • Book Reference: “Linear Algebra and Its Applications” by Gilbert Strang, Ch. 4
  3. Modular arithmetic (optional)
    • Why might you use modulo to keep numbers small?
    • Book Reference: “A Book of Abstract Algebra” by Charles Pinter, Ch. 2

Questions to Guide Your Design

  1. Encoding scheme
    • How will you map letters to numbers?
    • How will you handle lowercase and punctuation?
  2. Key management
    • How will you generate and store the key matrix?
    • How will you verify invertibility?

Thinking Exercise

Small Matrix Cipher

Take the message “HI” mapped to [8, 9] and multiply by the 2x2 matrix [[2,1],[1,1]]. Compute the encoded vector.

Questions while working:

  • Can you recover the original using the inverse?
  • What happens if the matrix is not invertible?

The Interview Questions They’ll Ask

Prepare to answer these:

  1. “What does an invertible matrix mean?”
  2. “Why does multiplication scramble data?”
  3. “How do you decode using the inverse?”
  4. “What is the determinant’s role?”
  5. “Why is this not secure encryption in practice?”

Hints in Layers

Hint 1: Starting Point Start with 2x2 matrices and short messages.

Hint 2: Next Level Pad messages so they fit the matrix size.

Hint 3: Technical Details Check determinant is non-zero before encoding.

Hint 4: Tools/Debugging Verify decoding by multiplying with the inverse matrix.


Books That Will Help

Topic Book Chapter
Matrix multiplication “Linear Algebra and Its Applications” by Gilbert Strang Ch. 2
Invertibility “Linear Algebra and Its Applications” by Gilbert Strang Ch. 4
Modular arithmetic “A Book of Abstract Algebra” by Charles Pinter Ch. 2

Implementation Hints

  • Keep matrices small to simplify debugging.
  • Validate invertibility before proceeding.
  • Keep encoding and decoding steps symmetric.

Learning Milestones

  1. First milestone: You can encode and decode short messages.
  2. Second milestone: You can explain invertibility and determinants.
  3. Final milestone: You can scale to larger blocks and keys.