JavaScript Algorithms: Cryptography
Katy Moe
Introduction
intro to crypto
the RSA algorithm
mathematical concepts
key generation
signing, encryption/decryption
Crypto is about keeping secrets
THIS IS A SECRET MESSAGE TO BE XORED WITH A SECRET KEY TO PRODUCE A CIPHERTEXT. THIS IS CALLED ENCRYPTION.
plaintext
HSD0472B1VX8V5BLR02010DHCV8B6W25N6KGBJV9SYS9C6 5BOG98383BY398Y98FR9GBBV2964783NHJFIFNJB90N7480
9GFGJFHGD76FVB2C1SD1SA2P408G00ZXBVB302479DHFJHAA83T1VSNXYBV8B9CBVA7F5395GKVBQ9X8GK2N1V49V09
+
=
key
ciphertext
crypto in JS!
home-made crypto!
RSA
Rivest, Shamir, Adleman (1977)
Cocks (1973, declassified 1997)
Puzzle
Bob wants to send a box to Alice.
Alice and Bob each have a distinct padlock and key (i.e. Alice has key which fits only her padlock, and Bob has a key which only fits his). Bob wants to lock a box and send it to Alice, but doesn't want Eve (who can intercept the package) to be able to open it. Eve has no padlocks or keys.
Alice and Bob can send messages to each other, as well as padlocks, keys, and boxes. A box is locked by attaching a padlock to it - the key is only required for unlocking. How can Bob send the box to Alice without Eve getting access to the contents?
Alice
Eve
Bob
H487SBFK593OSFH0N8BLSK4278SBTOB9P
Bob
Alice
+ Alice's public key
+ Alice's private key
Hello, Alice!
Hello, Alice!
Some mathematical concepts
Prime
A number only divisible by itself
2, 3, 5, 7, 11, 13, 17, 19, 23, 29...
Fundamental Theorem of Arithmetic
Every number has a unique set of prime factors
12 = 3 x 2 x 2
330 = 11 x 5 x 3 x 2
Coprime
Two numbers are coprime if they share no prime factors
12 = 3 x 2 x 2
35 = 7 x 5
Modular arithmetic
a is congruent to b mod n if they have the same remainder when divided by n
17 ≡ 5 (mod 12)
125 ≡ 5 (mod 12)
Euler's Totient
Euler's Totient ϕ(n) is the number of numbers less than or equal to n that are coprime to n
ϕ(4) = 2
ϕ(7) = 6
ϕ(2) = 1
Fermat's Little Theorem
a ≡ 1 (mod p)
p - 1
If p is prime and is coprime to a then:
RSA key generation
-
Large Primes: Generate two large prime numbers, p and q.
- Modulus: From the two primes, calculate the modulus n = p * q.
- Totient: Calculate Euler's Totient of n, ϕ(n).
- Public Key: A prime number is calculated from the range [3,ϕ(n)) that is coprime with ϕ(n).
- Private Key: Because the prime in step 4 has a gcd of 1 with ϕ(n), we are able to determine its inverse with respect to modϕ(n).
RSA encryption/decryption
For message m and key k:
F(m,k)= m (mod n)
k
RSA signing
-
Alice encrypts the ciphertext with her own private key and sends this along with the ciphertext to Bob.
-
Bob decrypts the signature with Alice's public key and checks whether it matches the ciphertext.
Advanced challenges
- [JS] Delete the helper functions and write them yourself.
- [JS] Expand the algorithm to deal with Unicode characters.
- [crypto/JS] Design a brute-force algorithm against RSA.
- [UX] How would you incorporate RSA into a messaging app?
- [crypto] Have a go at symmetric key exchange.
- [JS] How can signing be made more performant?
Let's code!
Resources
Encryption in JavaScript
- What's wrong with in-browser cryptography? [article]
- No I didn't use the Web Crypto API [article]
- JavaScript cryptography considered harmful [article]
- The anatomy of a bad idea [article]
RSA
JavaScript Algorithms: Cryptography
By Katharine Moe
JavaScript Algorithms: Cryptography
Supporting materials for the JavaScript Algorithms: Cryptography class (http://www.meetup.com/JavaScript-Algorithms-Study-Group/events/228613297/) on 18th February 2016.
- 1,904