Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/152.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ RSA_验证失败返回错误的签名错误_C++_Openssl_Rsa - Fatal编程技术网

C++ RSA_验证失败返回错误的签名错误

C++ RSA_验证失败返回错误的签名错误,c++,openssl,rsa,C++,Openssl,Rsa,签名后我无法验证邮件 我正在编写一个项目来模拟使用OpenSSL对文件中的数据进行签名和验证。 签名过程似乎很有效,但在验证过程中,RSA_Verify始终返回0。这是未经核实的。 我搞不懂。我错过了什么 #include <string> #include <fstream> #include <openssl/rsa.h> #include <openssl/pem.h> #include <openssl/applink.c> #

签名后我无法验证邮件

我正在编写一个项目来模拟使用OpenSSL对文件中的数据进行签名和验证。 签名过程似乎很有效,但在验证过程中,RSA_Verify始终返回0。这是未经核实的。 我搞不懂。我错过了什么

#include <string>
#include <fstream>
#include <openssl/rsa.h>
#include <openssl/pem.h>
#include <openssl/applink.c>
#include <openssl/err.h>

#define PRIKEY_FILENAME "private"
#define PUBKEY_FILENAME "public"

using namespace std;
typedef struct _INFO
{
    unsigned char *sig;
    unsigned int nLen;
}INFO, *pINFO;

bool ReadFileContent(string fileName,  char** out, size_t  &nLen)
{
    if (NULL == out)
        return false;

    ifstream file(fileName, ios::in | ios::binary | ios::ate);
    if (file.is_open())
    {
        nLen = (int)file.tellg();
        *out = new char[nLen];
        file.seekg(0, ios::beg);
        file.read(*out, nLen);

        file.close();
    }
    else
    {
        cout << "Unable to open file \"" << fileName << " \"\n";
        return false;
    }
    return true;
}

bool WriteFileContent(const char* data, int nLen, string fileName)
{
    if (NULL == data)
        return false;

    ofstream file(fileName, ios::out | ios::binary | ios::ate);
    if (file.is_open())
    {
        file.write(data, nLen);
        file.close();
    }
    else
    {
        cout << "Unable to open file \"" << fileName << " \"\n";
        return false;
    }
    return true;
}

bool GenerateKeyPairs()
{
    int             ret = 0;
    RSA             *r = NULL;
    BIGNUM          *bne = NULL;
    BIO             *bp_public = NULL, *bp_private = NULL;

    int             bits = 2048;
    unsigned long   e = RSA_F4;

    // 1. generate rsa key
    bne = BN_new();
    ret = BN_set_word(bne, e);
    if (ret != 1)
    {
        goto free_all;
    }

    r = RSA_new();
    ret = RSA_generate_key_ex(r, bits, bne, NULL);
    if (ret != 1)
    {
        goto free_all;
    }

    // 2. save public key
    bp_public = BIO_new_file(PUBKEY_FILENAME, "w+");
    ret = PEM_write_bio_RSAPublicKey(bp_public, r);
    if (ret != 1)
    {
        goto free_all;
    }

    // 3. save private key
    bp_private = BIO_new_file(PRIKEY_FILENAME, "w+");
    ret = PEM_write_bio_RSAPrivateKey(bp_private, r, NULL, NULL, 0, NULL, NULL);

    // 4. free
free_all:

    BIO_free_all(bp_public);
    BIO_free_all(bp_private);
    RSA_free(r);
    BN_free(bne);

    return (ret == 1);
}

bool DoSign(string priKeyFile, pINFO pInfo, string fileName)
{
    int     ret;
    unsigned char* data = NULL;
    unsigned char* encodedData = NULL;
    size_t  nFileSize = 0;

    unsigned int nEncodedDataLen = 0;
    RSA* priKey = NULL;
    FILE* fp = NULL;

    if (!ReadFileContent(fileName, (char**)&data, nFileSize))
    {
        return false;
    }

    if (data == NULL || nFileSize <= 0)
        return false;

    //SHA512(data, nFileSize, pInfo->sig);
    fp = fopen(PRIKEY_FILENAME, "r");
    priKey = PEM_read_RSAPrivateKey(fp, &priKey, NULL, NULL);

    pInfo->sig = (unsigned char*)malloc(RSA_size(priKey));
    /* Sign */
    ret = RSA_sign(NID_sha512, data, nFileSize, pInfo->sig, &pInfo->nLen, priKey);

    WriteFileContent((char*)pInfo->sig, pInfo->nLen, fileName + ".sign");

    return true;
}

