Last active
November 12, 2020 17:25
-
-
Save rafaelsq/87ac2309cc19744973e49e6305bfefcf to your computer and use it in GitHub Desktop.
Encrypt/Decrypt with KeyPair
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package main | |
import ( | |
"bytes" | |
"crypto/rand" | |
"crypto/rsa" | |
"crypto/sha256" | |
"crypto/x509" | |
"encoding/pem" | |
"fmt" | |
"log" | |
) | |
const ( | |
rawPrivateKey = `-----BEGIN RSA PRIVATE KEY----- | |
MIICXQIBAAKBgQDViO0h841VfHQJ8g1/AVBLbx3uXCVl0kjMaBPjUUWhoGnOZSWr | |
q9tdLe6ADC5SR01WiZwNKqnBDlkzgFaFNmOMAIaXFrur5cCc1zyfEAOnG4LS/s13 | |
Tu/J/ibIAPcNkyh5q3gHU6AcUgves5srpsH4DZPvmdrtd0xEFJ9P+ehdTwIDAQAB | |
AoGAP/CIUSpEn9xsQq4gLG+QQ4Y6SC1/ZUXaVpqzmdW0K/mEtTDEXutO/E/MorsD | |
A9Al8Eu0Vpz3x78sJqe/QJRQqgdA2NbvM7J200gRFUgkzUvpPoOWumKQk1gn5Z3T | |
aX6FER/TjLFLWej3//CXSZy3w47Tqk8Ts6eAz4DgU25nAMECQQDyi+TWzHBlhBzV | |
R38LkCSjBawfWfqZb18wi52KGJw+y3Wr5a3ik0wAb1U12Ees6c96cKY+/BZtuo9W | |
u6aUTp0rAkEA4WES/wVSpcYUHJ4Ecybw+xCVbvwfmelUCu2+yt3uBFYh4upfOQHQ | |
Xea6AwqpsJB1TJlo1iXf+H/6OY99X2lWbQJBAMNTmcTJ+zIlxLqVy2BagsauiMDu | |
y3vJTxVfOk2+KAYCAA+g9fnjuW8YOb0VIhPEb4whMCDFDqLrfQfSMwJ4lOUCQQC3 | |
AfxK2CekNJNgKW+zkYtcUrsEw/VlJPH5bdPfJxZ+rvIVL/rib3aiyReCIMZpeOPG | |
r0tRmK+lOUYWN022i/KBAkA80g4cL5LpPqbIG8cV62P2q1fujBOV4FuhuX7mLqQJ | |
0GLAx8IJKQeUmVbl0dO36CZ6BHMIqWRMFMWGmDIH2gyV | |
-----END RSA PRIVATE KEY-----` | |
rawPublicKey = `-----BEGIN PUBLIC KEY----- | |
MIGfMA0GCSqGSIb3DQEBAQUAA4GNADCBiQKBgQDViO0h841VfHQJ8g1/AVBLbx3u | |
XCVl0kjMaBPjUUWhoGnOZSWrq9tdLe6ADC5SR01WiZwNKqnBDlkzgFaFNmOMAIaX | |
Frur5cCc1zyfEAOnG4LS/s13Tu/J/ibIAPcNkyh5q3gHU6AcUgves5srpsH4DZPv | |
mdrtd0xEFJ9P+ehdTwIDAQAB | |
-----END PUBLIC KEY-----` | |
) | |
func main() { | |
content := []byte("something...") | |
// private key | |
block, _ := pem.Decode([]byte(rawPrivateKey)) | |
privateKey, err := x509.ParsePKCS1PrivateKey(block.Bytes) | |
if err != nil { | |
log.Fatal(err) | |
} | |
// public key | |
block, _ = pem.Decode([]byte(rawPublicKey)) | |
publicKey, err := x509.ParsePKIXPublicKey(block.Bytes) | |
if err != nil { | |
log.Fatal(err) | |
} | |
// encrypt | |
hash := sha256.New() | |
encrypted, err := rsa.EncryptOAEP(hash, rand.Reader, publicKey.(*rsa.PublicKey), content, nil) | |
if err != nil { | |
log.Fatal(err) | |
} | |
// decrypt | |
output, err := rsa.DecryptOAEP(hash, rand.Reader, privateKey, encrypted, nil) | |
if err != nil { | |
log.Fatal(err) | |
} | |
fmt.Println("IsEqual?", bytes.Equal(content, output)) | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package main | |
import ( | |
"bytes" | |
"crypto/rand" | |
"crypto/rsa" | |
"crypto/x509" | |
"encoding/pem" | |
"fmt" | |
"log" | |
) | |
const ( | |
rawPrivateKey = `-----BEGIN RSA PRIVATE KEY----- | |
MIICXQIBAAKBgQDViO0h841V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kyh5q3gHU6AcUgves5srpsH4DZPv | |
mdrtd0xEFJ9P+ehdTwIDAQAB | |
-----END PUBLIC KEY-----` | |
) | |
func main() { | |
content := []byte("something...") | |
// private key | |
block, _ := pem.Decode([]byte(rawPrivateKey)) | |
privateKey, err := x509.ParsePKCS1PrivateKey(block.Bytes) | |
if err != nil { | |
log.Fatal(err) | |
} | |
// public key | |
block, _ = pem.Decode([]byte(rawPublicKey)) | |
publicKey, err := x509.ParsePKIXPublicKey(block.Bytes) | |
if err != nil { | |
log.Fatal(err) | |
} | |
// encrypt | |
encrypted, err := rsa.EncryptPKCS1v15(rand.Reader, publicKey.(*rsa.PublicKey), content) | |
if err != nil { | |
log.Fatal(err) | |
} | |
// decrypt | |
output, err := rsa.DecryptPKCS1v15(rand.Reader, privateKey, encrypted) | |
if err != nil { | |
log.Fatal(err) | |
} | |
fmt.Println("IsEqual?", bytes.Equal(content, output)) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
https://levelup.gitconnected.com/a-guide-to-rsa-encryption-in-go-1a18d827f35d