简体   繁体   中英

C++/Openssl Get RSA key from encoded bytes (encoded by java)

Does somebody know how I can create an RSA key in C++ from an encoded byte array?

My problem is that I try to develop a C++ client that is interacting with a server which is coded in Java. Well in Java the client receives the rsa key encoded as an byte array, decodes it to a RSA RSAPublicKey and encrypts a message with this key.

The java server/client code:

public static PublicKey decodePublicKey(byte[] p_75896_0_)
{
    try
    {
        X509EncodedKeySpec var1 = new X509EncodedKeySpec(p_75896_0_);
        KeyFactory var2 = KeyFactory.getInstance("RSA");
        return var2.generatePublic(var1);
    }
    catch (NoSuchAlgorithmException var3)
    {
        ;
    }
    catch (InvalidKeySpecException var4)
    {
        ;
    }

    field_180198_a.error("Public key reconstitute failed!");
    return null;
}

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

this.publicKey = CryptManager.decodePublicKey(data.readByteArray());

After that the client is doing some encrypting stuff with his key.

The key gets sent like this:

public static final KeyPair keys;
static
{
    try
    {
        KeyPairGenerator generator = KeyPairGenerator.getInstance( "RSA" );
        generator.initialize( 1024 );
        keys = generator.generateKeyPair();
    } catch ( NoSuchAlgorithmException ex )
    {
        throw new ExceptionInInitializerError( ex );
    }
}

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
byte[] pubKey = keys.getPublic().getEncoded();
writeBytes(pubKey);

My problem is how to get the key from the byte array in C++.

Update: Im currently working on this code:

    char* publicKey = ...
    int publicKeyLength = 162;
    EVP_PKEY* key = EVP_PKEY_new();

    if(d2i_PUBKEY(&key, (const unsigned char**) &publicKey, publicKeyLength) != 0){
        logError("Problem!");
    }
    logMessage("Key: "+to_string((uint64_t) (void*) key));

Well my problem now is that i have an SIGSEGV error on the third line and dont know what this course. Well the key should be valid.

What Java returns for the public key is a SubjectPublicKeyInfo structure, which doesn't just contain the (PKCS#1 encoded) values for the public key, but also the key identifier etc.

So to decode this you have to type "decode SubjectPublicKeyInfo openssl" in your favorite search engine. Then you'll find (after some scrolling) the following information from here :

 d2i_PUBKEY() and i2d_PUBKEY() decode and encode an EVP_PKEY structure using SubjectPublicKeyInfo format. They otherwise follow the conventions of other ASN.1 functions such as d2i_X509(). 

Obviously you'd need the decoding algorithm.


Note that openssl is C so beware of buffer overruns when decoding stuff. I'd rather have a 1024 bit RSA key that is used with secure software than a 2048 bit key with software full of buffer overruns.

Needless to say you need to trust the public key before importing it. There is a reason why it is called the public key infrastructure (PKI).

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM