Skip to content

Instantly share code, notes, and snippets.

@totoleo
Created October 21, 2019 12:58
Show Gist options
  • Save totoleo/385ad3927889da6f86ff91105542f41c to your computer and use it in GitHub Desktop.
Save totoleo/385ad3927889da6f86ff91105542f41c to your computer and use it in GitHub Desktop.
package ciphers
import (
"crypto/rand"
"crypto/rsa"
"crypto/sha1"
"crypto/x509"
"encoding/base64"
"testing"
)
func TestEncryptWithPublicKey(t *testing.T) {
pubkey:=`MFswDQYJKoZIhvcNAQEBBQADSgAwRwJAdedQ+RhUgHocW62HvHUX3Y/r/s/PfS8JhfscO3w1YSryW048No8+8oKMUmUoxTBKSBneVZPS/LnaFWQbIucpdwIDAQAB`
var dst =make([]byte,base64.RawStdEncoding.DecodedLen(len(pubkey)))
_, err := base64.RawStdEncoding.Decode(dst, []byte(pubkey))
if err != nil {
t.Error(err)
return
}
ifc, err := x509.ParsePKIXPublicKey(dst)
if err != nil {
panic(err)
}
key, ok := ifc.(*rsa.PublicKey)
if !ok {
panic("not ok")
}
hash := sha1.New()
ciphertext, err := rsa.EncryptOAEP(hash, rand.Reader, key, []byte("thisisatrickytext"), nil)
if err != nil {
panic(err)
}
t.Log(base64.RawStdEncoding.EncodeToString(ciphertext))
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment