Forum Stats

  • 3,838,499 Users
  • 2,262,376 Discussions
  • 7,900,672 Comments

Discussions

Java : how to create java.security.Key from .asc file?

user4399002
user4399002 Member Posts: 2

I have to encrypt a file to pgp format. I have my public key in .asc format.

The cipher init() method needs a public key to be passed in. What is way to load that key from the file like this? In my case it is .asc file. And the details ablout the key file is described below:

I am getting an error: " java.security.spec.InvalidKeySpecException: java.security.InvalidKeyException: invalid key format "   when I am trying to encrypt a file.

Here is my encrypt method:

public static CipherOutputStream encrypt(OutputStream outputStream, String publicKeyPath)

  {

  Cipher cipher;

    Key publicKey = null;

  

    try

    {

  cipher = Cipher.getInstance("RSA", "BC");

  } catch (NoSuchAlgorithmException | NoSuchProviderException | NoSuchPaddingException e) {

  String msg = "failed to create output stream";

            System.out.println(  msg + " : " + e.getMessage());

            e.printStackTrace();

            throw new RuntimeException( msg, e );

  }

  

    try {

    publicKey = getPublicKey(publicKeyPath);

  } catch (Exception e) {

  // TODO Auto-generated catch block

  e.printStackTrace();

  }

  

    try {

  cipher.init(Cipher.ENCRYPT_MODE, publicKey);

  } catch (InvalidKeyException e) {

  // TODO Auto-generated catch block

  e.printStackTrace();

  }

  

    return ( new CipherOutputStream(outputStream, cipher));

  }

I am getting error: java.security.spec.InvalidKeySpecException: java.security.InvalidKeyException: invalid key format

My getPublicKey method looks like :

public static PublicKey getPublicKey(String filename)

       throws Exception {

  File pubKeyFile = new File(filename);

  DataInputStream dis = new DataInputStream(new FileInputStream(pubKeyFile));

        byte[] pubKeyBytes = new byte[(int)pubKeyFile.length()];

        dis.readFully(pubKeyBytes);

        dis.close();

       

        KeyFactory keyFactory = KeyFactory.getInstance("RSA");

       

        // decode public key

        X509EncodedKeySpec pubSpec = new X509EncodedKeySpec(pubKeyBytes);

        return keyFactory.generatePublic(pubSpec);

       

}

Public_key.asc looks like :

<span class="pun" style="color: #303336;">-----</span><span class="pln" style="color: #303336;">BEGIN PGP PUBLIC KEY BLOCK</span><span class="pun" style="color: #303336;">-----</span><span class="pln" style="color: #303336;"><br/>   </span><span class="typ" style="color: #2b91af;">Version</span><span class="pun" style="color: #303336;">:</span><span class="pln" style="color: #303336;"> </span><span class="typ" style="color: #2b91af;">Encryption</span><span class="pln" style="color: #303336;"> </span><span class="typ" style="color: #2b91af;">Desktop</span><span class="pln" style="color: #303336;"> </span><span class="lit" style="color: #7d2727;">10.3</span><span class="pun" style="color: #303336;">.</span><span class="lit" style="color: #7d2727;">2</span><span class="pln" style="color: #303336;"> </span><span class="pun" style="color: #303336;">(</span><span class="typ" style="color: #2b91af;">Build</span><span class="pln" style="color: #303336;"> </span><span class="lit" style="color: #7d2727;">16127</span><span class="pun" style="color: #303336;">)</span><span class="pln" style="color: #303336;"><br/><br/>  mQENBFYhXNoBCACgX</span><span class="pun" style="color: #303336;">/</span><span class="pln" style="color: #303336;">u03wm8cLqmTZiKGx6H</span><span class="pun" style="color: #303336;">/</span><span class="lit" style="color: #7d2727;">1ZUoBsfaDB2rdG2D8jYQzvaq4MA</span><span class="pln" style="color: #303336;"><br/>  hZWBEVhA2BGKrNI</span><span class="pun" style="color: #303336;">+</span><span class="pln" style="color: #303336;">a2SDhKGAY4OK7aUZVAVG1bfQNVdNe80TbEF8g</span><span class="pun" style="color: #303336;">/</span><span class="pln" style="color: #303336;">wO2FreYPkb<br/>  ojPtkwgyzsvb1BKwgRM1UMjkM5OWnhAPDhFDc39SFbmHLsXrURqFqJd9T3xzF6ty<br/><br/>   </span><span class="pun" style="color: #303336;">................................................................</span><span class="pln" style="color: #303336;"><br/><br/><br/>  D4WXvHpPXCJcwCBe</span><span class="pun" style="color: #303336;">+/</span><span class="lit" style="color: #7d2727;">81ZpjxlrLkUu8bO79jxZdKcI5ZRpmIe</span><span class="pun" style="color: #303336;">/</span><span class="typ" style="color: #2b91af;">VPJoDUVKLvl9n3</span><span class="pln" style="color: #303336;"><br/>   </span><span class="typ" style="color: #2b91af;">ANvDJGdGcW3x6RyL9QOnoRDf6njimqcTm8UqImdLCz4TFdv94dvM4K0NOWuFdYal</span><span class="pln" style="color: #303336;"><br/>  E9Q</span><span class="pun" style="color: #303336;">+</span><span class="pln" style="color: #303336;">U0Q7aiaWn</span><span class="pun" style="color: #303336;">+</span><span class="typ" style="color: #2b91af;">Kt</span><span class="pun" style="color: #303336;">+</span><span class="typ" style="color: #2b91af;">OYpd6</span><span class="pun" style="color: #303336;">++</span><span class="pln" style="color: #303336;">m7wnJRH</span><span class="pun" style="color: #303336;">/</span><span class="pln" style="color: #303336;">q0H69LIR9v3Td3udzOaxv</span><span class="pun" style="color: #303336;">/</span><span class="pln" style="color: #303336;">gzXF1BFuAS<br/>   </span><span class="typ" style="color: #2b91af;">DQs6iA</span><span class="pun" style="color: #303336;">==</span><span class="pln" style="color: #303336;"><br/>   </span><span class="pun" style="color: #303336;">=</span><span class="pln" style="color: #303336;">ckOV<br/>   </span><span class="pun" style="color: #303336;">-----</span><span class="pln" style="color: #303336;">END PGP PUBLIC KEY BLOCK</span><span class="pun" style="color: #303336;">-----</span>

Here are the properties of this key:

key_properties.png

Answers

  • karianna
    karianna Member Posts: 205 Blue Ribbon
    edited Oct 6, 2016 3:27PM

    I'd check your key against a public key checker (if you Google it, you'll find a couple).  You might have invalid hidden characters etc

  • user4399002
    user4399002 Member Posts: 2
    edited Oct 6, 2016 6:46PM

    My Keys are good as I used org.bouncycastle.openpgp* lib to implement this earlier which worked perfectly fine until I tested with 10GB file to encrypt/decrypt. As this org.bouncycastle api holds data in memory after compress/de-compress, we saw java heap space error  with that big file.Now we are trying javax.crypto.Cipher and got stuck with the issue described ( How to create the public key).

  • karianna
    karianna Member Posts: 205 Blue Ribbon
    edited Oct 6, 2016 3:52PM

    Yeah, you'll need to have a larger heap and move to G1 to support that heap size.

This discussion has been closed.