# Rsa

### Creating A Keys

Creating 2048 bits rsa keys.

{% code title="main.go" %}

```go
package main

import (
	"fmt"
	helper_rsa "github.com/langwan/langgo/helpers/crypto/rsa"
)

func main() {
	publicKeyString, privateKeyString := helper_rsa.CreateKeyX509PKCS1(2048)
	fmt.Printf("publicKeyString = %v\n", publicKeyString)
	fmt.Printf("privateKeyString = %v\n", privateKeyString)
	publicKey, err := helper_rsa.PublicKeyFromX509PKCS1(publicKeyString)
	if err != nil {
		panic(err)
	}
	privateKey, err := helper_rsa.PrivateKeyFromX509PKCS1(privateKeyString)
	if err != nil {
		panic(err)
	}
	fmt.Printf("publicKey = %v\n", publicKey)
	fmt.Printf("privateKey = %v\n", privateKey)
}
```

{% endcode %}

{% code title="output" overflow="wrap" %}

```
publicKeyString = MIIBCgKCAQEA3AK+ArvjAoYZPOKd30w8CpPKkv49DR9ECfM5Hql0XuSo7+b29m7pCLWt+ac0skCHWlHS2kQUx6Kb5U8wHbDkBbx9FYRaWZOmtBnjA5sQmBjPsCXc3jxnpfXgEMVkK33z4BzG1IES/O5cbYMoEmqjeXPHi7NeaI+BlPUIKqa99Es/Zn1xYJh10KY8o2Lm50w3yC13GWW/3NLup/FaTJcVi2NhO4hxvmSWB6xj2YX2UHLhf4tmKxRY0sH7CrQtDJ69wELpMzrpSsstXsqYsTNjKP+CGwckBmml+7M9+YlZXw7mRUK2DAYlq4TOik/tzp76OB+SZ13yIxiIZSsI/6EX1wIDAQAB
privateKeyString = MIIEpAIBAAKCAQEA3AK+ArvjAoYZPOKd30w8CpPKkv49DR9ECfM5Hql0XuSo7+b29m7pCLWt+ac0skCHWlHS2kQUx6Kb5U8wHbDkBbx9FYRaWZOmtBnjA5sQmBjPsCXc3jxnpfXgEMVkK33z4BzG1IES/O5cbYMoEmqjeXPHi7NeaI+BlPUIKqa99Es/Zn1xYJh10KY8o2Lm50w3yC13GWW/3NLup/FaTJcVi2NhO4hxvmSWB6xj2YX2UHLhf4tmKxRY0sH7CrQtDJ69wELpMz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kqzmeXGa02535pDYHizkNrODUBq+kR+EfqTE/G8Mt2PuhkHJIECgYBuZU2PD3EJvc+opSImpznB+M8kMS56v35LsQJ9c6Q0KKcx0SMxvnxrcT/cOhbckwzaLXkho1g24aDHgEbJ71ClZoThufAp2RurPt1caCNG0DP+Z4kphmjEiDNBxHSETudodCy25MSFoRQkliWGOEXami/4HzuX38WV+1BgAZtd4w==
publicKey = &{27773779336158363924842658450535593808757640684548715697365660245795232066158892120996707474320449197190815872528139222287810096186369039421920755766774002047153956836594453593293155335579270597381205838434300143694079403092854708102376331916088574874721541993360865972984165899082742617744843780824910561667334712063461122070640761712575027163847032865045170681797028755617295697373381197299157067178094492702033513226373883346740462902362486722935840398270137676446766237328975813620433106776084766340157896476561757478699950057525382396118346897881870109408299318747757332932607987520915567748676769159148818470871 65537}
privateKey = &{{27773779336158363924842658450535593808757640684548715697365660245795232066158892120996707474320449197190815872528139222287810096186369039421920755766774002047153956836594453593293155335579270597381205838434300143694079403092854708102376331916088574874721541993360865972984165899082742617744843780824910561667334712063461122070640761712575027163847032865045170681797028755617295697373381197299157067178094492702033513226373883346740462902362486722935840398270137676446766237328975813620433106776084766340157896476561757478699950057525382396118346897881870109408299318747757332932607987520915567748676769159148818470871 65537} 4465875561048519752811265922330043907360544692826561576191148933734991768210055467462094745935714079680132103463715628186657045540869385803869583964329530396156497812594905961001011198656245381314420054708342690606042521778422920090679185585726252376975076819598651229432338072303186622912174249085751674608951463462967938882498197822839003777983978024904516200366381849220006721528454402403224149975559244036585263150239768878967427684271343253714133975976762052243317105366724620046218415050886135523460432010933933312214521987185558037030025438449061047935582125404441881345014952292164076496672671833032235294273 [178819592350655492787950212031906825061554584719182399925701784802801839690011089761109799966150772510565999620018856622316289940280110791029928478471257644778321323341225690985285165476456205166206661118521203558496921245774872010708910324309856321698657567513403971238733849890091116717402780733588269625031 155317317141040639127779168468742791078516065658377898173133077065055585828149366382220628670643077144808907506785494268465897374296171451106595314789480276698785288402341940440327646443875603895432889115814591927168000583037289938186360358005861396501229742064595572957088701269887168685246573933110975446641] {141763442638061809104639229997249660541345041788147775615907989856331116513331647460643922777077521652791050494494708556526315823690334257881059005255850316494536867349996521061865450928403170661083560203761045700676961720028064302430568149439902574884041878293580980662521261346866259672694491291855167869113 137398367007476266751180631874266934030670421633735401749936116635239065626635147555970263634423349261446834942298179893931349713477803928305475837622028907668779100056672968536375415875461678310597329438003979150243251931003401459576917285132792473313628874161725329779516525700336885907134219911592564696193 77522507203621544280047197994025669342510920403973149204522909602421619316552927287914879201913910141129505973438350691718270902261544961162467396634090150843502466581660955811421506473207548765836079457336546669972318144559847554325664826934015724163341235603376868209743875748404123032936003402905490775523 []}}
```

