这个RSA加密代码有什么问题? [英] What is wrong with this RSA encryption code?

查看:79
本文介绍了这个RSA加密代码有什么问题?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

代码执行成功,但快速查看生成的文件会显示后续结果。我错了什么?



  //   SaveAppData.cpp:定义控制台应用程序的入口点。 
#define _CRT_RAND_S
#include < cstdlib >
#include < stdio.h >
#include < string.h >
#include < math.h >
#include openssl / constant_time_locl.h //此头文件必须手动获取并放入openssl文件夹
#include openssl / cryptlib.h //这个头文件必须手动获取并放入openssl文件夹
#include openssl / bn.h
# include openssl / rsa.h
#include openssl / pem.h
#include openssl / err.h
#include openssl / evp.h
# include openssl / rand.h
#include < span class =code-string> openssl / sha.h
#include Header.h


#define LOG(x)3.32 * log10(x)
#define ENTROPY(x) - (x * LOG(x))

#define RSA_F_RSA_PADDING_CHECK_PKCS1_OAEP_MGF1 153


const int KEY_LENGTH = 4096 ;
const int PUB_EXP = RSA_F4;

const int BUFFER_LENGTH = 256 ;
unsigned int uiTable [BUFFER_LENGTH];

double 分析( int iDataNum);
void ReadData( unsigned char * pData, int iLength);
int GenerateRandomString( char * pszString, int nLength);
double FindEntropy( unsigned char * pData, int iDataNum);

int main( int argc, char * argv [])
{
char * pszData = 这是对我的测试;
char * pszDestinationFile = TestFile< /跨度>;
char * pszDestinationFile1 = TestFile1< /跨度>;
char szBuffer [ 256 ];
BIGNUM * pBigNumber = NULL;
RSA * pKeyPair = NULL;

BIO * pBioPub = NULL;
char * pszPubKey = NULL;

BIO * pBioPriv = NULL;
char * pszPrivKey = NULL;
unsigned char * pEncryptData = NULL;
bool bSuccess = true ;
double dEntropy;

if (!GenerateRandomString(szBuffer, 255 ))
{
return 0 ;
}

dEntropy = FindEntropy(( unsigned char *) szBuffer,strlen的(szBuffer));

pBigNumber = BN_new();
if (!pBigNumber)
{
bSuccess = 0 ;
goto 清理;
}

pKeyPair = RSA_new();
if (!pKeyPair)
{
bSuccess = 0 ;
goto 清理;
}

if (!BN_set_word(pBigNumber,PUB_EXP))
{
bSuccess = 0 ;
goto 清理;
}

RAND_add(szBuffer,strlen(szBuffer),dEntropy);

if (!RSA_generate_key_ex(pKeyPair,KEY_LENGTH,pBigNumber,NULL))
{
bSuccess = 0 ;
goto 清理;
}

pBioPub = BIO_new(BIO_s_mem());
PEM_write_bio_RSAPublicKey(pBioPub,pKeyPair);
int iPubKeyLen = BIO_pending(pBioPub);

pszPubKey = new char [iPubKeyLen + 1 ];
BIO_read(pBioPub,pszPubKey,iPubKeyLen);
pszPubKey [iPubKeyLen] = ' \ 0';

FILE * fp = fopen(pszDestinationFile, wb,ccs = UTF-8< /跨度>);
if (!fp)
{
bSuccess = 0 ;
goto 清理;
}
if (!fwrite(pszPubKey, sizeof char )*(iPubKeyLen + 1 ), 1 ,fp ))
{
bSuccess = 0 ;
goto 清理;
}
fclose(fp);

pEncryptData = new unsigned char [RSA_size(pKeyPair)];
if (!RSA_private_encrypt(strlen(pszData)+ 1 ,( unsigned char *)pszData,pEncryptData,pKeyPair,RSA_PKCS1_OAEP_PADDING))
{
bSuccess = 0 ;
goto 清理;
}

