-1

on java side:

  • use bouncycastle to get the key pair, privateKey and publicKey

  • encrypt orig message1 with privateKey to get a encrypted message2

  • decrypt encrypted message2 with publicKey is ok, success to get the same orig message1

on c++ side:

  • based on openssl, "RSA_public_decrypt" and "RSA_private_encrypt" API

  • use the same publicKey(which generated on java side) to decryt the message2, return a buffer with every byte filled 0, and the RSA_public_decrypt return success.

in addition:

  • on c++ side, if use the privateKey to encrypt the orig message1 to get a encrypted message3, and then decrypt it with public key, success to get message1. but the message3 is not the same to the encrypted message2(java side).

  • all the above used RSA_NO_PADDING

  • on java side, encryted more times, get the same message2

  • on c++ side, encrypted more times, get the same message3 too. but message2 not same to message3.

the question is how to decrypt on c++ side to get the orig message1 wich encrypted on java side?

thanks!

tim
  • 21
  • 4
  • Java uses by default RSA/ECB/PKCS1Padding ... see post : RSA/ECB/PKCS1Padding – Exception_al Jan 20 '17 at 09:23
  • Sorry, the post is : http://stackoverflow.com/questions/21066902/default-rsa-padding-in-sun-jce-oracle-jce ... So when Decrypting in c++, use this padding scheme – Exception_al Jan 20 '17 at 09:30
  • Surely you don't expect us to guess what's wrong with your code. Please post your code. Also see [How to create a Minimal, Complete, and Verifiable example](https://stackoverflow.com/help/mcve). – jww Jan 20 '17 at 12:20

1 Answers1

0
it works ok, thanks for all the reply!
here is the c code(use public key to decrypt with openssl api):

#include <stdio.h>
#include <openssl/pem.h>
#include <openssl/rsa.h>

RSA* createRSA(unsigned char * key) {
    RSA *rsa = NULL;
    BIO *keybio = NULL;
    keybio = BIO_new_mem_buf(key, -1);
    if (keybio == NULL) {
        return 0;
    }
    return PEM_read_bio_RSA_PUBKEY(keybio, &rsa, NULL, NULL);
}

int main() {
    char encrypted_data[] = {0x62,0xe2,0xe6,0xfd,0xca,0x69,0x39,0x2f,0x0f,0x07,0x3c,0x27,0xd7,0x49,0x2c,0xd6,0x6e,0xec,0xa0,0xdd,0x7c,0xa9,0xce,0x0a,0xad,0x4a,0x68,0xa2,0x2c,0x99,0xec,0xe9,0xa0,0x3c,0x72,0x66,0xf9,0xb1,0x59,0x11,0x7e,0x64,0x87,0x22,0xa7,0x4a,0x66,0xe2,0x8b,0x51,0xa5,0x6a,0x93,0x92,0x3f,0x57,0xae,0xea,0xfa,0xe7,0x6b,0x1b,0xae,0x8f};
    char publicKey[]="-----BEGIN PUBLIC KEY-----\n"\
                     "MFwwDQYJKoZIhvcNAQEBBQADSwAwSAJBAJVdq5JlvtxJT4CqwEceW4M4AKFbDmJE\n"\
                     "H2K0a4aXmeHqedlsQgRePVCDgiiCC7kr1DEkP3+9uOUHDUtvIIoE4VsCAwEAAQ==\n"\
                     "-----END PUBLIC KEY-----\n";
    unsigned char decrypted[1024]= {0};
    int decrypted_length = RSA_public_decrypt(sizeof(encrypted_data), encrypted_data, decrypted, createRSA(publicKey), RSA_NO_PADDING);
    if(decrypted_length == -1) {
        return -1;
    }
    printf("decrypted by openssl:\n");
    for(int i=0; i<decrypted_length; i++) {
        printf("%02x ",(unsigned char)decrypted[i]);
    }
    printf("\n");
}
tim
  • 21
  • 4