简体   繁体   English

在java上加密的RSA,无法通过openssl api在c ++上解密

[英]RSA encrypted on java, can not decrypt on c++ by openssl api

on java side:在java端:

  • use bouncycastle to get the key pair, privateKey and publicKey使用 bouncycastle 获取密钥对,privateKey 和 publicKey

  • encrypt orig message1 with privateKey to get a encrypted message2使用私钥加密原始消息1以获取加密消息2

  • decrypt encrypted message2 with publicKey is ok, success to get the same orig message1用 publicKey 解密加密的 message2 没问题,成功获得相同的 orig message1

on c++ side:在 C++ 方面:

  • based on openssl, "RSA_public_decrypt" and "RSA_private_encrypt" API基于 openssl、“RSA_public_decrypt”和“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.使用相同的 publicKey(在 java 端生成)解密 message2,返回一个每个字节填充为 0 的缓冲区,并且 RSA_public_decrypt 返回成功。

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.在c++端,如果使用privateKey加密原始message1得到加密的message3,然后用公钥解密,成功得到message1。 but the message3 is not the same to the encrypted message2(java side).但 message3 与加密的 message2(java 端)不同。

  • all the above used RSA_NO_PADDING以上所有使用 RSA_NO_PADDING

  • on java side, encryted more times, get the same message2在java端,加密更多次,得到相同的消息2

  • on c++ side, encrypted more times, get the same message3 too.在 C++ 方面,加密更多次,也得到相同的消息3。 but message2 not same to message3.但 message2 与 message3 不同。

the question is how to decrypt on c++ side to get the orig message1 wich encrypted on java side?问题是如何在 C++ 端解密以获得在 Java 端加密的原始消息 1?

thanks!谢谢!

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");
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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