You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

228 lines
5.4 KiB

  1. package ownrsa
  2. import (
  3. "errors"
  4. "fmt"
  5. "math/big"
  6. "math/rand"
  7. "strconv"
  8. "strings"
  9. "time"
  10. )
  11. type RSAPublicKey struct {
  12. E *big.Int `json:"e"`
  13. N *big.Int `json:"n"`
  14. }
  15. type RSAPublicKeyString struct {
  16. E string `json:"e"`
  17. N string `json:"n"`
  18. }
  19. type RSAPrivateKey struct {
  20. D *big.Int `json:"d"`
  21. N *big.Int `json:"n"`
  22. }
  23. type RSA struct {
  24. PubK RSAPublicKey
  25. PrivK RSAPrivateKey
  26. }
  27. type PackRSA struct {
  28. PubK string `json:"pubK"`
  29. PrivK string `json:"privK"`
  30. Date time.Time `json:"date"`
  31. PubKSigned string `json:"pubKSigned"`
  32. }
  33. const maxPrime = 500
  34. const minPrime = 100
  35. func GenerateKeyPair() RSA {
  36. rand.Seed(time.Now().Unix())
  37. p := randPrime(minPrime, maxPrime)
  38. q := randPrime(minPrime, maxPrime)
  39. fmt.Print("p:")
  40. fmt.Println(p)
  41. fmt.Print("q:")
  42. fmt.Println(q)
  43. n := p * q
  44. phi := (p - 1) * (q - 1)
  45. e := 65537
  46. var pubK RSAPublicKey
  47. pubK.E = big.NewInt(int64(e))
  48. pubK.N = big.NewInt(int64(n))
  49. d := new(big.Int).ModInverse(big.NewInt(int64(e)), big.NewInt(int64(phi)))
  50. var privK RSAPrivateKey
  51. privK.D = d
  52. privK.N = big.NewInt(int64(n))
  53. var rsa RSA
  54. rsa.PubK = pubK
  55. rsa.PrivK = privK
  56. return rsa
  57. }
  58. func Encrypt(m string, pubK RSAPublicKey) []int {
  59. var c []int
  60. mBytes := []byte(m)
  61. for _, byte := range mBytes {
  62. c = append(c, EncryptInt(int(byte), pubK))
  63. }
  64. return c
  65. }
  66. func Decrypt(c []int, privK RSAPrivateKey) string {
  67. var m string
  68. var mBytes []byte
  69. for _, indC := range c {
  70. mBytes = append(mBytes, byte(DecryptInt(indC, privK)))
  71. }
  72. m = string(mBytes)
  73. return m
  74. }
  75. func EncryptBigInt(bigint *big.Int, pubK RSAPublicKey) *big.Int {
  76. Me := new(big.Int).Exp(bigint, pubK.E, nil)
  77. c := new(big.Int).Mod(Me, pubK.N)
  78. return c
  79. }
  80. func DecryptBigInt(bigint *big.Int, privK RSAPrivateKey) *big.Int {
  81. Cd := new(big.Int).Exp(bigint, privK.D, nil)
  82. m := new(big.Int).Mod(Cd, privK.N)
  83. return m
  84. }
  85. func EncryptInt(char int, pubK RSAPublicKey) int {
  86. charBig := big.NewInt(int64(char))
  87. Me := charBig.Exp(charBig, pubK.E, nil)
  88. c := Me.Mod(Me, pubK.N)
  89. return int(c.Int64())
  90. }
  91. func DecryptInt(val int, privK RSAPrivateKey) int {
  92. valBig := big.NewInt(int64(val))
  93. Cd := valBig.Exp(valBig, privK.D, nil)
  94. m := Cd.Mod(Cd, privK.N)
  95. return int(m.Int64())
  96. }
  97. func Blind(m []int, r int, pubK RSAPublicKey, privK RSAPrivateKey) []int {
  98. var mBlinded []int
  99. rBigInt := big.NewInt(int64(r))
  100. for i := 0; i < len(m); i++ {
  101. mBigInt := big.NewInt(int64(m[i]))
  102. rE := new(big.Int).Exp(rBigInt, pubK.E, nil)
  103. mrE := new(big.Int).Mul(mBigInt, rE)
  104. mrEmodN := new(big.Int).Mod(mrE, privK.N)
  105. mBlinded = append(mBlinded, int(mrEmodN.Int64()))
  106. }
  107. return mBlinded
  108. }
  109. func BlindSign(m []int, privK RSAPrivateKey) []int {
  110. var r []int
  111. for i := 0; i < len(m); i++ {
  112. mBigInt := big.NewInt(int64(m[i]))
  113. sigma := new(big.Int).Exp(mBigInt, privK.D, privK.N)
  114. r = append(r, int(sigma.Int64()))
  115. }
  116. return r
  117. }
  118. func Unblind(blindsigned []int, r int, pubK RSAPublicKey) []int {
  119. var mSigned []int
  120. rBigInt := big.NewInt(int64(r))
  121. for i := 0; i < len(blindsigned); i++ {
  122. bsBigInt := big.NewInt(int64(blindsigned[i]))
  123. //r1 := new(big.Int).Exp(rBigInt, big.NewInt(int64(-1)), nil)
  124. r1 := new(big.Int).ModInverse(rBigInt, pubK.N)
  125. bsr := new(big.Int).Mul(bsBigInt, r1)
  126. sig := new(big.Int).Mod(bsr, pubK.N)
  127. mSigned = append(mSigned, int(sig.Int64()))
  128. }
  129. return mSigned
  130. }
  131. func Verify(msg []int, mSigned []int, pubK RSAPublicKey) bool {
  132. if len(msg) != len(mSigned) {
  133. return false
  134. }
  135. var mSignedDecrypted []int
  136. for _, ms := range mSigned {
  137. msBig := big.NewInt(int64(ms))
  138. //decrypt the mSigned with pubK
  139. Cd := new(big.Int).Exp(msBig, pubK.E, nil)
  140. m := new(big.Int).Mod(Cd, pubK.N)
  141. mSignedDecrypted = append(mSignedDecrypted, int(m.Int64()))
  142. }
  143. fmt.Print("msg signed decrypted: ")
  144. fmt.Println(mSignedDecrypted)
  145. r := true
  146. //check if the mSignedDecrypted == msg
  147. for i := 0; i < len(msg); i++ {
  148. if msg[i] != mSignedDecrypted[i] {
  149. r = false
  150. }
  151. }
  152. return r
  153. }
  154. func HomomorphicMultiplication(c1 int, c2 int, pubK RSAPublicKey) int {
  155. c1BigInt := big.NewInt(int64(c1))
  156. c2BigInt := big.NewInt(int64(c2))
  157. c1c2 := new(big.Int).Mul(c1BigInt, c2BigInt)
  158. n2 := new(big.Int).Mul(pubK.N, pubK.N)
  159. d := new(big.Int).Mod(c1c2, n2)
  160. r := int(d.Int64())
  161. return r
  162. }
  163. func PubKStringToBigInt(kS RSAPublicKeyString) (RSAPublicKey, error) {
  164. var k RSAPublicKey
  165. var ok bool
  166. k.E, ok = new(big.Int).SetString(kS.E, 10)
  167. if !ok {
  168. return k, errors.New("error parsing big int E")
  169. }
  170. k.N, ok = new(big.Int).SetString(kS.N, 10)
  171. if !ok {
  172. return k, errors.New("error parsing big int N")
  173. }
  174. return k, nil
  175. }
  176. func PackKey(k RSA) PackRSA {
  177. var p PackRSA
  178. p.PubK = k.PubK.E.String() + "," + k.PubK.N.String()
  179. p.PrivK = k.PrivK.D.String() + "," + k.PrivK.N.String()
  180. return p
  181. }
  182. func UnpackKey(p PackRSA) RSA {
  183. var k RSA
  184. var ok bool
  185. k.PubK.E, ok = new(big.Int).SetString(strings.Split(p.PubK, ",")[0], 10)
  186. k.PubK.N, ok = new(big.Int).SetString(strings.Split(p.PubK, ",")[1], 10)
  187. k.PrivK.D, ok = new(big.Int).SetString(strings.Split(p.PrivK, ",")[0], 10)
  188. k.PrivK.N, ok = new(big.Int).SetString(strings.Split(p.PrivK, ",")[1], 10)
  189. if !ok {
  190. fmt.Println("error on Unpacking Keys")
  191. }
  192. return k
  193. }
  194. func ArrayIntToString(a []int, delim string) string {
  195. return strings.Trim(strings.Replace(fmt.Sprint(a), " ", delim, -1), "[]")
  196. }
  197. func StringToArrayInt(s string, delim string) []int {
  198. var a []int
  199. arrayString := strings.Split(s, delim)
  200. for _, s := range arrayString {
  201. i, err := strconv.Atoi(s)
  202. if err != nil {
  203. fmt.Println(err)
  204. }
  205. a = append(a, i)
  206. }
  207. return a
  208. }