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!

Extract Modulus and Exponent from Byte[]

843811Feb 22 2008 — edited Dec 18 2009
I am trying to use RSA encryption on J2ME. I just need the private key to sign a document.

To do it, I changed the PEMReader and PEMUtilities classes (org.bouncycastle.openssl) so they work on J2ME (implementing the classes that doesn't exist on J2ME and reducing its functionality).

But I am having a lot of troubles with javax.crypto.Cipher and org.bouncycastle.asn1.ASN1InputStream.

javax.crypto.Cipher tells me that the encryption key was too large (1024 bits). As I am working for phones, I can't use the unrestricted Java.

org.bouncycastle.asn1.ASN1InputStream shows me the Exception about not being able to create the class. Doesn't make sense, because this class is part of J2ME.

But I can't finish properly the private KeyPair readKeyPair(...) function of org.bouncycastle.openssl.PEMReader. I can extract and decrypt, with the simmetric key, the array of bytes containing the private key. But I don't know how can I extract the modulus and exponent from that array so I can use the private key:
    private KeyPair readKeyPair(
        String  type,
        String  endMarker)
        throws Exception
    {
        boolean         isEncrypted = false;
        String          line = null;
        String          dekInfo = null;
        StringBuffer    buf = new StringBuffer();

        while ((line = readLine()) != null)
        {
            if (line.startsWith("Proc-Type: 4,ENCRYPTED"))
            {
                isEncrypted = true;
            }
            else if (line.startsWith("DEK-Info:"))
            {
                dekInfo = line.substring(10);
            }
            else if (line.indexOf(endMarker) != -1)
            {
                break;
            }
            else
            {
                buf.append(line.trim());
            }
        }

        //
        // extract the key
        //
        byte[] keyBytes = Base64decode(buf.toString());

        if (isEncrypted)
        {
            if (pFinder == null)
            {
                throw new IOException("Se necesita una contrase�a que no se ha especificado");
            }

            char[] password = pFinder.getPassword();

            if (password == null)
            {
                throw new IOException("La contrase�a es nula, pero se necesita una contrase�a");
            }

            StringTokenizer tknz = new StringTokenizer(dekInfo, ",");
            String          dekAlgName = tknz.nextToken();
            byte[]          iv = Hexdecode(tknz.nextToken());

            keyBytes = PEMUtilities.crypt(false, provider, keyBytes, password, dekAlgName, iv);
        }


        KeySpec                 pubSpec;
        ByteArrayInputStream    bIn = new ByteArrayInputStream(keyBytes);
*//This fails, ASN1InputStream doesn't work!!*
        *ASN1InputStream         aIn = new ASN1InputStream(bIn);*
        ASN1Sequence            seq = (ASN1Sequence)aIn.readObject();
        PrivateKey privada;

        DERInteger              mod = (DERInteger)seq.getObjectAt(1);
        DERInteger              pubExp = (DERInteger)seq.getObjectAt(2);
        DERInteger              privExp = (DERInteger)seq.getObjectAt(3);
...
    }
It says: Exception NoClassDefFoundError:* +"org/bouncycastle/asn1/ASN1InputStream: Cannot create class in system package "+

Is there any way to do it without using ASN1InputStream? Directly from the byte array, from example...

Edited by: Delawen on Feb 22, 2008 2:11 PM
Comments
Locked Post
New comments cannot be posted to this locked post.
Post Details
Locked on Jan 15 2010
Added on Feb 22 2008
12 comments
704 views