×

Discussion Board

Results 1 to 6 of 6
  1. #1
    Registered User
    Join Date
    Jun 2009
    Posts
    2

    How to encode/decode ECC keys in BouncyCastle j2me to/from byte array

    Hi,

    I was trying to encode/decode ECC public and private keys into byte arrays. It is straight forward if I have java.security.PublicKey object, which has a getEncoded() method. And I can use org.bouncycastle.jce.provider.asymmetric.ec.KeyFactory.EC.createPublicKeyFromDERStream(byteKey) to recreate a PublicKey object.

    However, I don't have java.security and org.bouncycastle.jce in j2me, and I have to stick to org.bouncycastle.crypto. I could only produce AsymmetricCipherKeyPair pbject, which is fine to do encryption/decryption. Anyone knows how to do the encode/decode on this object?

    Thanks.

  2. #2
    Registered User
    Join Date
    Nov 2009
    Posts
    10

    Re: How to encode/decode ECC keys in BouncyCastle j2me to/from byte array

    In fact I encounter the same problem now same like you. I suppose I can only use j2me version of bouncy castle (which contains jce.provider package) for mobile development but not jdk version.

    I have a keystore of type bks generated by keytool IUI. I want to retrieve the private and public keys and convert them them to ECPrivateKeyParameters and ECPublicKeyParameters objects. the following code has two problem:


    Code:
      
      Security.addProvider(new org.bouncycastle.jce.provider.BouncyCastleProvider());
      KeyStore ks = KeyStore.getInstance("BKS", "BC");
      ks.load(new FileInputStream(args[0]), args[1].toCharArray());
      Key key = ks.getKey(alias, password);
      if (key instanceof PrivateKey) {
                    // Get certificate of public key
                    java.security.cert.Certificate cert = keystore.getCertificate(alias);
        
                    // Get public key
                    PublicKey publicKey = cert.getPublicKey();
        
                    // Return a key pair
                    return new KeyPair(publicKey, (PrivateKey)key);
    }
    first I don't have org.bouncycastle.jce under j2me version so I can't get bouncy castle provider.

    second how can' I convert PublicKey and PrivateKey into ECPublicKeyParameters and ECPrivateKeyParameters so I can use them for EC encryption/decryption?


    please help?
    Last edited by polaris777; 2009-12-04 at 04:18.

  3. #3
    Registered User
    Join Date
    Apr 2006
    Posts
    1

    Re: How to encode/decode ECC keys in BouncyCastle j2me to/from byte array

    hi, were any of you able to encode the key for storage/transfer yet?

  4. #4
    Registered User
    Join Date
    Jun 2009
    Posts
    2

    Re: How to encode/decode ECC keys in BouncyCastle j2me to/from byte array

    Quote Originally Posted by speigel View Post
    hi, were any of you able to encode the key for storage/transfer yet?
    Yes. This is how I did it:

    public static byte[] encodeECPrivateKeyParameters(AsymmetricKeyParameter akp) {
    if (!(akp instanceof ECPrivateKeyParameters))
    return null;
    X962Parameters params;

    ECPrivateKeyParameters ecPriKey = (ECPrivateKeyParameters) akp;
    ECDomainParameters ecp = ecPriKey.getParameters();
    ECCurve curve = ecp.getCurve();

    X9ECParameters ecP = new X9ECParameters(curve, ecp.getG(), ecp.getN(), ecp.getH(), ecp.getSeed());

    params = new X962Parameters(ecP);

    PrivateKeyInfo info = new PrivateKeyInfo(new AlgorithmIdentifier(X9ObjectIdentifiers.id_ecPublicKey, params.getDERObject()), new ECPrivateKeyStructure(
    ecPriKey.getD()).getDERObject());

    return info.getDEREncoded();
    }

    public static byte[] encodeECPublicKeyParameters(AsymmetricKeyParameter akp) {
    if (!(akp instanceof ECPublicKeyParameters))
    return null;
    ASN1Encodable params;
    SubjectPublicKeyInfo info;
    boolean withCompression = false;

    ECPublicKeyParameters ecPubKey = (ECPublicKeyParameters) akp;

    // DERObjectIdentifier curveOid = getNamedCurveOid(((ECNamedCurveSpec)ecSpec).getName());
    DERObjectIdentifier curveOid = getNamedCurveOid(ECC_ALG_NAME[eccStrengthId]);
    params = new X962Parameters(curveOid);

    ECDomainParameters ecp = ecPubKey.getParameters();
    ECCurve curve = ecp.getCurve();

    ASN1OctetString p = (ASN1OctetString) new X9ECPoint(curve.createPoint(ecPubKey.getQ().getX().toBigInteger(), ecPubKey.getQ().getY().toBigInteger(),
    withCompression)).getDERObject();

    info = new SubjectPublicKeyInfo(new AlgorithmIdentifier(X9ObjectIdentifiers.id_ecPublicKey, params.getDERObject()), p.getOctets());

    return info.getDEREncoded();
    }

    public static ECPublicKeyParameters decodeECPublicKeyParameters(byte[] pkByte) throws IOException {
    return (ECPublicKeyParameters) PublicKeyFactory.createKey(pkByte);
    }

    public static ECPrivateKeyParameters decodeECPrivateKeyParameters(byte[] pkByte) throws IOException {
    return (ECPrivateKeyParameters) PrivateKeyFactory.createKey(pkByte);
    }

  5. #5
    Registered User
    Join Date
    Nov 2011
    Posts
    1

    Re: How to encode/decode ECC keys in BouncyCastle j2me to/from byte array

    Hello all,

    Please please please help me with this. I've tried the suggested code and I'm having trouble with the line:

    DERObjectIdentifier curveOid = getNamedCurveOid(ECC_ALG_NAME[eccStrengthId]);

    I don't know what ECC_ALG_NAME is and can't find it anywhere. I've tried to replace that line of code with the following:

    DERObjectIdentifier curveOid = ECUtil.getNamedCurveOid("ECDH");

    This allows me to encode the public key but when i try to decode it I get an error saying that the "algorithm identifier in key not recognized". Can anyone help!!?? Thanks!

  6. #6
    Nokia Developer Expert
    Join Date
    Aug 2007
    Posts
    1,595

    Re: How to encode/decode ECC keys in BouncyCastle j2me to/from byte array

    Hello hobeau,

    and welcome to Nokia Developer Discussion Boards!

    Just to double check and in case the other contributors cannot provide an exact answer to your question here, have you checked whether the Bouncy Castle resources provide any information regarding the issue?:

    http://www.bouncycastle.org/wiki/dis...sked+Questions

    Also, you could try to contact the Bouncy Castle creators as suggested here: http://www.bouncycastle.org/java.html

    Regards,
    r2j7
    [URL="http://library.forum.nokia.com/java"][B] >>> Java Developer's Library <<<[/B][/URL]
    [URL="https://www.developer.nokia.com/Resources/Support/Technical_support.xhtml"] [B]>>> Technical Support for Java ME development <<<[/B][/URL]
    [URL="https://publish.ovi.com/info/"][B]>>> Nokia Publish: reach millions of Nokia users worldwide through Nokia Store <<<[/B][/URL]

Similar Threads

  1. how to convert an image to byte array in j2me
    By vineetbillorey in forum Mobile Java General
    Replies: 17
    Last Post: 2009-02-25, 11:48
  2. saving unsigned bytes in byte array
    By nicenouman in forum Mobile Java General
    Replies: 3
    Last Post: 2006-10-05, 10:26
  3. Convert wav to byte array?
    By SpaceCowboy in forum Mobile Java Media (Graphics & Sounds)
    Replies: 5
    Last Post: 2006-09-04, 09:25
  4. How do I know what values to give a byte array for a tone?
    By dwneonstar in forum Mobile Java General
    Replies: 1
    Last Post: 2002-12-18, 07:20
  5. Loading images from byte array on 6130i
    By enlightment in forum Mobile Java General
    Replies: 2
    Last Post: 2002-06-05, 19:44

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
×