CryptoAPI PUBLICKEYBLOB or RSAPublicKey to SubjectPublicKeyInfo Converter
Encodekey.c is a C console application for Win32
which converts a Microsoft CryptoAPI PUBLICKEYBLOB file,or an ASN.1 encoded RSAPublicKey file into
an ASN.1 SubjectPublicKeyInfo public key.
Encodekey first performs a basic test on the input file contents, using CryptDecodeObject(), to determine if the file is
either a valid RSAPublicKey blob or a SubjectPublicKeyInfo blob.
- If the input file is a PUBLICKEYBLOB structure, the application sequentially encodes twice using
CryptEncodeObject()
to generate an ANS.1 encoded pkcs#1 RSAPublicKey (with lpszStuctType = RSA_CSP_PUBLICKEYBLOB) and then
a pkcs #1 SubjectPublicKeyInfo (with lpszStuctType = X509_PUBLIC_KEY_INFO).
In this case, two encoded public key files are created: 'rsapublickey' and 'subjectpublickeyinfo' in the current directory.
- If the input file is a RSAPublicKey structure, the application encodes once using lpszStuctType = X509_PUBLIC_KEY_INFO
and the 'subjectpublickeyinfo' file is created.
- If the input file is already a SubjectPublicKeyInfo structure, the status is indicated and application exits with no files created.
- If the input file is NOT recognized as any of PUBLICKEYBLOB, RSAPublicKey or SubjectPublicKeyInfo the status is indicated and
the application exits with no files created.
A PUBLICKEYBLOB is typically generated by
CryptExportKey().
The RSAPublicKey form is commonly
used in CryptoAPI, whereas the higher encapsulation SubjectPublicKeyInfo is the encoded format usually more commonly
used in Java, .NET, OpenSSL etc..
Sample output for a PUBLICKEYBLOB file (for a 1024 bit RSA key):
C:\>encodekey.exe
Enter CryptoAPI PUBLICKEYBLOB or RSAPublicKey file: publickeyblob
Read 'publickeyblob' file into buffer: 148 bytes.
Got RSAPublicKey encoded data: 140 bytes
Wrote 140 bytes to file 'rsapublickey'
Got SubjectPublicKeyInfo encoded data: 162 bytes
Wrote 162 bytes to file 'subjectpublickeyinfo'
encodekey.exe (signed and timestamped; 83,112 bytes)
PUBLICKEYBLOB specification.
See also: JkeyNet: Using ASN.1 encoded public keys in .NET