fp = fopen(pszDestinationFile1, wb,ccs = UTF- 8\" );
if (!fp)
{
bSuccess = 0 ;
goto 清理;
}
if (!fwrite(pEncryptData,RSA_size(pKeyPair), 1 ,fp))
{
bSuccess = 0 ;
goto 清理;
}
fclose(fp);

清理:
如果(pBigNumber)
{
BN_free(pBigNumber);
}

if (pKeyPair)
{
RSA_free(pKeyPair);
}

if (pBioPub)
{
BIO_free_all(pBioPub);
}

if (pszPubKey)
{
删除 [] pszPubKey;
}
if (pEncryptData)
{
delete [] pEncryptData;
}

return bSuccess;
}


double FindEntropy( unsigned < span class =code-keyword> char
* pData, int iDataNum)
{
memset(uiTable, 0 sizeof (uiTable));

ReadData(pData,iDataNum);
double dEntropy = Analyze(iDataNum);

return dEntropy;
}

void ReadData( unsigned char * pData, int iLength)
{
int ch;

for int i = 0 ; i< iLength; i ++)
{
ch = pData [i];
uiTable [ch] ++;
}
}

double 分析( int iDataNum)
{
double dAccum = 0 0 ;
double dFreq;
register int z;

for (z = 0 ; z< BUFFER_LENGTH; z ++)
{
if (uiTable [z])
{
dFreq =( double )uiTable [z] / iDataNum;
dAccum + =( double )ENTROPY(dFreq);
}
}

return dAccum;

}

int GenerateRandomString( char * pszString, int nLength)
{
unsigned int iNumber;
int iCount = 0 ;

if (rand_s(& iNumber))
{
返回 0 ;
}

while (iCount< nLength)
{
int iChar = iNumber% 256 ;

if (isprint(iChar)&&!isspace(iChar))
{
pszString [iCount ] =( char )iChar;
iCount ++;
}
if (rand_s(& iNumber))
{
return 0 ;
}
}

pszString [nLength] = ' \ 0' ;

return 1 ;
}









公钥文件如下:< br $>


----- BEGIN RSA PUBLIC KEY -----

MIICCgKCAgEAnK / 2DuuahdzthOQBL3d7fm8vWBMye7FHGSR8hceJUaAQUS21XweP

cGHJ9A6Z1B / V29hWqjtTAxy7yj3y / eo7J + hsKgR + FqCsErw77bHFbWDM3 + wYnqVE

y0FjSKT0CHSX88LZ6l + 0grf21gpBhpgcDWwEfptBMLz1ImQtR7sDqQQeUok6yNez

cy4rrDf4A0NVlVHtyVRkXoN9r2AfM81codtmYXnEcTaODmyrHYuJIHuOKNFAiPVd

nQwYY14V6N + KyBDUgGbJ52Iu6G + xN8fgzc0HVeLPRBrfx6uQ8fPYPso6q + 6QfX4C

G75fkVIYLcSxivFWwziZqsni2UYVljayiFaPzGkNWeBqejTN6CnHnoJ15ZuP9791

X / UFjw / ihSYWqayDNtKvy79nN7YFv66oM090qqzgW6pMeqXHDmuqEDqZcD / 7tcWf

RByR0MAiuoT2rUQ2jo0y21 + SH / + + MhCNwjjoRnLa lGdPESqW / 2iR5XOWB31qfPJ /

9SxOnqratjy7Zkfwapxu8ahg22rRe5jWtj7wZ02aarWYuYy16T9Wgg2vUtaGYnqh

sWREaxp / F3ZuWUYIDnF8FJxKbNlweSjkWDRJwX5qsPh / 5XhUCMGs69ZsBwSw4h0r

zoFq93 + E0mmDNgjjhviLQlgbwfrjpJy2WpZIEZHsRCS51OEyCNXTm48CAwEAAQ ==

----- END RSA PUBLIC KEY -----



加密的消息如下:



ÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍ ÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍ







我不认为上述加密消息可能是正确的加密消息。

解决方案

RSA_private_encrypt返回-1表示错误。



您可能还有其他错误,但其中一个突出。

