Date: 2026-05-14

# Importing and exporting RSA/DSA/EC keys

#### 2026-05-14

The `openssl` package implements a modern interface to libssl and libcrypto for R. It builds on the new `EVP` api which was introduced in OpenSSL 1.0 and provides a unified API to the various methods and formats. OpenSSL supports three major public key crypto systems:

- RSA : Most popular method. Supports both encryption and signatures.
- DSA : Digital Signature Algorithm. Mostly for signatures, not very popular anymore.
- ECDSA : Elliptic Curve DSA. Supports signatures and encryption via Diffie Hellman. Gaining popularity.

For each type there are several common formats for storing keys and certificates:

- DER: binary format, serialized ASN.1 structure
- PEM: base64 encoded DER + header wrapped in `===`
- SSH: single line of base64 with a header. Only for pubkeys.
- JWK: JSON Web key. Stores key data in a JSON object

The openssl package automatically detects the format when possible. However being able to recognize the various formats can be useful.

### The DER format

DER is the standard binary format using by protocols for storing and exchanging keys and certificates. It consists of a serialized ASN.1 structure which hold the key’s (very large) prime numbers.

    key <- ec_keygen()
    pubkey <- key$pubkey
    bin <- write_der(pubkey)
    print(bin)

     [1] 30 59 30 13 06 07 2a 86 48 ce 3d 02 01 06 08 2a 86 48 ce 3d 03 01 07 03 42
    [26] 00 04 74 5c 61 2b b2 05 0e 4e 97 b2 e8 d1 bc 6e 46 da 34 2c f6 23 e8 f9 9f
    [51] 28 4e 19 e9 95 e2 a4 da f0 27 e2 d8 b4 18 90 7b ba 05 1f a5 4b d5 1d 00 c7
    [76] a8 a9 0f 50 43 85 37 3a 92 35 57 8b 1a 72 44 ca

To read a DER key use `read_key` or `read_pubkey` with `der = TRUE`.

    read_pubkey(bin, der = TRUE)

    [256-bit ecdsa public key]
    md5: 24c14878d343bd57dd98874690fae738
    sha256: 42e683e5e2e201e274a173390cf123a167dc402630943bd8810c9108a7fa5cdc

Users typically don’t need to worry about the key’s underlying primes, but have a look at `key$data` if you are curious.

### The PEM format

In practice the user rarely encounters DER because it is mainly for internal use. When humans exchange keys and certificates they typically use the PEM format. PEM is simply base64 encoded DER data, plus a header. The header identifies the key (and possibly encryption) type.

    cat(write_pem(pubkey))

    -----BEGIN PUBLIC KEY-----
    MFkwEwYHKoZIzj0CAQYIKoZIzj0DAQcDQgAEdFxhK7IFDk6XsujRvG5G2jQs9iPo
    +Z8oThnpleKk2vAn4ti0GJB7ugUfpUvVHQDHqKkPUEOFNzqSNVeLGnJEyg==
    -----END PUBLIC KEY-----

    cat(write_pem(key, password = NULL))

    -----BEGIN PRIVATE KEY-----
    MIGHAgEAMBMGByqGSM49AgEGCCqGSM49AwEHBG0wawIBAQQgOwdfvFjwJQCAy28I
    aVxoG054NNoFtxL/9Mi9yMYRknyhRANCAAR0XGErsgUOTpey6NG8bkbaNCz2I+j5
    nyhOGemV4qTa8Cfi2LQYkHu6BR+lS9UdAMeoqQ9QQ4U3OpI1V4sackTK
    -----END PRIVATE KEY-----

The PEM format allows for protecting private keys with a password. R will prompt you for the password when reading such a protected key.

    cat(write_pem(key, password = "supersecret"))

    -----BEGIN ENCRYPTED PRIVATE KEY-----
    MIHrMFYGCSqGSIb3DQEFDTBJMDEGCSqGSIb3DQEFDDAkBBA4LtuZf+OnLUgCaWLh
    HckpAgIIADAMBggqhkiG9w0CCQUAMBQGCCqGSIb3DQMHBAj6UsXPBPWkSQSBkH65
    2lX3Yvo9BP1HGpPdjxi1vfCbQ+oYVWdDHmfFomTsNfRXtMy2thp+ApWtvqoaT7v2
    vs/Lzc3gkplYVUlFUDIKIEFoVmnCMERgxU/8Y7p6mM/DfYrc2mfc4G9Hy9yLMuRs
    +6W9YNxZk9+BYuMEcRsg+2jeLHtL2VryW1OrGkLL+2jgiATdRfRfjS52ElsG8A==
    -----END ENCRYPTED PRIVATE KEY-----

### The OpenSSH format

For better or worse, OpenSSH uses a custom format for public keys. The advantage of this format is that it fits on a single line which is nice for e.g. your `~/.ssh/known_hosts` file. There is no special format for private keys, OpenSSH uses PEM as well.

    str <- write_ssh(pubkey)
    print(str)

    [1] "ecdsa-sha2-nistp256 AAAAE2VjZHNhLXNoYTItbmlzdHAyNTYAAAAIbmlzdHAyNTYAAABBBHRcYSuyBQ5Ol7Lo0bxuRto0LPYj6PmfKE4Z6ZXipNrwJ+LYtBiQe7oFH6VL1R0Ax6ipD1BDhTc6kjVXixpyRMo="

The `read_pubkey` function will automatically detect if a file contains a `PEM` or `SSH` key.

    read_pubkey(str)

    [256-bit ecdsa public key]
    md5: 24c14878d343bd57dd98874690fae738
    sha256: 42e683e5e2e201e274a173390cf123a167dc402630943bd8810c9108a7fa5cdc

### The JSON Web Key (JWK) format

Yet another recent format to store RSA or EC keys are JSON Web Keys (JWK). JWK is part of the Javascript Object Signing and Encryption (JOSE) specification. The `write_jwk` and `read_jwk` functions are implemented in a separate package which uses the `openssl` package.

    library(jose)
    json <- write_jwk(pubkey)
    jsonlite::prettify(json)

    {
        "kty": "EC",
        "crv": "P-256",
        "x": "dFxhK7IFDk6XsujRvG5G2jQs9iPo-Z8oThnpleKk2vA",
        "y": "J-LYtBiQe7oFH6VL1R0Ax6ipD1BDhTc6kjVXixpyRMo"
    }

Keys from `jose` and `openssl` are the same.

    mykey <- read_jwk(json)
    identical(mykey, pubkey)

    [1] TRUE

    print(mykey)

    [256-bit ecdsa public key]
    md5: 24c14878d343bd57dd98874690fae738
    sha256: 42e683e5e2e201e274a173390cf123a167dc402630943bd8810c9108a7fa5cdc