|
| 1 | +package main |
| 2 | + |
| 3 | +import ( |
| 4 | + //"math/big" |
| 5 | + "math/rand" |
| 6 | + "math" |
| 7 | + "time" |
| 8 | + "fmt" |
| 9 | +) |
| 10 | +func generatePrimes(limit int)int{ |
| 11 | + /* |
| 12 | + generate primes by factoring |
| 13 | + relies on the 30k+i, though better formulae exist |
| 14 | + where k >=0 and i = (1,7,11,13,17,13,19,23,29) |
| 15 | + */ |
| 16 | + primes:= prime(limit) |
| 17 | + var choice []int |
| 18 | + choice = append(choice, 1,7,11,13,17,19,23,29) |
| 19 | + for{ |
| 20 | + k:=rand.Intn(int(limit/30)) |
| 21 | + i:=choice[rand.Intn(len(choice))] |
| 22 | + c:=30*k+i |
| 23 | + found := true |
| 24 | + for _,v:= range primes{ |
| 25 | + if c%v==0{ |
| 26 | + found = false |
| 27 | + break |
| 28 | + } |
| 29 | + } |
| 30 | + if found{ |
| 31 | + return c |
| 32 | + } |
| 33 | + } |
| 34 | +} |
| 35 | +func prime(limit int)(primes []int){ |
| 36 | + sqrtLimit:=int(math.Ceil(math.Sqrt(float64(limit)))) |
| 37 | + exit:= false |
| 38 | + primes = append(primes,2,3,5) |
| 39 | + lastIndex :=2 |
| 40 | + for ;primes[lastIndex]<sqrtLimit;{ |
| 41 | + if exit == true{ |
| 42 | + break |
| 43 | + } |
| 44 | + for i:=primes[lastIndex]+2;i<primes[lastIndex]*primes[lastIndex];i+=2{ |
| 45 | + found:= true |
| 46 | + for _,v:= range primes { |
| 47 | + if i%v==0{ |
| 48 | + found= false |
| 49 | + break |
| 50 | + } |
| 51 | + } |
| 52 | + if found{ |
| 53 | + primes = append(primes,i) |
| 54 | + lastIndex++ |
| 55 | + if i >=sqrtLimit{ |
| 56 | + exit = true |
| 57 | + break |
| 58 | + } |
| 59 | + } |
| 60 | + |
| 61 | + } |
| 62 | + } |
| 63 | + return |
| 64 | +} |
| 65 | +func lcm (a int, b int)int{ |
| 66 | + //complexity depende |
| 67 | + return int((a*b)/gcd(a,b)) |
| 68 | + |
| 69 | +} |
| 70 | +func gcd (a int, b int) int{ |
| 71 | + //complexity not clear |
| 72 | + for b != 0{ |
| 73 | + t:=b |
| 74 | + b = a % b |
| 75 | + a = t |
| 76 | + } |
| 77 | + return a |
| 78 | +} |
| 79 | +func modularMultiplicativeInverse(e int, delta int)int{ |
| 80 | + //runs in O(n) where n = delta |
| 81 | + e= e % delta |
| 82 | + for i:=1;i<delta;i++{ |
| 83 | + if (i*e)%delta==1{ |
| 84 | + return i |
| 85 | + } |
| 86 | + } |
| 87 | + return 0 |
| 88 | +} |
| 89 | + |
| 90 | +func modularExponentiation(b int, e int, mod int)int{ |
| 91 | + //runs in O(log(n)) where n = e |
| 92 | + if mod == 1{ |
| 93 | + return 0 |
| 94 | + } |
| 95 | + r:=1 |
| 96 | + b = b % mod |
| 97 | + for ;e>0;{ |
| 98 | + if e%2==1{ |
| 99 | + r=(r*b)%mod |
| 100 | + } |
| 101 | + e =e>>1 |
| 102 | + b = (b*b)%mod |
| 103 | + } |
| 104 | + return r |
| 105 | +} |
| 106 | + |
| 107 | +func encryptRSA(message []int,e int,n int)[]int{ |
| 108 | + //runs in O(k*log(n)) where k = len(message) and n = e |
| 109 | + var ciphertext []int |
| 110 | + for _,v := range message{ |
| 111 | + ciphertext = append(ciphertext, modularExponentiation(v,e,n)) |
| 112 | + } |
| 113 | + return ciphertext |
| 114 | +} |
| 115 | +func decryptRSA(ciphertext []int, d int, n int )[]int{ |
| 116 | + //runs in O(k*log(n)) where k = len(ciphertext) and n = d |
| 117 | + var message []int |
| 118 | + for _,v := range ciphertext { |
| 119 | + message = append(message, modularExponentiation(v,d,n)) |
| 120 | + } |
| 121 | + return message |
| 122 | +} |
| 123 | +func toASCII(slice []rune)[]int{ |
| 124 | + //runs in O(n) where n = len(slice) |
| 125 | + var converted []int |
| 126 | + for _,v:= range slice{ |
| 127 | + converted = append(converted, int(v)) |
| 128 | + } |
| 129 | + return converted |
| 130 | +} |
| 131 | + |
| 132 | +func toRune(slice []int)string{ |
| 133 | + //runs in O(n) where n = len(slice) |
| 134 | + var str string |
| 135 | + for _,v:= range slice{ |
| 136 | + str += string(v) |
| 137 | + } |
| 138 | + return str |
| 139 | +} |
| 140 | + |
| 141 | + |
| 142 | +func main(){ |
| 143 | + rand.Seed(time.Now().UTC().UnixNano()) |
| 144 | + bits:=15 |
| 145 | + |
| 146 | + p:= generatePrimes(1<<bits) |
| 147 | + q:= generatePrimes(1<<bits) |
| 148 | + for p==q{ |
| 149 | + q = generatePrimes(1<<bits) |
| 150 | + } |
| 151 | + |
| 152 | + n:= p*q |
| 153 | + |
| 154 | + delta:=lcm(p-1,q-1) |
| 155 | + |
| 156 | + e:=generatePrimes(delta) |
| 157 | + d:=modularMultiplicativeInverse(e,delta) |
| 158 | + |
| 159 | + fmt.Printf("%v \n%v \n%v \n%v\n",p,q,e,d) |
| 160 | + |
| 161 | + |
| 162 | + str:="I think RSA is really great" |
| 163 | + message := []rune(str) |
| 164 | + asciiSlice :=toASCII(message) |
| 165 | + |
| 166 | + fmt.Printf("asciiSlice :%v \n",asciiSlice) |
| 167 | + encrypted := encryptRSA(asciiSlice,e,n) |
| 168 | + fmt.Printf("encrypted :%v \n",encrypted) |
| 169 | + decrypted := decryptRSA(encrypted,d,n) |
| 170 | + fmt.Printf("decrypted : %v \n",decrypted) |
| 171 | + fmt.Printf("cleartext : %v \n",toRune(decrypted)) |
| 172 | +} |
0 commit comments