The code executes successfully, but a quick look at the files generated shows the succeeding results. What am I getting wrong?

// SaveAppData.cpp : Defines the entry point for the console application.
#define _CRT_RAND_S
#include <cstdlib>
#include <stdio.h>
#include <string.h>
#include <math.h>
#include "openssl/constant_time_locl.h"//This header file has to be manually got and put in openssl folder
#include "openssl/cryptlib.h"//This header file has to be manually got and put in openssl folder
#include "openssl/bn.h"
#include "openssl/rsa.h"
#include "openssl/pem.h"
#include "openssl/err.h"
#include "openssl/evp.h"
#include "openssl/rand.h"
#include "openssl/sha.h"
#include "Header.h"


#define LOG(x) 3.32 * log10(x)
#define ENTROPY(x) -(x * LOG(x))

#define RSA_F_RSA_PADDING_CHECK_PKCS1_OAEP_MGF1                 153


const int KEY_LENGTH = 4096;
const int PUB_EXP = RSA_F4;

const int BUFFER_LENGTH = 256;
unsigned int uiTable[BUFFER_LENGTH];

double Analyze(int iDataNum);
void ReadData(unsigned char *pData, int iLength);
int GenerateRandomString(char *pszString,int nLength);
double FindEntropy(unsigned char *pData, int iDataNum);

int main(int argc,char *argv[])
{
	char *pszData =  "This is a test for me";
	char *pszDestinationFile = "TestFile";
	char *pszDestinationFile1 = "TestFile1";
	char szBuffer[256];
	BIGNUM *pBigNumber  = NULL;
	RSA * pKeyPair = NULL;

	BIO *pBioPub = NULL;
	char *pszPubKey = NULL;

	BIO *pBioPriv = NULL;
	char *pszPrivKey = NULL;
    unsigned char * pEncryptData = NULL;
	bool bSuccess = true;
	double dEntropy;

	if(!GenerateRandomString(szBuffer,255))
	{
		return 0;
	}

	dEntropy = FindEntropy((unsigned char *)szBuffer,strlen(szBuffer));

	pBigNumber = BN_new();
	if(!pBigNumber)
	{
		bSuccess =  0;
		goto Cleanup;
	}

	pKeyPair = RSA_new();
	if(!pKeyPair)
	{
		bSuccess =  0;
		goto Cleanup;
	}

	if(!BN_set_word(pBigNumber,PUB_EXP))
	{
		bSuccess =  0;
		goto Cleanup;
	}
	
	RAND_add(szBuffer,strlen(szBuffer),dEntropy);

	if(!RSA_generate_key_ex(pKeyPair,KEY_LENGTH,pBigNumber,NULL))
	{
		bSuccess =  0;
		goto Cleanup;
	}

	pBioPub = BIO_new(BIO_s_mem());
	PEM_write_bio_RSAPublicKey(pBioPub,pKeyPair);
	int  iPubKeyLen = BIO_pending(pBioPub);

	pszPubKey = new char [iPubKeyLen + 1];
	BIO_read(pBioPub,pszPubKey,iPubKeyLen);
	pszPubKey[iPubKeyLen] = '\0';

	FILE *fp = fopen(pszDestinationFile,"wb ,ccs = UTF-8");
	if(!fp)
	{
		bSuccess =  0;
		goto Cleanup;
	}
	if(!fwrite(pszPubKey,sizeof(char)* (iPubKeyLen + 1),1,fp))
	{
		bSuccess =  0;
		goto Cleanup;
	}
	fclose(fp);
	
	pEncryptData = new unsigned char [RSA_size(pKeyPair)];
	if(!RSA_private_encrypt(strlen(pszData) + 1,(unsigned char *)pszData,pEncryptData,pKeyPair,RSA_PKCS1_OAEP_PADDING))
	{
		bSuccess =  0;
		goto Cleanup;
	}

	fp = fopen(pszDestinationFile1,"wb ,ccs = UTF-8");
	if(!fp)
	{
		bSuccess =  0;
		goto Cleanup;
	}
	if(!fwrite(pEncryptData,RSA_size(pKeyPair),1,fp))
	{
		bSuccess =  0;
		goto Cleanup;
	}
	fclose(fp);

Cleanup:
	if(pBigNumber)
	{
		BN_free(pBigNumber);
	}
	
	if(pKeyPair)
	{
		RSA_free(pKeyPair);
	}

	if(pBioPub)
	{
		BIO_free_all(pBioPub);
	}

	if(pszPubKey)
	{
		delete [] pszPubKey;
	}
	if(pEncryptData)
	{
		delete [] pEncryptData;
	}
	
	return bSuccess;
}


