thebossdev
thebossdev

Reputation: 134

GO RSA load public key

Hello i was wondering how i could load a RSA public key via a string in GO? I've read a few docs but i cant figure out how i could go about loading it? I don't wanna load via PEM i wanna load via the

-----BEGIN PUBLIC KEY-----
KEY
-----END PUBLIC KEY-----

Im trying to use rsa.EncryptOAEP but i need the public key parameter but i cant figure out how todo it.

func main() {
    pubPem, err := pem.Decode([]byte("KEY"))
    if err != nil {
        fmt.Println(string(err))
        return
    }
    if pubPem.Type != "RSA PUBLIC KEY" {
        fmt.Println("not rsa?")
    }
}

Ive tried this but it doesnt work because its pem.Decode (i think)

Upvotes: 1

Views: 6760

Answers (1)

Topaco
Topaco

Reputation: 49121

As already described in the comment, the posted key is PEM encoded. You may be confusing something.
The part called KEY is the Base64 encoded DER encoded key with line breaks after every 64 characters. If you have only this, it is easiest to add header and footer.
pem.Decode() works even without the line breaks in the body, only header and footer must be in separate lines.

From the header/footer lines it can be deduced that this is a public key in X.509/SPKI format. Importing a key of this format is supported by ParsePKIXPublicKey().

If you want to use OAEP as padding, the OAEP parameters must be specified for encryption. For decryption the same parameters must be used, otherwise decryption will fail.

OAEP uses two digests, one for hashing the label, one for the mask generation function MGF1. RFC8017 allows both to be chosen independently of each other. However, Go does not currently support this, i.e. the two digests must be chosen identically. Also, OAEP uses a label which is empty by default (and should not be changed for compatibility reasons).

The following code shows the import of an X.509/SPKI key and the encryption with OAEP using SHA256 as digest (for simplicity without exception handling):

import (
    "crypto/rand"
    "crypto/rsa"
    "crypto/sha256"
    "crypto/x509"
    "encoding/pem"
)

...

// X.509 SPKI key, PEM encoded
var spkiPem = `-----BEGIN PUBLIC KEY-----
MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEAoZ67dtUTLxoXnNEzRBFB
mwukEJGC+y69cGgpNbtElQj3m4Aft/7cu9qYbTNguTSnCDt7uovZNb21u1vpZwKH
yVgFEGO4SA8RNnjhJt2D7z8RDMWX3saody7jo9TKlrPABLZGo2o8vadW8Dly/v+I
d0YDheCkVCoCEeUjQ8koXZhTwhYkGPu+vkdiqX5cUaiVTu1uzt591aO5Vw/hV4DI
hFKnOTnYXnpXiwRwtPyYoGTa64yWfi2t0bv99qz0BgDjQjD0civCe8LRXGGhyB1U
1aHjDDGEnulTYJyEqCzNGwBpzEHUjqIOXElFjt55AFGpCHAuyuoXoP3gQvoSj6RC
sQIDAQAB
-----END PUBLIC KEY-----`

/*
//Works also:
var spkiPem = `-----BEGIN PUBLIC KEY-----
MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEAoZ67dtUTLxoXnNEzRBFBmwukEJGC+y69cGgpNbtElQj3m4Aft/7cu9qYbTNguTSnCDt7uovZNb21u1vpZwKHyVgFEGO4SA8RNnjhJt2D7z8RDMWX3saody7jo9TKlrPABLZGo2o8vadW8Dly/v+Id0YDheCkVCoCEeUjQ8koXZhTwhYkGPu+vkdiqX5cUaiVTu1uzt591aO5Vw/hV4DIhFKnOTnYXnpXiwRwtPyYoGTa64yWfi2t0bv99qz0BgDjQjD0civCe8LRXGGhyB1U1aHjDDGEnulTYJyEqCzNGwBpzEHUjqIOXElFjt55AFGpCHAuyuoXoP3gQvoSj6RCsQIDAQAB
-----END PUBLIC KEY-----`
*/

// Load X.509/SPKI key
spkiBlock, _ := pem.Decode([]byte(spkiPem))
var spkiKey *rsa.PublicKey
pubInterface, _ := x509.ParsePKIXPublicKey(spkiBlock.Bytes)
spkiKey = pubInterface.(*rsa.PublicKey)

// Encryption using OAEP
plaintext := []byte("The quick brown fox jumps over the lazy dog")
oaepLabel := []byte("")
oaepDigests := sha256.New()
ciphertext, _ := rsa.EncryptOAEP(oaepDigests, rand.Reader, spkiKey, plaintext, oaepLabel)
fmt.Println(ciphertext)

Upvotes: 5

Related Questions