#include <openssl/rsa.h> #include <openssl/evp.h> #pragma comment(lib, "libeay32.lib") DWORD ReadBufferFromFile(LPCSTR lpFileName, PBYTE pBuffer, PDWORD pdwLength) { if (NULL == lpFileName || NULL == pBuffer || NULL == pdwLength) { return -1; } //---------------------------------------------------------------- // 0. Local variables declaration DWORD nbytes = 0; HANDLE hFile = NULL; DWORD dwBufferLen = *pdwLength; DWORD dwFileLen = 0; //---------------------------------------------------------------- // 1. Read certificate to buff // This certificate contains certificate info (CI) and digital signature (DS) hFile = CreateFileA(lpFileName, GENERIC_READ, 0, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL ); if (hFile == INVALID_HANDLE_VALUE) { return -1; } dwFileLen = GetFileSize(hFile, NULL); *pdwLength = dwFileLen; if (dwBufferLen >= dwFileLen) { ReadFile(hFile, pBuffer, dwFileLen, &nbytes, NULL); } else { if (hFile != NULL) { CloseHandle(hFile); } return -1; } if (hFile != NULL) { CloseHandle(hFile); } return 0; } void FlipBuffer(unsigned char *pBuf, unsigned long ulLen) { if(0 == ulLen) { return; } //char tmp; unsigned char ucTemp; for(unsigned long i = 0; i < ulLen >> 1; ++i) { ucTemp = pBuf[i]; pBuf[i] = pBuf[ulLen - i - 1]; pBuf[ulLen - i - 1] = ucTemp; } } void MSkeyConvertToOpenSSLKey(const char* filename, RSA& Rsa) { if (NULL == filename) { return; } unsigned char byPrivateKeyBlob[2324] = {0}; DWORD dwPrvKeySize = sizeof(byPrivateKeyBlob); ReadBufferFromFile(filename, byPrivateKeyBlob, &dwPrvKeySize); BYTE *pszPubBlob = byPrivateKeyBlob; RSAPUBKEY *rsapubkey = reinterpret_cast<RSAPUBKEY *>(pszPubBlob + sizeof(PUBLICKEYSTRUC)); BYTE *pE = (BYTE *)&(rsapubkey->pubexp); DWORD dwElen = sizeof(DWORD); DWORD dwModulusLen = rsapubkey->bitlen / 8; DWORD dwPrimeLen = rsapubkey->bitlen/16; BYTE *pOffset = pszPubBlob + sizeof(BLOBHEADER) + sizeof(RSAPUBKEY); BYTE *pN = pOffset; pOffset += dwModulusLen; BYTE *pP = pOffset; pOffset += dwPrimeLen; BYTE *pQ = pOffset; pOffset += dwPrimeLen; BYTE *pP1 = pOffset; pOffset += dwPrimeLen; BYTE *pQ1 = pOffset; pOffset += dwPrimeLen; BYTE *pPQ = pOffset; pOffset += dwPrimeLen; BYTE *pD = pOffset; pOffset += dwModulusLen; FlipBuffer(pE, dwElen); FlipBuffer(pN, dwModulusLen); FlipBuffer(pP, dwPrimeLen); FlipBuffer(pQ, dwPrimeLen); FlipBuffer(pP1, dwPrimeLen); FlipBuffer(pQ1, dwPrimeLen); FlipBuffer(pPQ, dwPrimeLen); FlipBuffer(pD, dwModulusLen); while(0x00 == *pE) { pE = pE + 1; dwElen = dwElen - 1; } BIGNUM *pBE = BN_new(); pBE = BN_bin2bn(pE, dwElen, pBE); BIGNUM *pBN = BN_new(); pBN = BN_bin2bn(pN, dwModulusLen, pBN); BIGNUM *pBP = BN_new(); pBP = BN_bin2bn(pP, dwPrimeLen, pBP); BIGNUM *pBQ = BN_new(); pBQ = BN_bin2bn(pQ, dwPrimeLen, pBQ); BIGNUM *pBP1 = BN_new(); pBP1 = BN_bin2bn(pP1, dwPrimeLen, pBP1); BIGNUM *pBQ1 = BN_new(); pBQ1 = BN_bin2bn(pQ1, dwPrimeLen, pBQ1); BIGNUM *pBPQ = BN_new(); pBPQ = BN_bin2bn(pPQ, dwPrimeLen, pBPQ); BIGNUM *pBD = BN_new(); pBD = BN_bin2bn(pD, dwModulusLen, pBD); Rsa.n = pBN; Rsa.e = pBE; Rsa.p = pBP; Rsa.q = pBQ; Rsa.dmp1 = pBP1; Rsa.dmq1 = pBQ1; Rsa.iqmp = pBPQ; Rsa.d = pBD; } void OpenSSLGenFileHash() { RSA* rsa = RSA_new(); MSkeyConvertToOpenSSLKey("Test_1024_Key.prv", *rsa); const EVP_MD* algrothm = EVP_sha1(); EVP_MD_CTX mdctx; EVP_MD_CTX_init(&mdctx); EVP_DigestInit_ex(&mdctx, algrothm, NULL); byte file[512] = {0}; DWORD dwFileSize = sizeof(file); ReadBufferFromFile("PublicKey.bin", file, &dwFileSize); EVP_DigestUpdate(&mdctx, file, dwFileSize); BYTE signValue[128] = {0}; UINT signLen = 0; EVP_DigestFinal_ex(&mdctx, signValue, &signLen); EVP_MD_CTX_cleanup(&mdctx); RSA_free(rsa); }