- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我们正在尝试使用 CNG 生成 RSA SHA512 签名,我们编写了代码并生成了与 OpenSSL 不匹配的哈希值。
用于签署我们使用 OpenSSL 命令以 DER 格式生成的私钥,如下所示。
openssl genpkey -out privkey.pem -algorithm rsa
openssl pkcs8 -topk8 -inform PEM -outform DER -in privkey.pem -out privkey.der -nocrypt
openssl rsa -in privkey.pem -pubout -outform DER -out pubkey.der
使用以下代码生成带有 Windows CNG 的签名:
#include<stdio.h>
#include<windows.h>
#include<wincrypt.h>
#pragma comment (lib, "crypt32")
#pragma comment(lib, "ncrypt.lib")
#include<ntstatus.h>
void
isi_hexdump(const char *msg, const unsigned char *in, unsigned int len)
{
printf("%s [%d][", msg, len);
for (; len > 0; len--, in++) {
printf("%02X", *in);
}
printf("]\n");
}
static int
readBinFile(unsigned char **buffer, char *file, long *size)
{
FILE *fp;
size_t foo;
printf("\n inside readBinFile");
fp = fopen(file,"rb");
if (fp == NULL) {
printf("Error: reading the file %s\n", file);
return -1;
}
fseek(fp, 0, SEEK_END);
*size = ftell(fp);
fseek(fp, 0, SEEK_SET);
*buffer = malloc(*size);
if ((foo = fread(*buffer, 1, *size, fp)) != *size)
{
printf("Error: reading the file to buffer %s:%ld\n", file, *size);
return -1;
}
printf("\n exit from readBinFile <%s>",*buffer);
isi_hexdump("Pavan char at :",*buffer,*size);
return 0;
}
NTSTATUS
ComputeHash(
_In_reads_bytes_(DataLength)
PBYTE Data,
_In_ DWORD DataLength,
_Outptr_result_bytebuffer_maybenull_(*DataDigestLengthPointer)
PBYTE *DataDigestPointer,
_Out_ DWORD *DataDigestLengthPointer
)
{
NTSTATUS Status;
BCRYPT_ALG_HANDLE HashAlgHandle = NULL;
BCRYPT_HASH_HANDLE HashHandle = NULL;
PBYTE HashDigest = NULL;
DWORD HashDigestLength = 0;
DWORD ResultLength = 0;
*DataDigestPointer = NULL;
*DataDigestLengthPointer = 0;
//
// Open a Hash algorithm handle
//
Status = BCryptOpenAlgorithmProvider(
&HashAlgHandle,
BCRYPT_SHA512_ALGORITHM,
NULL,
0);
if(Status)
{
}
//
// Calculate the length of the Hash
//
Status= BCryptGetProperty(
HashAlgHandle,
BCRYPT_HASH_LENGTH,
(PBYTE)&HashDigestLength,
sizeof(HashDigestLength),
&ResultLength,
0);
if(Status)
{
}
//allocate the Hash buffer on the heap
HashDigest = (PBYTE)HeapAlloc (GetProcessHeap (), 0, HashDigestLength);
if( NULL == HashDigest )
{
printf("\nNO memory");
Status = STATUS_NO_MEMORY;
}
//
// Create a Hash
//
Status = BCryptCreateHash(
HashAlgHandle,
&HashHandle,
NULL,
0,
NULL,
0,
0);
if(Status)
{
}
//
// Hash Data(s)
//
Status = BCryptHashData(
HashHandle,
(PBYTE)Data,
DataLength,
0);
/* Status = BCryptHashData(
HashHandle,
(PBYTE)Data,
sizeof(Data),
0); */
if(Status)
{
}
//
// Close the Hash
//
Status = BCryptFinishHash(
HashHandle,
HashDigest,
HashDigestLength,
0);
if(Status)
{
}
isi_hexdump("Digest :",HashDigest,HashDigestLength);
*DataDigestPointer = HashDigest;
HashDigest = NULL;
*DataDigestLengthPointer = HashDigestLength;
Status = 0;
cleanup:
if( NULL != HashDigest )
{
HeapFree( GetProcessHeap(), 0, HashDigest );
HashDigest = NULL;
}
if( NULL != HashHandle )
{
Status = BCryptDestroyHash(HashHandle);
HashHandle = NULL;
}
if( NULL != HashAlgHandle )
{
BCryptCloseAlgorithmProvider(HashAlgHandle,0);
}
return Status;
}
int main()
{
long privksize = 0;
unsigned char *privkey = NULL;
if (readBinFile(&privkey, "privkey.der", &privksize) != 0)
return 1;
CRYPT_DECODE_PARA decode_para = {0, 0, 0};
BCRYPT_RSAKEY_BLOB *rsa_private_key = NULL;
DWORD rsa_private_key_size = 0;
BOOL ret_val = FALSE;
PCRYPT_PRIVATE_KEY_INFO PrivateKeyInfo;
ret_val = CryptDecodeObjectEx(X509_ASN_ENCODING | PKCS_7_ASN_ENCODING,
PKCS_PRIVATE_KEY_INFO,
privkey,
privksize,
CRYPT_DECODE_ALLOC_FLAG|CRYPT_DECODE_NOCOPY_FLAG,
0,
(void**)&PrivateKeyInfo,
&rsa_private_key_size);
if(!ret_val)
{
printf("\nCryptDecodeObjectEx() Failed : status <0x%x>, ret val <%d>",GetLastError(), ret_val);
}
else
{
printf("\nCryptDecodeObjectEx() Success : status <0x%x>, ret val <%d>",GetLastError(), ret_val);
}
ret_val = CryptDecodeObjectEx(X509_ASN_ENCODING | PKCS_7_ASN_ENCODING,
CNG_RSA_PRIVATE_KEY_BLOB,
PrivateKeyInfo->PrivateKey.pbData,
PrivateKeyInfo->PrivateKey.cbData,
CRYPT_DECODE_ALLOC_FLAG,
0,
(void**)&rsa_private_key,
&rsa_private_key_size);
if(!ret_val)
{
printf("\nCryptDecodeObjectEx() Failed : status <0x%x>, ret val <%d>",GetLastError(), ret_val);
}
else
{
printf("\nCryptDecodeObjectEx() Success : status <0x%x>, ret val <%d>",GetLastError(), ret_val);
}
LocalFree(PrivateKeyInfo);
BCRYPT_KEY_HANDLE phKey;
NCRYPT_PROV_HANDLE hProvider = 0;
if (NCryptOpenStorageProvider(
&hProvider,
MS_KEY_STORAGE_PROVIDER,
0)) {
printf("\nNCryptOpenStorageProvider() Failed : status <0x%x>",GetLastError());
}
NCRYPT_KEY_HANDLE hKeyNew = 0;
NTSTATUS Status;
if (NCryptImportKey(hProvider,
0,
BCRYPT_RSAPRIVATE_BLOB,
//NCRYPT_PKCS8_PRIVATE_KEY_BLOB,
NULL,
&hKeyNew,
(BYTE *)rsa_private_key,
rsa_private_key_size,
NCRYPT_DO_NOT_FINALIZE_FLAG)) {
printf("\nNCryptOpenStorageProvider() Failed : status <0x%x>",GetLastError());
}
DWORD export_policy = NCRYPT_ALLOW_EXPORT_FLAG | NCRYPT_ALLOW_PLAINTEXT_EXPORT_FLAG;
if (NCryptSetProperty(hKeyNew,
NCRYPT_EXPORT_POLICY_PROPERTY,
(PBYTE)&export_policy,
sizeof(export_policy),
NCRYPT_PERSIST_FLAG)) {
printf("\nFailed to NCryptSetProperty");
}
if(NCryptFinalizeKey(hKeyNew, 0)) {
printf("\nFailed to NCryptFinalizeKey");
}
PBYTE MessageDigest = NULL;
DWORD MessageDigestLength = 0;
PBYTE MessageToSign = "1234567890123456789012345678901234567890123456789012345678901234";
DWORD MessageLength = strlen(MessageToSign);
Status = ComputeHash(MessageToSign,
MessageLength,
&MessageDigest,
&MessageDigestLength);
if( Status )
{
printf("\nFailed to calculate hash");
}
printf("\n hashvalue \n");
isi_hexdump("Digest :",MessageDigest,MessageDigestLength);
PBYTE SignatureBlob = NULL;
DWORD SignatureBlobLength = 0;
DWORD ResultLength = 0;
BCRYPT_PKCS1_PADDING_INFO PKCS1PaddingInfo = {0};
PKCS1PaddingInfo.pszAlgId = BCRYPT_SHA512_ALGORITHM;
Status = NCryptSignHash(
hKeyNew, // Key handle used to sign the hash
&PKCS1PaddingInfo, // Padding information
MessageDigest, // Hash of the message
MessageDigestLength, // Length of the hash
NULL, // Signed hash buffer
0, // Length of the signature(signed hash value)
&SignatureBlobLength, // Number of bytes copied to the signature buffer
BCRYPT_PAD_PKCS1);
if( Status)
{
printf("\nFailed******** 1");
}
printf("\nPavan sign length <%d>",SignatureBlobLength);
//allocate the signature buffer
SignatureBlob = (PBYTE)HeapAlloc (GetProcessHeap (), 0, SignatureBlobLength);
if( NULL == SignatureBlob )
{
Status = NTE_NO_MEMORY;
printf("\nFailed******** 1.0");
}
memset(SignatureBlob, '\0', SignatureBlobLength);
Status = NCryptSignHash(
hKeyNew, // Key handle used to sign the hash
&PKCS1PaddingInfo, // Padding information
MessageDigest, // Hash of the message
MessageDigestLength, // Length of the hash
SignatureBlob, // Signed hash buffer
SignatureBlobLength, // Length of the signature(signed hash value)
&SignatureBlobLength, // Number of bytes copied to the signature buffer
BCRYPT_PAD_PKCS1);
if( Status)
{
printf("\nFailed******** 2 status <0x%x>",Status);
}
else
{
printf("\nSuccess NCryptSignHash");
}
isi_hexdump("Signature :",SignatureBlob,SignatureBlobLength);
return 0;
}
Windows CNG 生成的签名:
#include<stdio.h>
#include<windows.h>
#include <openssl/opensslv.h>
#include <openssl/evp.h>
#include <openssl/crypto.h>
#include <openssl/err.h>
#include <openssl/rand.h>
#include <openssl/des.h>
#include <openssl/ssl.h>
#include<wincrypt.h>
void
isi_hexdump(const char *msg, const unsigned char *in, unsigned int len)
{
printf("%s [%d][", msg, len);
for (; len > 0; len--, in++) {
printf("%02X", *in);
}
printf("]\n");
}
static int
readBinFile(unsigned char **buffer, char *file, long *size)
{
FILE *fp;
size_t foo;
printf("\n inside readBinFile");
fp = fopen(file,"rb");
if (fp == NULL) {
printf("Error: reading the file %s\n", file);
return -1;
}
fseek(fp, 0, SEEK_END);
*size = ftell(fp);
fseek(fp, 0, SEEK_SET);
*buffer = malloc(*size);
if ((foo = fread(*buffer, 1, *size, fp)) != *size)
{
printf("Error: reading the file to buffer %s:%ld\n", file, *size);
return -1;
}
printf("\nExit from readBinFile <%s>",*buffer);
return 0;
}
int main()
{
unsigned char *privkey = NULL;
unsigned char *pubkey = NULL;
unsigned char *sig = NULL;
long privksize, pubksize;
unsigned int sigSize;
char srcstr[] = "1234567890123456789012345678901234567890123456789012345678901234";
int verified = -1;
if (readBinFile(&privkey, "privkey.der", &privksize) != 0)
return 1;
if (readBinFile(&pubkey, "pubkey.der", &pubksize) != 0)
return 1;
printf("data='%s'\n", srcstr);
sig = calloc(1, sigSize);
EVP_PKEY *private_key = NULL;
RSA *rsa = NULL;
if ((private_key = d2i_AutoPrivateKey(NULL, &privkey, privksize)) == NULL) {
printf("\nFailed d2i_AutoPrivateKey ");
}
sigSize = EVP_PKEY_size(private_key);
sig = calloc(1, sigSize);
if ((rsa = EVP_PKEY_get1_RSA(private_key)) == NULL) {
printf("\nFailed EVP_PKEY_get1_RSA ");
}
if ((RSA_sign(EVP_MD_type(EVP_sha512()),
srcstr, strlen(srcstr), sig, &sigSize, rsa)) != 1) {
printf("\nFailed RSA_sign");
}
isi_hexdump("OpenSSL sig",sig,sigSize);
return 0;
}
签名使用OpenSSL:045F86E2D7AFE8001A42A1B113F60F7B96513DB829034DF6BAA27D5E7A13AF52896FBD4DE870D0C3F6734AE8AB061BA6959500BF1473F8726E0CB2819A3FC14318AE3CEFED15AA43C82339290A88EBAE363507FE835A02110D5CE8ABF9594F9195AA8033DCD78C10AA589C9BC523A138655E8485C87A8AB4FAE77ED5DA4268C1BC373A9B1C88572AEB3FD020BBF891FAFE223F3DE5FF89E56B7A916663D76303FAD7C69F2014ACF0FB6418305426FB6CAFF6C332126DF138D7FF9220B0B384BD3A053AA123A521D0AACC5D474AD2A2D06416F38647BC0B4F6AE25AA7BBAC3F7DF6BAFFB9EE8EB4E8E00D4C67252E2C2CAFEDB2F344424B9034371FDDA6C8D152
最佳答案
两种代码的签名方法都不会隐式散列,即必须传递已经散列的数据。虽然这对于 CNG 代码是满意的,但对于 OpenSSL 代码却不是。
对于 OpenSSL 代码,可以按如下方式确定和应用散列:
...
char srcstr[] = "1234567890123456789012345678901234567890123456789012345678901234";
unsigned char* digest;
unsigned int digLen;
digest_message((const unsigned char*)srcstr, strlen((const char*)srcstr), &digest, &digLen);
...
if ((RSA_sign(EVP_MD_type(EVP_sha512()), digest, digLen, sig, &sigSize, rsa)) != 1) {
...
和
void digest_message(const unsigned char* message, size_t message_len, unsigned char** digest, unsigned int* digest_len)
{
EVP_MD_CTX* mdctx = EVP_MD_CTX_new();
EVP_DigestInit_ex(mdctx, EVP_sha512(), NULL);
EVP_DigestUpdate(mdctx, message, message_len);
*digest = (unsigned char*)OPENSSL_malloc(EVP_MD_size(EVP_sha512()));
EVP_DigestFinal_ex(mdctx, *digest, digest_len);
EVP_MD_CTX_free(mdctx);
}
来自 OpenSSL 文档的这个
example。
关于openssl - 由 Windows CNG(下一代加密)生成的 RSA SHA512 签名 NCryptSignHash 与由 openssl 生成的签名不匹配 RSA_sign,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/67600792/
使用sed和/或awk,仅在行包含字符串“ foo”并且行之前和之后的行分别包含字符串“ bar”和“ baz”时,我才希望删除行。 因此,对于此输入: blah blah foo blah bar
例如: S1: "some filename contains few words.txt" S2:“一些文件名包含几个单词 - draft.txt” S3:“一些文件名包含几个单词 - 另一个 dr
我正在尝试处理一些非常困惑的数据。我需要通过样本 ID 合并两个包含不同类型数据的大数据框。问题是一张表的样本 ID 有许多不同的格式,但大多数都包含用于匹配其 ID 中某处所需的 ID 字符串,例如
我想在匹配特定屏幕尺寸时显示特定图像。在这种情况下,对于 Bootstrap ,我使用 col-xx-## 作为我的选择。但似乎它并没有真正按照我认为应该的方式工作。 基本思路,我想显示一种全屏图像,
出于某种原因,这条规则 RewriteCond %{REQUEST_FILENAME} !-f RewriteCond %{REQUEST_FILENAME} !-d RewriteRule ^(.*
我想做类似的东西(Nemerle 语法) def something = match(STT) | 1 with st= "Summ" | 2 with st= "AVG" =>
假设这是我的代码 var str="abc=1234587;abc=19855284;abc=1234587;abc=19855284;abc=1234587;abc=19855284;abc=123
我怎样才能得到这个字符串的数字:'(31.5393701, -82.46235569999999)' 我已经在尝试了,但这离解决方案还很远:) text.match(/\((\d+),(\d+)\)/
如何去除输出中的逗号 (,)?有没有更好的方法从字符串或句子中搜索 url。 alert(" http://www.cnn.com df".match(/https?:\/\/([-\w\.]+
a = ('one', 'two') b = ('ten', 'ten') z = [('four', 'five', 'six'), ('one', 'two', 'twenty')] 我正在尝试
我已经编写了以下代码,我希望用它来查找从第 21 列到另一张表中最后一行的值,并根据这张表中 A 列和另一张表中 B 列中的值将它们返回到这张表床单。 当我使用下面的代码时,我得到一个工作表错误。你能
我在以下结构中有两列 A B 1 49 4922039670 我已经能够评估 =LEN(A1)如2 , =LEFT(B1,2)如49 , 和 =LEFT(B1,LEN(A1)
我有一个文件,其中一行可以以 + 开头, -或 * .在其中一些行之间可以有以字母或数字(一般文本)开头的行(也包含这些字符,但不在第 1 列中!)。 知道这一点,设置匹配和突出显示机制的最简单方法是
我有一个数据字段文件,其中可能包含注释,如下所示: id, data, data, data 101 a, b, c 102 d, e, f 103 g, h, i // has to do with
我有以下模式:/^\/(?P.+)$/匹配:/url . 我的问题是它也匹配 /url/page ,如何忽略/在这个正则表达式中? 该模式应该: 模式匹配:/url 模式不匹配:/url/page 提
我有一个非常庞大且复杂的数据集,其中包含许多对公司的观察。公司的一些观察是多余的,我需要制作一个键来将多余的观察映射到一个单独的观察。然而,判断他们是否真的代表同一家公司的唯一方法是通过各种变量的相似
我有以下 XML A B C 我想查找 if not(exists(//Record/subRecord
我制作了一个正则表达式来验证潜在的比特币地址,现在当我单击报价按钮时,我希望根据正则表达式检查表单中输入的值,但它不起作用。 https://jsfiddle.net/arkqdc8a/5/ var
我有一些 MS Word 文档,我已将其全部内容转移到 SQL 表中。 内容包含多个方括号和大括号,例如 [{a} as at [b],] {c,} {d,} etc 我需要进行检查以确保括号平衡/匹
我正在使用 Node.js 从 XML 文件读取数据。但是当我尝试将文件中的数据与文字进行比较时,它不匹配,即使它看起来相同: const parser: xml2js.Parser = new
我是一名优秀的程序员,十分优秀!