double FindEntropy(unsigned char *pData, int iDataNum)
{
	memset(uiTable,0,sizeof(uiTable));

	ReadData(pData,iDataNum);
	double dEntropy = Analyze(iDataNum);

	return dEntropy;
}

void ReadData(unsigned char *pData, int iLength)
{
	int ch;

	for(int i = 0; i < iLength; i++)
	{
		ch = pData[i];
		uiTable[ch]++;
	}
}

double Analyze(int iDataNum)
{
	double dAccum = 0.0;
	double dFreq;
	register int z;

	for(z = 0; z < BUFFER_LENGTH; z++)
	{
		if(uiTable[z])
		{
			dFreq = (double) uiTable[z]/iDataNum;
			dAccum += (double) ENTROPY(dFreq);
		}
	}

	return dAccum;

}

int GenerateRandomString(char *pszString,int nLength)
{
	unsigned int iNumber;
	int iCount = 0;
			
	if(rand_s(&iNumber))
	{
		return 0;
	}
			
	 while(iCount < nLength)
	 {
		 int iChar = iNumber % 256;

		 if(isprint(iChar) && !isspace(iChar))
		 {
			 pszString[iCount] = (char)iChar;
			 iCount++;
		 }
		 if(rand_s(&iNumber))
		 {
			return 0;
		 }
	 }
	 
	 pszString[nLength] = '\0';

	 return 1;
}





Public Key file follows:

-----BEGIN RSA PUBLIC KEY-----
MIICCgKCAgEAnK/2DuuahdzthOQBL3d7fm8vWBMye7FHGSR8hceJUaAQUS21XweP
cGHJ9A6Z1B/V29hWqjtTAxy7yj3y/eo7J+hsKgR+FqCsErw77bHFbWDM3+wYnqVE
y0FjSKT0CHSX88LZ6l+0grf21gpBhpgcDWwEfptBMLz1ImQtR7sDqQQeUok6yNez
cy4rrDf4A0NVlVHtyVRkXoN9r2AfM81codtmYXnEcTaODmyrHYuJIHuOKNFAiPVd
nQwYY14V6N+KyBDUgGbJ52Iu6G+xN8fgzc0HVeLPRBrfx6uQ8fPYPso6q+6QfX4C
G75fkVIYLcSxivFWwziZqsni2UYVljayiFaPzGkNWeBqejTN6CnHnoJ15ZuP9791
X/UFjw/ihSYWqayDNtKvy79nN7YFv66oM090qqzgW6pMeqXHDmuqEDqZcD/7tcWf
RByR0MAiuoT2rUQ2jo0y21+Sh/+MhCNwjjoRnLa+lGdPESqW/2iR5XOWB31qfPJ/
9SxOnqratjy7Zkfwapxu8ahg22rRe5jWtj7wZ02aarWYuYy16T9Wgg2vUtaGYnqh
sWREaxp/F3ZuWUYIDnF8FJxKbNlweSjkWDRJwX5qsPh/5XhUCMGs69ZsBwSw4h0r
zoFq93+E0mmDNgjjhviLQlgbwfrjpJy2WpZIEZHsRCS51OEyCNXTm48CAwEAAQ==
-----END RSA PUBLIC KEY-----

Encrypted message follows:

ÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍ



I dont think the above encrypted message can be the correct encrypted message.

解决方案

RSA_private_encrypt returns -1 for error.

You may have other mistakes but that one stands out.


这篇关于这个RSA加密代码有什么问题?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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