RSA_private_encrypt总是失败 [英] RSA_private_encrypt always fails

查看:2431
本文介绍了RSA_private_encrypt总是失败的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我学习在我的程序使用OpenSSL库。在这里,在code我生成私钥,并立即我加密使用该密钥的消息。但它总是失败。请帮帮我吧。

  PRIVATE_KEY = RSA_generate_key(RSA_KEY_LENGTH,RSA_3,NULL,NULL);
如果(RSA_check_key(PRIVATE_KEY)。1){
    的printf(generate_key:密钥生成失败\\ n);
    出口(-1);
}unsigned char型味精[25];
unsigned char型密码[128];
的strcpy((字符*)味精,你好);
INT RET = RSA_private_encrypt(25,味精,密码,PRIVATE_KEY,
                              RSA_PKCS1_OAEP_PADDING);
如果(保留℃,){
    的printf(在密钥生成加密失败\\ n);
    的printf(%S \\ n,ERR_error_string(ERR_get_error(),(字符*)密码));
    出口(-1);
}

这总是失败,这是我与ERR_error_string得到错误。

 错误:04066076:LIB(4):FUNC(102):原因(118)


解决方案

我已经找到了这个问题的原因。其实填充方法RSA_PKCS1_OAEP_PADDING是不是在我的CentOS都和Ubuntu机为我工作。有一次,我把它改成RSA_PKCS1_PADDING,它开始工作的罚款。但我不知道为什么会这样。

I am learning to use OpenSSL library in my program. Here in the code I generate a private key and immediately I am encrypting a message using that key. But always it fails. Kindly help me.

private_key = RSA_generate_key(RSA_KEY_LENGTH, RSA_3, NULL, NULL);
if (RSA_check_key(private_key) < 1) {
    printf("generate_key: key generation failed\n");
    exit(-1);
}

unsigned char msg[25];
unsigned char cipher[128];
strcpy((char*)msg, "hello");
int ret = RSA_private_encrypt(25, msg, cipher, private_key,
                              RSA_PKCS1_OAEP_PADDING);
if (ret < 0) {
    printf("encryption in key generation failed\n");
    printf ("%s\n", ERR_error_string (ERR_get_error (), (char *) cipher));
    exit (-1);
}

This always fails and this is the error I am getting with ERR_error_string.

error:04066076:lib(4):func(102):reason(118)

解决方案

I have found the reason for this problem. Actually the padding method RSA_PKCS1_OAEP_PADDING is not working for me in my both centos and ubuntu machines. Once I changed it to RSA_PKCS1_PADDING, it started working fine. But I am not sure why this is happening.

这篇关于RSA_private_encrypt总是失败的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