Sixth rule C. 68 is sexy, Φ is beautiful, 10,213,223 is cool (it describes itself when you read it), but the most important numbers are not 0, i, π, e, or NA. It is indeed primarily your girlfriend’s number, then her mom’s number, and finally her lawyer’s number, Apocalypse, Anawim, #justtothepoint.
Cryptography is the practice and study of techniques to secretly obscure, store or communicate messages so uninvited outsiders cannot read the message. It involves two steps: encryption, in which the message is obscured, distorted, and cannot be read, and decryption, in which the original message is recovered from the obscured form, typically in plain English.
A cypher is a scheme for changing or replacing a letter (a syllable, a number, etc.) in the message with a different letter or character following a pre-established mapping, called the encryption mapping.
Plaintext is usually ordinary readable text before it is encrypted into ciphertext or, in other words, readable text after it is decrypted. The mapping is bijective so ciphertexts can be converted back to the plain text using an inverse mapping, called the decryption mapping.
Symmetric key cryptography is any cryptographic algorithm that is based on a shared key that is used to encrypt or decrypt text, i.e., both the sender (Alice) and the receiver use the same key for encryption and decryption.
The mappings most commonly used in cryptography are algebraic, and therefore it previously require the message to be numerical. We will use the simple rule “A” or “a” = 0, “B” or “b” = 1, “C” or “c” = 2, and so on. However, ASCII is one standard way to encode a text written in English into integers.
This is the code in Python that digitalized “MySecretMessage” as [15, 1, 21, 7, 5, 20, 7, 22, 15, 7, 21, 21, 3, 9, 7], encrypt it using the Caesar Algorithm, decrypt it, then rebuild the text from the list of digits.
def digitalize_message(text):
# Convert a message into an array or list of digits
numbers = []
for char in text: # It iterates over the message.
if char.isalpha(): # It returns True if the character is an alphabet letter.
number = ord(char.lower()) - 97
numbers.append(number)
else:
numbers.append(char)
return numbers
def convert_to_text(numbers):
# Reconstruct the message from the list of digits.
letters = ''
for num in numbers: # It iterates over the list of digits.
if isinstance(num, int): # If it a digit, then it converts it into a letter
letter = chr(num + 65)
letters += letter
else:
letters += num
return letters
def caesar_encrypt(text, a):
numbers = digitalize_message(text) # 1st Digitalize the message.
transformed_list = []
for x in numbers:
transformed_x = (x + a) % 26 # 2nd Encrypt the message using Caesar
transformed_list.append(transformed_x)
return transformed_list
def caesar_dencrypt(coded_text, a):
transformed_list = []
for x in coded_text:
transformed_x = (x - a) % 26 # Decrypt the message using Caesar
transformed_list.append(transformed_x)
original_text = convert_to_text(transformed_list)
return original_text
text = "MySecretMessage"
print(text)
coded_text = caesar_encrypt(text, 3)
print(coded_text)
print(convert_to_text(coded_text))
original_text = caesar_dencrypt(coded_text, 3)
print(original_text)
It returns:
MySecretMessage
[15, 1, 21, 7, 5, 20, 7, 22, 15, 7, 21, 21, 3, 9, 7]
PBVHFUHWPHVVDJH
MYSECRETMESSAGE
Caesar cipher is one of the earliest and simplest methods of encryption technique where each letter is replaced by another letter some fixed number of positions later in the alphabet, historically three. This fixed number is the key of a Caesar cipher.
When at the end of the alphabet, the letter will wrap around to the beginning, so it can be represented using modular arithmetic, and the encryption can be described as En(x) = (x + n) mod 26 -Encryption of a letter x by a shift n where the alphabet has 26 characters-, and the decryption Dn(x) = (x - n) mod 26.
This method is super easy to use and implement compared to other algorithms, but both parties must keep the key secure. Besides, this method has only 26 encryption keys, that is quite easy to break through brutal force by hackers.
An easy improvement is an affine cipher, the encryption mapping is as follows, x → ax + b(mod n), and decryption, y → a-1y -a-1b(mod n) where gcd(a, n) = 1.
y = ax + b(mod n) ⇒ ax = y - b (mod n) ⇒ [gcd(a, n) = 1, so a has an inverse in ℤn] x = a-1(y - b) = a-1y - a-1b.
Diffie–Hellman key exchange is a mathematical method of securely exchanging cryptographic keys over a public and therefore, insecure channel. It was an incredible breakthrough in cryptography that showed that private keys could be securely exchanged in plain sight.
Alice and Bob want to talk or, even better, share a treasure 📦 to each other securely, but they have to assume that a malicious actor, Eve (as in “eavesdropper”) has access to the channel as well. Let’s try to figure out how Alice could send a treasure to Bob.
This method is know as the the Diffie-Hellman Key Exchange, but the back-and-forth nature of this exchange can make the process very slow. Besides, what if the courier instead of bringing the box to Bob, the courier himself places a lock on the box, claiming it is Bob's lock, and return it to Alice. She may be fooled, then she removes her lock leaving the courier's lock on the box!!
This is where RSA comes to play. Bob shares with everyone (chests with open locks) his public key. Then, when Alice sends a message or treasure to Bob, it locked the message with Bob's public key or public lock, hence only Bob's private key is capable of unlocking it.
To create a public and private key for RSA, Alice selects an integer e such that gcd(e, Φ(n)) = 1 (in other words, e ∈ ℤΦ(n)*)
Then, using the Euclidean algorithm (gcd(e, Φ(n)) = 1), Alice computer her private key d where de + kΦ(n) = 1 for some k ∈ ℤ ↭ [By construction, n = pq, Φ(n) = lcm(Φ(p), Φ(q)), and since p and q are primes, Φ(n) = lcm(p-1, q-1)] Φ(n) = lcm(p-1, q-1) =[de + kΦ(n) = 1] ed -1 = h(p-1) = k(q-1) for some non-negative integers h and k.
To check (me)d ≡ m (mod pq) for ever integer m (p, q distinct prime numbers), it suffices to check that they are congruent mod p and mod q separately (the case mod q is completely analogous) and then applied the Chinese Remainder Theorem, Let m and n be relatively prime positive integers. For all integers a and b, the pair of congruences x ≡ a (mod m), x ≡ b (mod n) has a solution, and this solution is uniquely determined modulo mn.
Obviously, x = med, we will prove that med ≡ m (mod p), med ≡ m (mod q).
If m ≡ 0 (mod p), m is a multiple of p ⇒ med is a multiple of p ⇒ med ≡ 0 (mod p) and we are done.
med ≡ med-1m ≡ mh(p-1)m ≡ $(m^{p-1})^hm$ ≡ 1hm ≡ m (mod p). The most important step is that mp-1 ≡ 1 (mod p) by Fermat’s little theorem (ap ≡ a (mod p) or ap-1 ≡ 1 (mod p))
med ≡ med-1m ≡ mk(q-1)m ≡ $(m^{q-1})^km$ ≡[Fermat’s little theorem] 1hm ≡ m (mod q).
Euler’s totient function counts the positive integers up to a given integer n that are relatively prime to n. Euler’s theorem. If n is a positive integer, and a, n are coprime (gcd(a, n) = 1), then aΦ(n) ≡ 1 (mod n).
mde ≡ m1-kΦ(n) ≡ m(mΦ(n))-k ≡[By Euler’s Theorem. If m is not relatively prime to n, this argument is invalid, but improvable. This case can be treated using the previous proof.] m(1)-k ≡ m (mod n)
Since Eve knows e and n, can she calculate d? That’s relatively easy if she knows Φ(n). The totient function is multiplicative. Φ(1) = 1, and if gcd(a, b) = 1 ⇒ Φ(ab)=Φ(a)Φ(b)), so Φ(n) is easy to compute if one has a prime factorization of n and nearly impossible otherwise.
How do we create a secure Φ(n)? Let’s pick two large primes p and q with 45 digits plus, n = pq ⇒ Φ(n) = Φ(pq) = Φ(p)Φ(q) = [p and q are primes] (p - 1)(q - 1). In other words, knowing the factorization of n as pq, it’s very easy to compute Φ(n), otherwise it is is nearly impossible, Alice needs to keep her primes secret.