簡體   English   中英

如何制作RSA加密C ++和解密C#

[英]How to make a rsa encryption C++ and decryption C#

我有一個問題-與客戶端,用C#編寫的服務器以及C ++上的客戶端(Windows MacOs Linux)進行加密通信。

我為Rijndael生成了一個密鑰,RSA對其進行了加密並發送服務器,該服務器使用Rijndael來加密消息。

我的問題是我無法加密RSA,我有一個帶有公鑰的xml文件

<RSAKeyValue><Modulus>pmmv...</Modulus><Exponent>AQAB</Exponent></RSAKeyValue>

我如何使用C ++? 可能具有適用於所有平台的庫或更好的算法,可將其包含在草案中

我在Google中找到了一種算法,但它需要BigInt的公鑰格式。 如何擺脫XML?

通過解析XML,base 64對文本中的文本進行解碼,並使用結果字節數組創建一個無符號的大整數,一個用於模數,一個用於公共指數(盡管后者可能始終為0x10001或65537-第四個數字) Fermat F4-當前的密鑰對生成器現在是這樣)。

如果只需要執行一次,則還可以基於64位解碼字符串並將結果手動復制到C ++代碼中。

表示格式為RFC 3275, XML簽名語法和處理

您將需要解析RFC 3275 RSAKeyValue,然后將其放入OpenSSL RSA結構中。

它有點痛苦...。確定要看嗎? (XML解析作為練習留給讀者,但它使用您上面提供的值)。

#include <stdio.h>
#include <string.h>

#include <openssl/bn.h>
#include <openssl/rsa.h>
#include <openssl/bio.h>
#include <openssl/evp.h>

int main(int argc, char* argv[])
{
    int ret = -1;

    char nz[] = "sqprMX0n4y1gmmgpTt6pHb870k5U0MIuXixidD+S8foQf5Bb"
             "FS44kth2uWDKzXOXqiONxIPHPb+84XdxrRi2O7bvLysztgrF"
             "eU8oNDMeuIwJOKVQzKoJ1vGqjBKiA9w48oQKxvO+Ck3GmObW"
             "67LFNcrt50sEco2/OMmrpiH3W8hRx55TcR1flCJduU0/6jA7"
             "Yct9ZfhOw5wBq6o5IwiT8Mi1R6LVq9sTzSNAWHC/bFcEONkt"
             "z6NgUKbFKtt+mTfFGToiwPB1L4TecGyTIweH84nl8jVAngcM"
             "vvFP415Eg1kd9PJbRqrIESM5AU1YcsapWV3bsqEGVS2y+r5N"
             "4yzXPCYRCRyFWJSnNVlax+gtDFTNz3m9UT8m2E7elGe5hPhR"
             "6nN3votzBNvTeQ4Lwc5JDIvnWUg7aOdVIXnHQbBqEQke79BX"
             "xIv8tzVPczGkFqFExkmPPQQv8zJvBKkIYc+BFJtkylBiZfQX"
             "0590NS3L1y31VSeXn8Ncx2/ceJfUXsMWJ3sQ+dk51MKBJ2LL"
             "oyJq8IgloBLnXWvlYZ+tkzRVTExFR277V3Jr17DeTOMQGEg5"
             "HqRkbDDVGPTl2RvC2S2BTe7+r9xNzyAZMieVjZLZgb6icE6u"
             "SJFcu4qqJ1khQUjW7taymqW8Ao3oEiCUJKvRpZcJPMN+JtMn"
             "ji+2we17ytk=";

    char ez[] = "AQAB";

    BIO* nn = NULL, *ee = NULL;
    BIO* b1 = NULL, *b2 = NULL;
    RSA* rsa = NULL;

    nn = BIO_new_mem_buf(nz, strlen(nz));
    if(!nn) { ret = 1; goto done; }

    ee = BIO_new_mem_buf(ez, strlen(ez));
    if(!ee) { ret = 2; goto done; }

    b1 = BIO_new(BIO_f_base64());
    if(!b1) { ret = 3; goto done; }

    b2 = BIO_new(BIO_f_base64());
    if(!b2) { ret = 4; goto done; }

    /* If you leave these out even though you   */
    /* are reading, then BIO_read will return 0 */
    /* and BIO_should_retry will return false   */
    BIO_set_flags(b1, BIO_FLAGS_BASE64_NO_NL);
    BIO_set_flags(b2, BIO_FLAGS_BASE64_NO_NL);

    nn = BIO_push(b1, nn);
    if(!nn) { ret = 5; goto done; }

    ee = BIO_push(b2, ee);
    if(!ee) { ret = 6; goto done; }

    rsa = RSA_new();
    if(rsa == NULL) { ret = 7; goto done; }

    unsigned char buff[4096];
    const int bsize = sizeof(buff);
    int rr = 0, rd = 0;

    /* See http://marc.info/?l=openssl-users&m=123171064303018&w=2 */
    /* for this contorted goodness */
    rd = 0;
    do {
        rr = BIO_read(nn, buff + rd, bsize - rd);
        if(rr < 0) { ret = 8; goto done; } /* failed */

        rd += rr;
    } while (rr > 0 || BIO_should_retry(nn));

    if(rd == 0) { ret = 9; goto done; }

    rsa->n = BN_bin2bn(buff, rd, NULL);
    if(rsa->n == NULL) { ret = 10; goto done; }

    rd = 0;
    do {
        rr = BIO_read(ee, buff + rd, bsize - rd);
        if(rr < 0) { ret = 11; goto done; } /* failed */

        rd += rr;
    } while (rr > 0 || BIO_should_retry(ee));

    if(rd == 0) { ret = 12; goto done; }

    rsa->e = BN_bin2bn(buff, rd, NULL);
    if(rsa->e == NULL) { ret = 13; goto done; }

    /***** Paydirt *****/

    RSA_print_fp(stdout, rsa, 0);

    ret = 0;

done:

    if(ret != 0)
        fprintf(stderr, "Failed to parse and validate RSA key\n");

    if(rsa)
       RSA_free(rsa), rsa = NULL;

    if(nn)
        BIO_free_all(nn), nn = NULL;

    if(ee)
        BIO_free_all(ee), ee = NULL;

    return ret;
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM