Skip to content

Instantly share code, notes, and snippets.

@jemygraw
Created November 13, 2014 02:24
Show Gist options
  • Save jemygraw/4da51d58b349bfddd0c5 to your computer and use it in GitHub Desktop.
Save jemygraw/4da51d58b349bfddd0c5 to your computer and use it in GitHub Desktop.
Golang RSA-OAEP Encrypt&Decrypt
package main
import (
"crypto/rand"
"crypto/rsa"
"crypto/sha1"
"crypto/x509"
"encoding/base64"
"encoding/pem"
"fmt"
)
func main() {
var publicKeyData = `-----BEGIN PUBLIC KEY-----
MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEAygGoUiTD+LjwZIgwFZyj
iibWNQ2LM9xZ2pjKQGP8iUBtAuAW629/Ofw8qxToMyixPrG4A7j8+KOPwYrWPGV6
Og//4zm3cG+1hQvnNUWtMjHHBY8OByUPQ6/T8XHER1DxFBfnWfFLZ1yFX6oNNuvt
LgOreI6ehehJd5IB/4mOjMvFEBgOEejado2n55VNdcFpdQ3RcvGV+f/rl/lsIM08
QvL3lc5gqawj53sW9YZi1DL/uN48R+ghvAYhtx2jpHDBvlH1NCF1rU6CynYsgV9Q
Iksv0ihwl4T+k5F9ir0uv0WIS6kKKS1SRpAprRKunos4PlE8l2+jC6LaJUPhDZlj
/wIDAQAB
-----END PUBLIC KEY-----
`
var privateKeyData = `
-----BEGIN RSA PRIVATE KEY-----
MIIEogIBAAKCAQEAygGoUiTD+LjwZIgwFZyjiibWNQ2LM9xZ2pjKQGP8iUBtAuAW
629/Ofw8qxToMyixPrG4A7j8+KOPwYrWPGV6Og//4zm3cG+1hQvnNUWtMjHHBY8O
ByUPQ6/T8XHER1DxFBfnWfFLZ1yFX6oNNuvtLgOreI6ehehJd5IB/4mOjMvFEBgO
Eejado2n55VNdcFpdQ3RcvGV+f/rl/lsIM08QvL3lc5gqawj53sW9YZi1DL/uN48
R+ghvAYhtx2jpHDBvlH1NCF1rU6CynYsgV9QIksv0ihwl4T+k5F9ir0uv0WIS6kK
KS1SRpAprRKunos4PlE8l2+j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hMQu/nEn/6/laYhar
yZWLAoGASvCrpFKn0qllMKNUetBmYFpgtjmnNuW7l0xT2UftkW6AuFjU19gKgXhe
I+uZciHQ8kIUHfNLYBbhETsF3iqsklKfeoIr23zYHLE5GpoC151IpKf4guoPbCHX
a1oCDuZm//f5HMePb9juJN0WR//d5jWuizAycZf41XoEd8Bqydg=
-----END RSA PRIVATE KEY-----
`
pubKeyBlock, _ := pem.Decode([]byte(publicKeyData))
hash := sha1.New()
random := rand.Reader
msg := []byte("helloworldjemyme")
var pub *rsa.PublicKey
pubInterface, parseErr := x509.ParsePKIXPublicKey(pubKeyBlock.Bytes)
if parseErr != nil {
fmt.Println("Load public key error")
panic(parseErr)
}
pub = pubInterface.(*rsa.PublicKey)
encryptedData, encryptErr := rsa.EncryptOAEP(hash, random, pub, msg, nil)
if encryptErr != nil {
fmt.Println("Encrypt data error")
panic(encryptErr)
}
encodedData := base64.URLEncoding.EncodeToString(encryptedData)
fmt.Println(encodedData)
fmt.Println()
fmt.Println("------I AM JUST A LINE------")
fmt.Println()
privateKeyBlock, _ := pem.Decode([]byte(privateKeyData))
var pri *rsa.PrivateKey
pri, parseErr = x509.ParsePKCS1PrivateKey(privateKeyBlock.Bytes)
if parseErr != nil {
fmt.Println("Load private key error")
panic(parseErr)
}
decryptedData, decryptErr := rsa.DecryptOAEP(hash, random, pri, encryptedData, nil)
if decryptErr != nil {
fmt.Println("Decrypt data error")
panic(decryptErr)
}
fmt.Println(string(decryptedData))
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment