Note that there are some explanatory texts on larger screens.

plurals
  1. POException when initializing PublicKey from RSA public key stored as a string (works on laptop, not on server)
    primarykey
    data
    text
    <p>I have a application I am working on that deals with handling certificates and I am running into a issue where my code works correct when I run in via JUnit tests locally on my laptop but when I actually deploy the code to my server I am getting a exception. The exception is happening while creating a PublicKey from a RSA public key stored in a String. I am hoping someone might be able to point me in a direction to look why it fails on the server but works in the Junit.</p> <p>My RSA Public Key:</p> <pre class="lang-none prettyprint-override"><code>-----BEGIN PUBLIC KEY----- MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEAvNEz3+TStAAndHTc1qwT NGvZYyB7DD1FshQf+mbQUGJ9HccOXNn5oHB7fWQjODjlDrYyCs7FclSMTLxA3lHX 98QWeWHL2O8t8qrJQQEUWZITmr/ddiNJOOvMeYF0K5if4m84vjgx/pTwwAVyU0Yo MMXPnRozO8o7zSyRsH4jixALDugrsveEjLQI/cIEFvNjqlhyfumHyJKywNkMH1oJ 4e/f89FkpeDV694lsLs1jguuLLnvroXYJ5Uzeos+F0Pj1zFDUvhWrjVwxsUfAxS8 5uFGTUm6EEl9XiKwi+mgg8ODrY5dh3uE2yKB2T1Qj8BfK55zB8cYbORSsm6/f6Bi BwIDAQAB -----END PUBLIC KEY----- </code></pre> <p>My Junit Code: &lt;- Works from laptop</p> <pre><code>@Test public void testkey() throws Exception { String pem = "-----BEGIN PUBLIC KEY-----"+ "MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEAvNEz3+TStAAndHTc1qwT"+ "NGvZYyB7DD1FshQf+mbQUGJ9HccOXNn5oHB7fWQjODjlDrYyCs7FclSMTLxA3lHX"+ "98QWeWHL2O8t8qrJQQEUWZITmr/ddiNJOOvMeYF0K5if4m84vjgx/pTwwAVyU0Yo"+ "MMXPnRozO8o7zSyRsH4jixALDugrsveEjLQI/cIEFvNjqlhyfumHyJKywNkMH1oJ"+ "4e/f89FkpeDV694lsLs1jguuLLnvroXYJ5Uzeos+F0Pj1zFDUvhWrjVwxsUfAxS8"+ "5uFGTUm6EEl9XiKwi+mgg8ODrY5dh3uE2yKB2T1Qj8BfK55zB8cYbORSsm6/f6Bi"+ "BwIDAQAB"+ "-----END PUBLIC KEY-----"; PublicKey willNotWork = decodeKey(pem); } </code></pre> <p>My Actual Code: &lt;- Throws exception when run on server</p> <pre><code>@Override public PublicKey getKey() { PublicKey pk = null; try { String crt = "-----BEGIN PUBLIC KEY-----"+ "MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEAvNEz3+TStAAndHTc1qwT"+ "NGvZYyB7DD1FshQf+mbQUGJ9HccOXNn5oHB7fWQjODjlDrYyCs7FclSMTLxA3lHX"+ "98QWeWHL2O8t8qrJQQEUWZITmr/ddiNJOOvMeYF0K5if4m84vjgx/pTwwAVyU0Yo"+ "MMXPnRozO8o7zSyRsH4jixALDugrsveEjLQI/cIEFvNjqlhyfumHyJKywNkMH1oJ"+ "4e/f89FkpeDV694lsLs1jguuLLnvroXYJ5Uzeos+F0Pj1zFDUvhWrjVwxsUfAxS8"+ "5uFGTUm6EEl9XiKwi+mgg8ODrY5dh3uE2yKB2T1Qj8BfK55zB8cYbORSsm6/f6Bi"+ "BwIDAQAB"+ "-----END PUBLIC KEY-----"; System.out.append("crt == " + crt); pk = decodeKey(crt); } catch(Exception ex){ System.out.println("getKey caught: " + ex.getMessage()); } return pk; } </code></pre> <p>decodeKey code:</p> <pre><code>public PublicKey decodeKey(String text) throws CryptographyException { String content = text; if( text.contains("-----BEGIN PUBLIC KEY-----") ) { content = text.replace("-----BEGIN PUBLIC KEY-----", "").replace("-----END PUBLIC KEY-----", ""); } if( text.contains("-----BEGIN RSA PUBLIC KEY-----") ) { content = text.replace("-----BEGIN RSA PUBLIC KEY-----", "").replace("-----END RSA PUBLIC KEY-----", ""); } byte[] der = Base64.decodeBase64(content); return decodeDerKey(der); } public PublicKey decodeDerKey(byte[] publicKeyBytes) throws CryptographyException { try { KeyFactory factory = KeyFactory.getInstance("RSA"); PublicKey publicKey = factory.generatePublic( new X509EncodedKeySpec(publicKeyBytes)); return publicKey; } catch(Exception e) { throw new CryptographyException(e); } } </code></pre> <p>Here's the exception that is thrown when I run my code on the server:</p> <pre class="lang-none prettyprint-override"><code>Could not parse certificate: java.io.IOException: DerInputStream.getLength(): lengthTag=111, too big` </code></pre> <p>So I am guessing is has to be some sort of difference between my laptop and the server. I have verified that both are running basically the same version of Java ("1.7.0_17"). I also checked the provider of the <code>KeyFactory</code> and they are the same as well. So where do I look from here? </p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload