Topic
4 replies Latest Post - ‏2012-11-23T07:11:49Z by willey
k10k
k10k
3 Posts
ACCEPTED ANSWER

Pinned topic java.security.spec.InvalidKeySpecException: Inappropriate key specification

‏2012-11-15T14:34:02Z |
Hi
I have written a program that will generate public key, private key and modulus and exponents using RSA Algorithm. When i run this program in Sun jdk Windows XP iam able to generate the keys. But the same program when executed in IBM AIX 5300-06-03-0732 (oslevel -s) i.e 5.3 with JDK 1.6 64bit iam getting the following exception

***************************************************************************
java.security.spec.InvalidKeySpecException: Inappropriate key specification
at com.ibm.crypto.provider.RSAKeyFactory.engineGetKeySpec(Unknown Source)
at java.security.KeyFactory.getKeySpec(Unknown Source)
at SecurityRSATest.main(SecurityRSATest.java:39)
****************************************************************************

$ java -version
java version "1.6.0"
Java(TM) SE Runtime Environment (build pap6460sr2-20080818_01(SR2))
IBM J9 VM (build 2.4, J2RE 1.6.0 IBM J9 2.4 AIX ppc64-64 jvmap6460-20080816_2209
3 (JIT enabled, AOT enabled)
J9VM - 20080816_022093_BHdSMr
JIT - r9_20080721_1330ifx2
GC - 20080724_AA)
JCL - 20080808_02
$
The program is

import java.security.KeyFactory;
import java.security.KeyPair;
import java.security.KeyPairGenerator;
import java.security.NoSuchAlgorithmException;
import java.security.PrivateKey;
import java.security.PublicKey;
import java.security.interfaces.RSAPrivateKey;
import java.security.interfaces.RSAPublicKey;
import java.security.spec.RSAPrivateKeySpec;
import java.security.spec.RSAPublicKeySpec;

public class SecurityRSATest {

/**
* @param args
*/
public static void main(String[] args) {
PrivateKey privateKey = null;
PublicKey publicKey = null;
KeyPairGenerator keyGen = null;
KeyFactory fact = null;
RSAPublicKeySpec pub = null;
RSAPrivateKeySpec priv = null;

String publicKeyModulus = null;
String publicKeyExponent = null;
String privateKeyModulus = null;
String privateKeyExponent = null;
KeyPair keypair = null;
try {
keyGen = KeyPairGenerator.getInstance("RSA");
keyGen.initialize(1024);
keypair = keyGen.genKeyPair();
privateKey = (RSAPrivateKey) keypair.getPrivate();
publicKey = (RSAPublicKey) keypair.getPublic();

fact = KeyFactory.getInstance("RSA");
pub = (RSAPublicKeySpec) fact.getKeySpec(publicKey, RSAPublicKeySpec.class);
priv = (RSAPrivateKeySpec) fact.getKeySpec(privateKey, RSAPrivateKeySpec.class);
publicKeyModulus = pub.getModulus().toString();
publicKeyExponent = pub.getPublicExponent().toString();

privateKeyModulus = priv.getModulus().toString();
privateKeyExponent = priv.getPrivateExponent().toString();
System.out.println("output");
System.out.println("publicKeyModulus=");
System.out.println("publicKeyExponent=");
System.out.println("privateKeyModulus=");
System.out.println("privateKeyExponent=");

} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}

}
what is the mistake in this program because it does not run in IBM JDK. Guide me in solving this problem
Updated on 2012-11-23T07:11:49Z at 2012-11-23T07:11:49Z by willey
  • JPdev
    JPdev
    18 Posts
    ACCEPTED ANSWER

    Re: java.security.spec.InvalidKeySpecException: Inappropriate key specification

    ‏2012-11-17T09:50:51Z  in response to k10k
    Hello,
    This is always the same problem, the IBM java SDK is not exactly the same as Oracle/Sun JAVA.
    I don't know and use IBM JAVA, but only The Sun version. To solve your problem you must read
    the documentation API IBM and specially com.ibm.crypto.provider.RSAKeyFactory class.
    Also java.security.KeyFactory class must be investigate.
    Be warning that there may be some changes in your code (functions, imports,..).

    Hope this help.
    • k10k
      k10k
      3 Posts
      ACCEPTED ANSWER

      Re: java.security.spec.InvalidKeySpecException: Inappropriate key specification

      ‏2012-11-19T05:09:14Z  in response to JPdev
      Hello,

      Can I install sun jdk in IBM AIX machine, is it compactable or not ? So this issue will get resolved , rite ?
      • JPdev
        JPdev
        18 Posts
        ACCEPTED ANSWER

        Re: java.security.spec.InvalidKeySpecException: Inappropriate key specification

        ‏2012-11-19T10:39:52Z  in response to k10k
        Hello,
        Sorry you can't do that, there is no JDK Oracle/Sun for IBM AIX.
        Perhaps you can install a Virtual machine on your server with an Oracle Linux and then get
        a Sun JDK, this work also with a Window's virtual.
      • willey
        willey
        4 Posts
        ACCEPTED ANSWER

        Re: java.security.spec.InvalidKeySpecException: Inappropriate key specification

        ‏2012-11-23T07:11:49Z  in response to k10k
        Yes you can install that way provided you have interconnection software available , research for that.

        Free Antivirus