⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 rsacode.cpp

📁 这是一个rsa的加密程序,可以加密一般的文本文件,对于数字签名有很重要的应用,还可以用于图象的加密
💻 CPP
字号:
// RSACode.cpp: implementation of the CRSACode class.
//
//////////////////////////////////////////////////////////////////////

#include "stdafx.h"
#include "文本加密系统.h"

#include "RSACode.h"
#include "MathFunc.h"

#ifdef _DEBUG
#undef THIS_FILE
static char THIS_FILE[]=__FILE__;
#define new DEBUG_NEW
#endif

//////////////////////////////////////////////////////////////////////
// Construction/Destruction
//////////////////////////////////////////////////////////////////////

CRSACode::CRSACode()
{

}

CRSACode::~CRSACode()
{

}


/************************************************************/
// 函数名:		RSA_Encrypt
// 功能:		RSA加密。
// 参数:		nLen			为加密内容的长度;
//				KeyStr,ModStr	为0结尾的密钥串,用于设置RSA密钥和模n。								
//				Import,Export	输入和输出。
// 返回值:		加密后的密文长度。
/************************************************************/
int CRSACode::RSA_Encrypt( char *Export, char *Import, UINT nLen,char *strKey, char *strMod )
{
	static BigNum  a,c;
	CMathFunc math;

	if( !( Export && Import && nLen && RSA_SetKey(strKey,strMod) ) )
		return false;

    // 由输入构造一个大数a
	if( !( BP.BuildBIFromByte(a,Import,nLen) && a.len <= n.len ) )
	{
		AfxMessageBox( "错误:RSA加密(解密)的内容过长!", MB_OK | MB_ICONERROR );
		return false;
	}
	
	// 加密(解密)
	if( !( BP.PowMod( c, a, key, n ) ) )
		return false;

    math.HalfByte2Byte( Export, c.bit, c.len );

	return	(c.len+1)>>1;
}

/************************************************************/
// 函数名:		RSA_Decrypt
// 功能:		RSA解密。
// 参数:		nLen			为解密内容的长度;
//				KeyStr,ModStr	为0结尾的密钥串,用于设置RSA密钥和模n。				
//				Import,Export	输入和输出。
// 返回值:		解密后的明文长度。
/************************************************************/
int CRSACode::RSA_Dencrypt( char *Export, char *Import, UINT nLen, char *strKey, char *strMod )
{
	return RSA_Encrypt( Export, Import, nLen, strKey, strMod );
}

/************************************************************/
// 函数名:		RSA_SetKey
// 功能:		由输入密钥串设置RSA密钥和模n。
// 参数:		strKey,strKey	以0结尾的密钥串,对应密钥和模n。
// 返回值:		设置成功返回true,否则返回false。
/************************************************************/
bool CRSACode::RSA_SetKey( char *strKey, char *strMod )
{
	int k_len,n_len;

	key = n = Zero;

	if( !( strKey && strMod && strKey && (k_len=strlen(strKey)) && (n_len=strlen(strMod)) &&
		       k_len<=n_len && BP.BuildBIFromStr(key,strKey,k_len) && BP.BuildBIFromStr(n,strMod,n_len) ) )
	{
		AfxMessageBox( "设置RSA密钥出错:空密钥,或者密钥太长!", MB_OK | MB_ICONERROR );
		return false;
	}

	return true;
}

/************************************************************/
// 函数名:		RSA_GetKey
// 功能:		获取RSA密钥对。
// 参数:		
// 返回值:		获取成功返回true,否则返回false。
// 素数p,q,  在调用前将p,q置0。
/************************************************************/
bool CRSACode::RSA_GetKey( BigNum &p, BigNum &q, BigNum &e, BigNum &d, BigNum &n,
						  UINT plen, UINT qlen, UINT elen )
{
	BigNum p_1, q_1, n_1, tmp;

    // 如果p,q=0,则产生素数
	if( !p.len )
	{
		if( !( BP.GetPrime(p,plen) ) )
			return false;
	}
	if( !q.len )
	{
		if( !( BP.GetPrime(q,qlen) ) )
			return false;
	}
    
	if( !( p.len>4 && p.len<=BI_MAXLEN/4 && q.len>4 && q.len<=BI_MAXLEN/4 &&    //BI_MAXLEN=300
	           elen>=max(p.len,q.len) && elen<=p.len+q.len ) )
	{
		AfxMessageBox( "长度不在合法范围之内!", MB_OK | MB_ICONERROR );
		return false;
	}

	if( !( BP.Compare(p,q) ) )
	{
		AfxMessageBox( "错误:素数P,Q相同!", MB_OK | MB_ICONERROR );
		return false;
	}

    // 计算n
	if( !( BP.Mul(n,p,q) ) )
		return false;

	// 防止密钥长度超过N,以免造成死循环
	if( elen>n.len )
		elen = n.len;
    p_1 = p; p_1.bit[0] -= 1;
    q_1 = q; q_1.bit[0] -= 1;

	if( !( BP.Mul(n_1,p_1,q_1) ) )
		return false;

	while( true )
	{
		BP.RandVal(e,elen);
		if( BP.Compare(e,n)<0 && BP.Inverse(e,d,n_1,tmp) )
			return true;
	}
}

/************************************************************/
// 函数名:		RSA_Decrypt
// 功能:		RSA解密。
// 参数:		nLen			为解密内容的长度;
//				KeyStr,ModStr	为0结尾的密钥串,用于设置RSA密钥和模n。
//				Import,Export	输入和输出。
// 返回值:		解密后的明文长度。
/************************************************************/
int CRSACode::RSA_Decrypt(char *Export, char *Import, UINT nLen, char *strKey, char *strMod)
{
    return RSA_Encrypt( Export, Import, nLen, strKey, strMod );
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -