CryptoAPI PUBLICKEYBLOB to Java PublicKey Converter


MSKeytoJKey.java is a simply utility Java class which takes a Microsoft CryptoAPI PUBLICKEYBLOB file and parses to instantiate a Java PublicKey object and optionally, writes out an ASN.1 encoded SubjectPublicKeyInfo public key file:
     java MSKeytoJKey  PUBLICKEYBLOBfile  [SubjectPublicKeyInfo_pubkey]
Since Java generally reads/writes multi-byte data in big-endian format, but the PUBLICKEYBLOB has members formatted in little-endian order, the parsing in Java must reformat the data and reverse byte arrays.

The class contains two static methods:

 public static PublicKey getPublicKey (String mspublickeyblobfile)

 public static PublicKey getPublicKey (byte[] mspublickeyblob) 

The first method simply reads the file into a byte[] and calls the second method. The PUBLICKEYBLOB file is parsed according to the documented PUBLICKEYBLOB specification, and any nonconformity with the specification causes the method to return null. The encoded SubjecPublicKeyInfo data is obtained with PublicKey.getEncoded().

See also: X.509 SubjectPublicKeyInfo to CryptoAPI PUBLICKEYBLOB Converter