Created
October 21, 2019 12:58
-
-
Save totoleo/385ad3927889da6f86ff91105542f41c to your computer and use it in GitHub Desktop.
This file contains hidden or 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 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