affine

An affine cipher is a monoalphabetic substitution cipher, that uses math to generate the substitution alphabet based on three paramaters:

modulus

specifies how many different plaintexts and ciphertexts are available.

a_key

multiplied against the plaintext. Must be coprime with the modulus.

b_key

added to the multiplied plaintext. No restrictions, but it's modulus math, so making it larger than modulus is useless.

These parameters are passed in order to the Cipher constructor:

a = Affine::Cipher.new(2176782371, 65182241782, 123235151)
r = rand(123235150) # (should be smaller then the modulus)
ciphertext = a.encipher r
# ciphertext = ((r * 65182241782) + 123235151) % 2176782371
r == a.decipher(ciphertext)

Giant security caveat

Don't use this cipher for security related tasks. I'm using it to make primary keys alphanumeric and less-predictable: github.com/bkerley/have-code/

How to crack it

Know two plaintexts and their ciphertexts, put into linear system, solve.

Know the relation between two plaintexts, predict future ciphertexts.

Know lots of ciphertexts, guess it's English, solve like a cryptogram.

Copyright © 2009 Bryce Kerley. See LICENSE for details.