C openssl解密已签名&;加密信息
我正在尝试一个小的示例程序来解密已签名的消息,然后使用openSSL进行加密。它在命令行中运行良好。但是,在修改OpenSSL的“demos”文件夹中的代码后尝试代码时,解密失败 以下是解密代码:C openssl解密已签名&;加密信息,c,openssl,C,Openssl,我正在尝试一个小的示例程序来解密已签名的消息,然后使用openSSL进行加密。它在命令行中运行良好。但是,在修改OpenSSL的“demos”文件夹中的代码后尝试代码时,解密失败 以下是解密代码: int decrypt_smime(){ BIO *in = NULL, *out = NULL, *tbio = NULL; X509 *rcert = NULL; EVP_PKEY *rkey = NULL; //PKCS7
int decrypt_smime(){
BIO *in = NULL, *out = NULL, *tbio = NULL;
X509 *rcert = NULL;
EVP_PKEY *rkey = NULL;
//PKCS7 *cms = NULL;
CMS_ContentInfo *cms = NULL;
int ret = 1;
int flags = CMS_STREAM;
OpenSSL_add_all_algorithms();
ERR_load_crypto_strings();
printf("decrypt...\n");
/* Read in recipient certificate and private key */
tbio = BIO_new_file("signer.pem", "r");
if (!tbio)
goto err;
rcert = PEM_read_bio_X509(tbio, NULL, 0, NULL);
BIO_reset(tbio);
rkey = PEM_read_bio_PrivateKey(tbio, NULL, 0, NULL);
if (!rcert || !rkey)
goto err;
printf("decrypt...\n");
/* Open S/MIME message to decrypt */
in = BIO_new_file("smencsign.txt", "r");
if (!in)
goto err;
printf("keys read...\n");
/* Parse message */
cms = SMIME_read_CMS(in, NULL); //here is the problem I think
if (!cms)
goto err;
printf("keys read...\n");
out = BIO_new_file("decout.txt", "w");
if (!out)
goto err;
/* Decrypt S/MIME message */
if (!CMS_decrypt(cms, rkey, rcert, NULL, out, flags))
goto err;
ret = 0;
err:
if (ret)
{
fprintf(stderr, "Error Decrypting Data\n");
ERR_print_errors_fp(stderr);
}
if (cms)
//PKCS7_free(cms);
CMS_ContentInfo_free(cms);
if (rcert)
X509_free(rcert);
if (rkey)
EVP_PKEY_free(rkey);
if (in)
BIO_free(in);
if (out)
BIO_free(out);
if (tbio)
BIO_free(tbio);
return ret;
}
我得到的错误是:
验证数据时出错
*307425568:错误:0D0D40D1:asn1编码例程:SMIME\u read\u asn1:无内容类型:asn\u mime.c:451:*
The commands on openssl that worked:
openssl cms -sign -in encr.txt -signer signer.pem -text | openssl cms -encrypt -out smencsign.txt signer.pem
openssl smime -decrypt -in smencsign.txt -recip signer.pem -inkey signer.pem
显然,openssl使用“cms”实用程序进行签名和加密,但似乎使用“smime”实用程序进行解密。那么代码等价物是什么?尝试添加以下行:
OpenSSL_添加所有密码() 还是一样。还有别的想法吗?事实上,我意识到,如果我对加密内容使用解密,它就会工作。只有在签名并加密后,解密例程才会抛出错误:(我也遇到过类似的问题。我有一个经过签名和加密的CMS文件。我可以解密并验证Java的bouncy castle和.NET 2.0没有问题。但是,openssl无法处理它而不出错。@Chris这可能是文件中的某些东西,比如BOM扇区吗?