bool DoVerify(string pubKeyFile, pINFO pInfo, string fileName)
{
    int ret = 0;
    unsigned char* data = NULL;
    unsigned char* encodedData = NULL;
    size_t  nFileSize = 0;
    FILE* fp = NULL;
    RSA* pubkey = NULL;

    unsigned int nEncodedDataLen = 0;

    if (!ReadFileContent(fileName, (char**)&data, nFileSize))
    {
        return false;
    }

    if (data == NULL || nFileSize <= 0)
        return false;

    fp = fopen(PUBKEY_FILENAME, "r");
    pubkey = PEM_read_RSAPublicKey(fp, NULL, NULL, NULL);
    ret = RSA_verify(NID_sha512, data, nFileSize, pInfo->sig, pInfo->nLen, pubkey);
    char buf[512];
    ERR_error_string(ERR_get_error(), buf);
    //Error here says bad signature
    return true;
}

int main()
{
    INFO info = { 0 };
    GenerateKeyPairs();
    DoSign(PRIKEY_FILENAME, &info, "Hello.txt");
    DoVerify(PUBKEY_FILENAME, &info, "Hello.txt.sign");
    return 0;
}

#包括
#包括
#包括
#包括
#包括
#包括
#定义PRIKEY_文件名“private”
#定义PUBKEY_文件名“public”
使用名称空间std;
类型定义结构信息
{
无符号字符*sig;
无符号整数nLen;
}信息,*pINFO;
bool ReadFileContent(字符串文件名、字符**输出、大小(&N)
{
if(NULL==out)
返回false;
ifstream文件(文件名,ios::in | ios::binary | ios::ate);
if(file.is_open())
{
nLen=(int)file.tellg();
*out=新字符[nLen];
seekg(0,ios::beg);
文件。读取(*输出,nLen);
file.close();
}
其他的
{
cout sig,&pInfo->nLen,priKey);
WriteFileContent((char*)pInfo->sig,pInfo->nLen,fileName+“.sign”);
返回true;
}
bool-DoVerify(字符串pubKeyFile、pINFO-pINFO、字符串文件名)
{
int-ret=0;
无符号字符*数据=NULL;
无符号字符*encodedData=NULL;
大小\u t nFileSize=0;
FILE*fp=NULL;
RSA*pubkey=NULL;
无符号int-nEncodedDataLen=0;
如果(!ReadFileContent(文件名,(字符**)和数据,n文件大小))
{
返回false;
}
如果(数据==NULL | | n文件大小sig,pInfo->nLen,pubkey);
char-buf[512];
ERR_error_字符串(ERR_get_error(),buf);
//这里的错误表示签名错误
返回true;
}
int main()
{
INFO={0};
GenerateKeyPairs();
DoSign(PRIKEY_文件名和信息,“Hello.txt”);
DoVerify(PUBKEY_文件名和信息,“Hello.txt.sign”);
返回0;
}
它应该在RSA_符号中返回1

DoVerify(PUBKEY_FILENAME, &info, "Hello.txt.sign");
这应该是:

DoVerify(PUBKEY_FILENAME, &info, "Hello.txt");

签名本身正在通过
&info
参数传递。最后一个参数应该是实际签名的东西,而不是签名。

那时候我需要第三只眼睛。你完全正确。我不知道为什么要传递签名。谢谢签名验证码需要三样东西:公钥,密码e签名,然后签名。你的
DoVerify
函数需要三个参数。如果你想,你可以很容易地修改它,以通过文件名获得签名。实际上,这是我应用程序的“beta版”。你在我的更新版本中是对的,我正在从文件中读取签名。感谢你的宝贵提示。