

0 / 2 embers
0 / 3000 xp
click for more info
Complete a lesson to start your streak
click for more info
Difficulty: 5
click for more info
Not enough gems
Cost: 6 gems
1: RSA
incomplete
2: RSA vs. ECC
incomplete
3: RSA Key Generation
incomplete
4: Totient and E
incomplete
5: Modular Arithmetic
incomplete
6: Modular Arithmetic
incomplete
7: Encryption
incomplete
8: Multiplicative Inverse
incomplete
9: Private Key
incomplete
10: Decryption
incomplete
11: Encryption and Decryption Explained
incomplete
Back
ctrl+,
Next
ctrl+.
This lesson's interactive features are locked, please to keep using them
Something you never want to do in the real world is to write your own cryptography code from scratch. Always use well-tested crypto libraries in production.
That said, we've been asked at Passly to write our own RSA from scratch to compare against the Go standard library's implementation. Don't worry, it's just for benchmarking purposes.
First of all, we need to generate some really big numbers. Go has a library for that: math/big. It's a bit tricky to use, but we'll figure it out together.
p and qp and q together, we'll call this nComplete the generatePrivateNums and getN functions.
func generatePrivateNums(keysize int) (*big.Int, *big.Int)
Use the provided getBigPrime function to generate two prime numbers of the given keysize in bits. Be sure to generate p first, as we're relying on the determinism of the random number generator. (Normally we would use the crypto/rand package's rand.Prime if we wanted true randomness).
func getN(p, q *big.Int) *big.Int
Use the math/big package's .Mul method to multiply p and q together to get n.
Notice that we're using *big.Int instead of int or int64. That's because we need to be able to handle numbers that are too big to fit in a regular integer. Most of the arithmetic with big integers is done through methods on the *big.Int type, and they mutate the pointer receiver.
You might notice that it takes a minute or so to generate those numbers, and that's because they're really big. Think about a 600-digit number! That's a 1 followed by 599 zeroes.