New initializes the RSA algorithm returns the RSA object
()
| 30 | // New initializes the RSA algorithm |
| 31 | // returns the RSA object |
| 32 | func New() *rsa { |
| 33 | // The following code generates keys for RSA encryption/decryption |
| 34 | // 1. Choose two large prime numbers, p and q and compute n = p * q |
| 35 | p, q := randomPrime() // p and q stands for prime numbers |
| 36 | modulus := p * q // n stands for common number |
| 37 | |
| 38 | // 2. Compute the totient of n, lcm(p-1, q-1) |
| 39 | totient := uint64(lcm.Lcm(int64(p-1), int64(q-1))) |
| 40 | |
| 41 | // 3. Choose an integer e such that 1 < e < totient(n) and gcd(e, totient(n)) = 1 |
| 42 | publicKey := uint64(2) // e stands for encryption key (public key) |
| 43 | for publicKey < totient { |
| 44 | if gcd.Recursive(int64(publicKey), int64(totient)) == 1 { |
| 45 | break |
| 46 | } |
| 47 | publicKey++ |
| 48 | } |
| 49 | |
| 50 | // 4. Compute d such that d * e ≡ 1 (mod totient(n)) |
| 51 | inv, _ := modular.Inverse(int64(publicKey), int64(totient)) |
| 52 | privateKey := uint64(inv) |
| 53 | |
| 54 | return &rsa{ |
| 55 | publicKey: publicKey, |
| 56 | privateKey: privateKey, |
| 57 | modulus: modulus, |
| 58 | } |
| 59 | } |
| 60 | |
| 61 | // EncryptString encrypts the data using RSA algorithm |
| 62 | // returns the encrypted string |