{% endcode %}

{% embed url="<https://github.com/langwan/langgo-examples/tree/main/0.5.x/Helpers/Rsa/Creating-A-Keys>" %}

### Encryption And Decryption

{% code title="main.go" %}

```go
package main

import (
   "fmt"
   helper_rsa "github.com/langwan/langgo/helpers/crypto/rsa"
)

func main() {
   bits := 2048
   data := "0123456789"

   publicKeyString, privateKeyString := helper_rsa.CreateKeyX509PKCS1(bits)
   publicKey, err := helper_rsa.PublicKeyFromX509PKCS1(publicKeyString)
   if err != nil {
      panic(err)
   }
   privateKey, err := helper_rsa.PrivateKeyFromX509PKCS1(privateKeyString)
   if err != nil {
      panic(err)
   }
   encrypt, err := helper_rsa.Encrypt(publicKey, []byte(data))
   if err != nil {
      panic(err)
   }
   fmt.Printf("encrypt=%v\n", encrypt)
   decrypt, err := helper_rsa.Decrypt(privateKey, encrypt)
   if err != nil {
      panic(err)
   }
   fmt.Printf("decrypt=%s\n", string(decrypt))
}
```

{% endcode %}

{% code title="output" %}

```
encrypt=[98 248 113 181 2 14 252 180 161 213 254 63 185 190 179 188 220 254 140 142 136 66 216 66 252 50 181 56 132 91 111 84 88 129 210 38 238 216 137 55 106 67 202 76 31 178 255 246 54 4 138 153 235 225 211 42 230 84 247 239 20 82 22 32 25 215 76 186 121 59 11 21 219 255 196 14 163 100 6 234 203 23 12 74 67 197 185 219 141 36 164 19 140 94 185 57 145 28 225 69 65 2 162 154 85 96 1 55 130 231 39 99 54 106 120 243 2 70 6 182 198 157 10 147 175 236 36 85 149 191 90 106 242 99 1 203 225 136 225 55 91 250 28 156 31 181 91 37 192 58 161 248 143 122 175 82 175 119 57 115 72 206 214 12 173 159 200 216 164 250 82 250 141 160 160 193 231 152 31 141 121 136 123 14 236 14 57 52 202 138 112 47 123 206 246 16 16 23 111 20 138 122 255 91 0 19 35 88 193 176 79 4 90 14 165 234 247 134 170 61 161 47 165 202 110 12 203 236 74 178 223 136 18 160 145 88 188 246 230 116 94 97 232 218 50 86 154 122 105 149 225 51 219 98 128 131]
decrypt=0123456789
```

{% endcode %}

{% embed url="<https://github.com/langwan/langgo-examples/tree/main/0.5.x/Helpers/Rsa/Encryption-And-Decryption>" %}

### Sign And Verify

{% code title="main.go" %}

```go
package main

import (
   "fmt"
   helper_rsa "github.com/langwan/langgo/helpers/crypto/rsa"
)

func main() {
   bits := 2048
   publicKeyString, privateKeyString := helper_rsa.CreateKeyX509PKCS1(bits)
   data := "0123456789"
   publicKey, err := helper_rsa.PublicKeyFromX509PKCS1(publicKeyString)
   if err != nil {
      panic(err)
   }
   privateKey, err := helper_rsa.PrivateKeyFromX509PKCS1(privateKeyString)
   if err != nil {
      panic(err)
   }

   sign, err := helper_rsa.Sign(privateKey, []byte(data))
   if err != nil {
      panic(err)
   }
   err = helper_rsa.Verify(publicKey, sign, []byte(data))
   if err != nil {
      panic(err)
   }
   fmt.Println("verify ok")
}
```

{% endcode %}

{% embed url="<https://github.com/langwan/langgo-examples/tree/main/0.5.x/Helpers/Rsa/Sign-And-Verify>" %}

{% code title="output" %}

```
verify ok
```

{% endcode %}
