Skip to Main Content

Java Security

Announcement

For appeals, questions and feedback about Oracle Forums, please email oracle-forums-moderators_us@oracle.com. Technical questions should be asked in the appropriate category. Thank you!

Problem with private RSA key generated using KeyPairGenerator

843811Sep 8 2009 — edited Sep 12 2009
Hi All,
I am generating a private key using KeyPairGenerator as follows:
try {
      // Generate a 512-bit RSA key pair     
      KeyPairGenerator keyGen = KeyPairGenerator.getInstance("RSA");
      keyGen.initialize(512);
      KeyPair keypair = keyGen.genKeyPair();
      PrivateKey privateKey = keypair.getPrivate();
      String privateKeyBase64 = getKeyAsString(privateKey);
      System.out.println(privateKeyBase64);    
    }......
public static String getKeyAsString(Key key)
  {
    // Get the bytes of the key
    byte[] keyBytes = key.getEncoded();
    // Convert key to BASE64 encoded string
    BASE64Encoder b64 = new BASE64Encoder();
    return b64.encode(keyBytes);
  }
However, when I try and use the private key with Dkim signing I get an error saying its not valid.
This is what is produced:
-----BEGIN RSA PRIVATE KEY-----
MIIBVAIBADANBgkqhkiG9w0BAQEFAASCAT4wggE6AgEAAkEAvP1H33A5bbk2z9o4
Xb7ev+27miJHhiTanYhNKlyx4/MBvWdN+S/lgQpLmNHs/WxQj+2Qy1qHmdjy8FZR
HZ/dbQIDAQABAkB+fyGOYvVthd00PqIfzVQXLy60umEZXq2dZ9AOnMDNC38Zf0+W
0WXFZzEezZelDAZI/iSEu0jW/0eqHWP11k2ZAiEA/adbPyVXLkt/f9ubZdOk0/Qw
+fP3vFs5+cFjtSQ7F+sCIQC+vM0g2SPMMMk8zhAl67i8w01M6Af9LOb/Mq0yLD8i
BwIhAJeApleJDhFwtq6/lIm1Z/XRHI+u0IGIW5B5mfxPr5wRAiAfiu2JXx9yZycA
bTtyafcVEF86Nq0Li19wGJtXzsVqBQIgbjxabMPzDd8sPYMIwaUp9YMmmogf6w7+
EwBshBk31+M=
-----END RSA PRIVATE KEY-----
If I create the Private Key using openssl, it works. Does my Private Key generation look correct?
Does anyone know if openssl uses SHA1withRSA?
If I try and do
 KeyPairGenerator keyGen = KeyPairGenerator.getInstance("SHA1withRSA");
I get:
java.security.NoSuchAlgorithmException: SHA1withRSA KeyPairGenerator not available. How do I make this available?

Thanks, Jackie.
Comments
Locked Post
New comments cannot be posted to this locked post.
Post Details
Locked on Oct 10 2009
Added on Sep 8 2009
7 comments
2,334 views