/*
本工程中主要使用了RSA的产生公私钥对、公钥加密和私钥解密功能,
即GenerateKeyPair、和RSA_Pub_Encrypt和RSA_Pri_Decrypt_CRT两个函数。
*/
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include "stdafx.h"
#include "global_rsa.h"
#include "rsaref.h"
#include "r_random.h"
#include "nn.h"
#include "prime.h"
#include "rsa.h"
#include "string.h"
#define MYE_LEN 3
unsigned char myE[MYE_LEN]={1,0,1};
R_RANDOM_STRUCT g__randomstruct =
{
0,
"1",
0,
"1"
};
/*
函数功能:产生公私钥对
入口参数:
module_length:密钥长度(位)
出口参数:(公开指数E固定:01 00 01)
N:公钥模数N=PQ
P:N的第一个因子P
Q:N的第二个因子Q
DP:CRT指数dP
DQ:CRT指数dQ
U:CRT系数qInV
*/
int GenerateKeyPair( unsigned long module_length, unsigned char *N,
unsigned char *P, unsigned char *Q,
unsigned char *DP, unsigned char *DQ, unsigned char *U )
{
int i;
unsigned long len;
R_RSA_PUBLIC_KEY publickey;
R_RSA_PRIVATE_KEY privatekey;
R_RSA_PROTO_KEY protokey;
int status;
publickey.bits = module_length;
len = module_length / 8;
memset( &publickey, 0, sizeof( publickey ) );
memset( &privatekey, 0, sizeof( privatekey ) );
memset( &protokey, 0, sizeof( protokey ) );
for(i=0;i<MYE_LEN;i++)
{
publickey.exponent[ sizeof( publickey.exponent ) - i-1 ] = myE[i];
}
protokey.bits = module_length;
protokey.useFermat4 = 1;
srand( (unsigned)time( NULL ) );
for(i=0;i<16;i++)
{
g__randomstruct.state[i]=rand()%255;
g__randomstruct.output[i]=rand()%255;
}
status = R_GeneratePEMKeys( &publickey, &privatekey, &protokey, &g__randomstruct );
if( !status )
{
if( N )
{
memcpy( N, publickey.modulus + sizeof( publickey.modulus ) - len, len );
}
len /= 2;
if( P )
{
memcpy( P, privatekey.prime[ 0 ] + sizeof( privatekey.prime[ 0 ] ) - len, len );
}
if( Q )
{
memcpy( Q, privatekey.prime[ 1 ] + sizeof( privatekey.prime[ 1 ] ) - len, len );
}
if( DP )
{
memcpy( DP, privatekey.primeExponent[ 0 ] + sizeof( privatekey.primeExponent[ 0 ] ) - len, len );
}
if( DQ )
{
memcpy( DQ, privatekey.primeExponent[ 1 ] + sizeof( privatekey.primeExponent[ 1 ] ) - len, len );
}
if( U )
{
memcpy( U, privatekey.coefficient + sizeof( privatekey.coefficient ) - len, len );
}
}
return status;
}
/*
函数功能:公钥加密
入口参数:
inputdata:待加密的数据
inputlength:待加密数据长度
publicKey:公钥结构体
publicKey.bits:公钥长度(位)
publicKey.modulus:公钥模数N
出口参数:
outputdata:加密后数据
outputlength:加密后数据长度
*/
int RSA_Pub_Encrypt( unsigned char *outputdata, unsigned long *outputlength,
unsigned char *inputdata, unsigned long inputlength,
R_RSA_PUBLIC_KEY *publicKey )
{
int i;
unsigned long mylength;
R_RSA_PUBLIC_KEY mypublickey;
unsigned char myinputdata[ MAX_RSA_MODULUS_LEN ];
int status;
//bits
mypublickey.bits=publicKey->bits;
//exponent
memset(mypublickey.exponent, 0, MAX_RSA_MODULUS_LEN);
for(i=0;i<MYE_LEN;i++)
{
mypublickey.exponent[ sizeof( mypublickey.exponent ) - i-1 ] = myE[i];
}
//modulus
mylength = mypublickey.bits/8;
memset(mypublickey.modulus, 0, MAX_RSA_MODULUS_LEN);
for(i=0;i<(int)mylength;i++)
{
mypublickey.modulus[MAX_RSA_MODULUS_LEN-mylength+i]=publicKey->modulus[i];
}
//input
memset( myinputdata, 0, sizeof( myinputdata ) );
for( i = 0; i < ( int )inputlength; i++ )
{
myinputdata[ i ] = inputdata[ i ]; //数据不够时在前面
}
status = RSAPublicEncrypt( outputdata, outputlength, myinputdata, mylength, &mypublickey );
return status;
}
/*
函数功能:CRT私钥解密
入口参数:
inputdata:待解密的数据
inputlength:待解密数据长度
module_length:密钥长度(位)
N:公钥模数N=PQ
P:N的第一个因子P
Q:N的第二个因子Q
DP:CRT指数dP
DQ:CRT指数dQ
U:CRT系数qInV
出口参数:
outputdata:加密后数据
outputlength:加密后数据长度
*/
int RSA_Pri_Decrypt_CRT( unsigned char *outputdata, unsigned long *outputlength,
unsigned char *inputdata, unsigned long inputlength,unsigned long module_length,
unsigned char *N, unsigned char *P,unsigned char *Q,
unsigned char *DP,unsigned char *DQ,unsigned char *U )
{
int i, status;
unsigned long len;
unsigned char myin[ MAX_RSA_MODULUS_LEN ];
R_RSA_PRIVATE_KEY privatekey;
memset( &privatekey, 0, sizeof( privatekey ) );
privatekey.bits = module_length;
len = module_length / 8;
for( i = 0; i < ( int )MYE_LEN; i++ )
{
privatekey.publicExponent[ sizeof( privatekey.publicExponent ) - MYE_LEN + i ] = myE[ i ];
}
for( i = 0; i < ( int )len; i++ )
{
privatekey.modulus[ sizeof( privatekey.modulus ) - len + i ] = N[ i ];
}
len = module_length / 16;
for( i = 0; i < ( int )len; i++ )
{
privatekey.prime[ 0 ][ sizeof( privatekey.prime[ 0 ] ) - len + i ] = P[ i ];
}
for( i = 0; i < ( int )len; i++ )
{
privatekey.prime[ 1 ][ sizeof( privatekey.prime[ 1 ] ) - len + i ] = Q[ i ];
}
for( i = 0; i < ( int )len; i++ )
{
privatekey.primeExponent[ 0 ][ sizeof( privatekey.primeExponent[ 0 ] ) - len + i ] = DP[ i ];
}
for( i = 0; i < ( int )len; i++ )
{
privatekey.primeExponent[ 1 ][ sizeof( privatekey.primeExponent[ 1 ] ) - len + i ] = DQ[ i ];
}
for( i = 0; i < ( int )len; i++ )
{
privatekey.coefficient[ sizeof( privatekey.coefficient ) - len + i ] = U[ i ];
}
//input
len = module_length / 8;
memset( myin, 0, sizeof( myin ) );
for( i = 0; i < ( int )inputlength; i++ )
{
myin[ i ] = inputdata[ i ]; //数据不够时在前面
}
status = RSAPrivateDecrypt_crt( outputdata, outputlength, myin, len, &privatekey );
return status;
}
/* RSA public-key encryption, according to PKCS #1.
*/
int RSAPublicEncrypt(
unsigned char *output, /* output block */
unsigned long *outputLen, /* length of output block */
unsigned char *input, /* input block */
unsigned long inputLen, /* length of input block */
R_RSA_PUBLIC_KEY *publicKey) /* RSA public key */
{
int status;
unsigned char pkcsBlock[MAX_RSA_MODULUS_LEN];
unsigned long modulusLen;
modulusLen = (publicKey->bits + 7) / 8;
if (inputLen > modulusLen)
return (RE_LEN);
memset(pkcsBlock, 0, MAX_RSA_MODULUS_LEN);
R_memcpy (pkcsBlock, input, inputLen);
status = RSAPublicBlock(output, outputLen, pkcsBlock, inputLen, publicKey);
R_memset ((POINTER)pkcsBlock, 0, sizeof (pkcsBlock));
return (status);
}
/* RSA public-key decryption, according to PKCS #1.
*/
int RSAPublicDecrypt (
unsigned char *output, /* output block */
unsigned long *outputLen, /* length of output block */
unsigned char *input, /* input block */
unsigned long inputLen, /* length of input block */
R_RSA_PUBLIC_KEY *publicKey) /* RSA public key */
{
int status;
unsigned char pkcsBlock[MAX_RSA_MODULUS_LEN];
unsigned long modulusLen;
modulusLen = (publicKey->bits + 7) / 8;
if (inputLen > modulusLen)
return (RE_LEN);
memset(pkcsBlock, 0, MAX_RSA_MODULUS_LEN);
R_memcpy (pkcsBlock, input, inputLen);
status = RSAPublicBlock(output, outputLen, pkcsBlock, inputLen, publicKey);
/* Zeroize potentially sensitive information.
*/
R_memset ((POINTER)pkcsBlock, 0, sizeof (pkcsBlock));
return (status);
}
/* RSA private-key encryption, according to PKCS #1.
*/
int RSAPrivateEncrypt_crt (
unsigned char *output, /* output block */
unsigned long *outputLen, /* length of output block */
unsigned char *input, /* input block */
unsigned long inputLen, /* length of
- 1
- 2
- 3
- 4
前往页