Problem transmiting a RSA public key, javaME , bouncy castle

后端 未结 1 425
旧巷少年郎
旧巷少年郎 2020-12-18 13:43

I\'m working on the porting of an instance messaging application from Java to JavaME ,that also implements cryptography. The problem is that I want to send my public key to

相关标签:
1条回答
  • 2020-12-18 14:15

    DER-encoding is just a standard for encoding. Saying that a key is DER-encoded is equivalent to saying it is XML-encoded: you need to agree on how it is DER-/XML-encoded to be able to decode it.

    In this case your RSAPublicKeyStructure.getEncoded() returns the key as the DER-encoding of an ASN.1 RSAPublicKey:

    RSAPublicKey ::= SEQUENCE {
      modulus INTEGER, -- n
      publicExponent INTEGER -- e 
    }
    

    The X509EncodedKeySpec on the other hand expects to be handed the DER-encoding of an ASN.1 PublicKeyInfo:

    PublicKeyInfo ::= SEQUENCE {
      algorithm AlgorithmIdentifier,
      PublicKey BIT STRING
    }
    

    To create a PublicKeyInfo using BouncyCastle do this (courtesy of GregS):

    RSAPublicKeyStructure rsaPublicKey = /* ... */
    AlgorithmIdentifier rsaEncryption = new AlgorithmIdentifier(PKCSObjectIdentifiers.rsaEncryption, DERNull.INSTANCE); 
    SubjectPublicKeyInfo publicKeyInfo = new SubjectPublicKeyInfo(rsaEncryption, rsaPublicKey);
    byte[] encodedPublicKeyInfo = publicKeyInfo.getEncoded();
    
    0 讨论(0)
提交回复
热议问题