openssl pkeyutl-在C ++中签名VS RSA_private_encrypt

时间:2018-08-01 07:36:59

标签: c++ rsa private-key

与签名的邮件不匹配,openssl给出正确的结果:

来自

  

22 | 44

结果是

  

cODrnY / TApQwki2bZ9psFjV44YasAZ5nVfRJsUaNJHB2AiAhRSBx6S2Hc6yA5ye3eOsg4jzy6kWD6A0as / CXL + eaJRf24sg4RFesLvcYIUAKCgDKJkAlgUyS6mtO0rucr7Awe + SI9Y2LotfzRak5uv5FpOZ5QefOXf9FPqbTaPuaEDFy2DvU55M03TqqqF8Sy1BRXRmYMR6gC74dIbq5ZTZiJvdvPzA1F5qB6m9L + nfh33ohnwJjJd1et72Uq1 / JFimiynF2YmZ5RUA / aSb2yMVc67sVh5dPqMgS0oEL26CfZXMqaZC2w6S5z + 0fhX5tNzPypauMKlrAH / 60 1 / I + WG ==

但是在C ++中,结果几乎相同(更短/错误)

  

cODrnY / TApQwki2bZ9psFjV44YasAZ5nVfRJsUaNJHB2AiAhRSBx6S2Hc6yA5ye3eOsg4jzy6kWD6A0as / cxl + eaJRf24sg4RFesLvcYIUAKCg ==

为什么?我的例子:

OpenSSL:

  

openssl pkeyutl -sign -inkey privateKey.pem -in text.txt -outsigned.txt

     

openssl base64 -insigned.txt -outsigned64.txt -A

C ++:

unsigned char* base64(const unsigned char *input, int length)
{
    BIO *bmem, *b64;
    BUF_MEM *bptr;
    unsigned char* buff;

    b64 = BIO_new(BIO_f_base64());
    bmem = BIO_new(BIO_s_mem());
    b64 = BIO_push(b64, bmem);
    BIO_write(b64, input, length);
    BIO_flush(b64);
    BIO_get_mem_ptr(b64, &bptr);

    buff = (unsigned char *)malloc(bptr->length);
    memcpy(buff, bptr->data, bptr->length - 1);
    buff[bptr->length - 1] = 0;

    BIO_free_all(b64);

    return buff;
}

int main()
{
    const char* privkey = "-----BEGIN RSA PRIVATE KEY-----\n"\
                            "MIIEowIBAAKCAQEArLNydIinK9HvJmRBGmat62bY9Y7GbBAKEGAMTVHAdGFX77JZ\n"\
                            "Jbg8OJIgJ+IAt/mLSeocpMoL7adAAZc+gQ91uxd4hdDjdiO8heGJrpMbypjS6oIo\n"\
                            "lSZV3sgmNBzUHzgyJ7HNpCLaw9A7CdJBZ7wTVpE7nBrK4KALsL0q+wY/M7hH6ALZ\n"\
                            "HDLmsiitT54ElXOh//whPKPbKLt+epwTL3Kduxzc/Q/wR9wfEdnW3kGBo+/pCUkz\n"\
                            "3OxilXY9kH1ZgDlczsOLl+0aabC70izJPqOtqokFjIkGRuhnLBgWKs6MpWrbxSs2\n"\
                            "zAE9sQyHeCw6l210SK2mXP6VZYnY6jf8ZcL+swIDAQABAoIBAFAT1NpcEseqzC0N\n"\
                            "B3LqcXJZefhCf5bTyRbWsl2XrplIGbI3DEcFKkdA2lAj5xlnSEK5nxZ56RYFGxSh\n"\
                            "wIWvWXOq6mOB/DZnU57weRrNc3F5czLxr0eZQu/N9ojrZSAkunzVYPjegHmTMAzZ\n"\
                            "xSIIfx8AsJcthSyqiUEmNau3tc6QvOgyOJgG4yQKMhPk92wdgUu6KIRbIlw04fsN\n"\
                            "qeM1BC7GxrP+jIYN/qhirk+fEllYgXq6Lrj3kYvwUB79Bouwq1gDmJ21l0yTBn7e\n"\
                            "d/BeZmvgDn1ojGtde/mRHXY5Ao9/ZTnxzbb/mt/8MQDxWWKVsFL62hznIKnrAklV\n"\
                            "fAz1EeECgYEA3TYxBm29mf+nn9qvxm82jFsJuhHo7VyykLOtgC4P+uZNWZJrrkxS\n"\
                            "YLdYswYw7y5WY7WV47vquoDW7vY4YEKfXjSgG3f7AJ6dt47VBkguT7YXwDMbZ9zy\n"\
                            "AZv55gKM8Q6AI5XLTp+Qtl3A2nlYF5K6LgzOgVj1/14s2PMsZRoqiAUCgYEAx9xA\n"\
                            "i90zdjAd/2vF4QAV0Ke29WIH3wJlNTdiLrbbCtt1MQlYq3wBZb4jAAmwhIFNaHRj\n"\
                            "zvCY+I3q7Hs5b6XNBU1ZyWmFwTfL7DiEjjxlEU67HazNz09KrgVGZb3reGewr66X\n"\
                            "Rln0K0Rov5Y+1lLJBLhkEnNTct+aXH7ZIlgnwVcCgYAuuM1uACu1pQuwda3+iWrK\n"\
                            "Bkhtpm6+A4Ark+enVRNG+PGJY6WesLwNdWwkk8AuJGlWsfmwmfYf4iqDWjnaCvhC\n"\
                            "G3ddld/L57SEGoTfyMsPRvSJQcIeE8qBkIIdlJhUKa3i1jaUqpsUCYU8H7j9cZ4l\n"\
                            "L8tGApYik6+BOws7Z2VoSQKBgQCj7iG3VwGN8Ju6VLDjA8ng8p/cF8NzBSj0hG/w\n"\
                            "9yUle6pdRVhImBchV9qnfksd9F0Ez2rKY4hlXyqRO+V5kVpf/fkmfnoqaNTH9fes\n"\
                            "HxebIaPTDFlnsJcFs+fxLd1FTm+bOi50kvX2w07Tzq3N5+tgBsp2Y8fP0l5vfdAm\n"\
                            "Hn7FUwKBgGvsiOx9t/aHJ447qwkcEdfg96woOI8sFp5o30yghrgo0DySbP+GN1VY\n"\
                            "2iEt+RspJnaGjIUX6jDAACytU8s4b3tKMD/7UZaLFidK7n+AVAHI4iMpsLjbgqcE\n"\
                            "lh7knmD6Jc7HW0jIIimtSDmdtW4JvVMF60y52wT8v9d2Pc7j46Rp\n"\
                            "-----END RSA PRIVATE KEY-----\n";

    BIO *priv_bio;
    RSA *private_key;

    priv_bio = BIO_new_mem_buf(privkey, -1);
    if (priv_bio == NULL) {
        ERR_print_errors_fp(stdout);
        return 1;
    }

    private_key = PEM_read_bio_RSAPrivateKey(priv_bio, NULL, NULL, NULL);
    if (private_key == NULL) {
        ERR_print_errors_fp(stdout);
    }

    const char * message = "22|44"

    unsigned char* sigret;
    sigret = (unsigned char*)malloc(RSA_size(private_key));

    unsigned int sig_len = 0;

    RSA_private_encrypt(strlen(message), (unsigned char*)message, sigret, private_key, RSA_PKCS1_PADDING);
    cout << base64((unsigned char*)sigret, strlen((char*)sigret)) << endl;

    RSA_free(private_key);

    return 0;
}

0 个答案:

没有答案