Skip to main content

How to Convert a Private Key to BEGIN RSA PRIVATE KEY with openssl

· 4 min read
Jagdish Kumawat
Founder @ Dewiride

A private key file can start with BEGIN PRIVATE KEY or BEGIN RSA PRIVATE KEY. They hold the same key, but some software, such as the MySql.Data driver for .NET, reads only the second. One option converts it, and it's one that older guides leave out.

What You Need​

An RSA private key in a file, and OpenSSL. If the key is inside a MySQL container, the image has OpenSSL.

Step 1: See Which Format You Have​

The first line tells you:

Terminal
head -1 client-key.pem
Output
-----BEGIN PRIVATE KEY-----
First lineFormatWho writes it
-----BEGIN PRIVATE KEY-----PKCS#8OpenSSL 3 and later by default, and MySQL's generated files
-----BEGIN RSA PRIVATE KEY-----PKCS#1, or "traditional"OpenSSL with -traditional, and older versions

Step 2: Convert It​

Terminal
openssl rsa -in client-key.pem -traditional -out client-rsa-key.pem
head -1 client-rsa-key.pem
Output
writing RSA key
-----BEGIN RSA PRIVATE KEY-----

openssl rsa with -traditional writing an RSA key, and the new file's first line reading BEGIN RSA PRIVATE KEY.

It can also write over the same file, by giving -in and -out the same name. Older guides show openssl rsa -in key.pem -out key.pem with no -traditional. I tried that on OpenSSL 4.1 and 3.5.8, and in both cases the key stayed as BEGIN PRIVATE KEY.

For a key in a MySQL container, there's no need to install anything:

Terminal
docker compose exec --user mysql mysql openssl rsa -in /var/lib/mysql/client-key.pem -traditional -out /var/lib/mysql/client-rsa-key.pem
docker compose cp mysql:/var/lib/mysql/client-rsa-key.pem certs/client-key.pem

The new file is as secret as the old one, and openssl gives it the same restricted permissions. Copying a key out of a container or a server means one more place to protect.

Step 3: Use It​

With MySql.Data, this is what SslKey needs:

Connection string
…;SslMode=VerifyFull;SslCa=/Users/you/projects/mysql-connection-test/certs/ca.pem;SslCert=/Users/you/projects/mysql-connection-test/certs/client-cert.pem;SslKey=/Users/you/projects/mysql-connection-test/certs/client-rsa-key.pem
Output
Connected as inventory_app@% to MySQL 9.7.4
TLS: TLSv1.2

Common Mistakes​

MistakeWhat happensDo this instead
Leaving out -traditionalThe format doesn't change on OpenSSL 3 and laterAdd it
Editing the header lines by handThe contents differ, so it still failsConvert it
Running it on a certificateCould not find or decode private keyPoint it at the key

Troubleshooting​

SymptomLikely causeFix
The specified file cannot be converted to a keyYou're still pointing at the old fileThe fix
A key that isn't RSAopenssl rsa only reads RSACreate an RSA key, with -newkey rsa:2048
It asks for a passphraseThe key is encryptedType it, and the new file has none

Conclusion​

openssl rsa -in old.pem -traditional -out new.pem writes BEGIN RSA PRIVATE KEY. Without -traditional, recent versions keep the format you started with.

Additional Resources​

Stay Updated

Subscribe to our newsletter for the latest tutorials, tech insights, and developer news.

By subscribing, you agree to our privacy policy. Unsubscribe at any time.