#include "tommath.h"
#pragma comment(lib,"tommath.lib")
extern "C"
{
#include "miracl.h"
#include "mirdef.h"
}
#pragma comment(linker,"/NODEFAULTLIB:LIBC.lib")
#pragma comment( lib, "miracl.lib" )
......
//使用RSATool生成
char szN[]="9283C1A94C923326183E4060F182D332B392FF0E4CB7B546E5B8556711E8B56669884D9255B19CE5EF238DC64EDB1792E0A5D8B1C88DCE78BA0D27F12B577E727A875388DFB847D4D362AF00683665EF0C42519F55CB7336D51F5BB5D2EDDB360B6660C17FB2EBC208F3870553600D3164BAD5D728B524CF284DFF003A70F751";
char szE[]="DD29F78DBEC28157E37D42FB22FFD42404561777A91C9E76C1E8E7AC632BED8566F16905E61443A8901E0261F6FA61BBF08F28B9A654581E53B76B82568A00AB";
char szD[]="2F01E817134733BE9F75435338C9D645E207826A1F136081270F0A6A29CED484772FD1F14D4BAB29AB26919E7EB3D62DC26CB537A39511658E3683CF29498A18CD77FBE7EB39D4B5ACFB265137B7B3C17F31DF9AB0B6F296B64E5B016743D68E50B0CF5A8EDC3ABF2231C0DE9FD893A10B0828C791218FE29EB2F4FDEC2CE42B";
char szOrgData[]="this is a test这是个测试";
void DbgPrint( LPCSTR lpStr )
{
OutputDebugString( lpStr);
OutputDebugString("\n" );
}
void MyTestMiracl_RSA()
{
big n,e,d,c,m;
miracl *mip=mirsys(100,0);
mip->IOBASE=16; // 16进制模式
c=mirvar(0); // MIRACL的大数类型
n=mirvar(0);
e=mirvar(0);
d=mirvar(0);
m=mirvar(0);
bytes_to_big(strlen(szOrgData),szOrgData,m); // 将数据转换成大数
cinstr(n, szN); // 初始化模数n
cinstr(e, szE); // 公用密钥e
powmod(m,e,n,c); // 计算c = (m ^ e) mod n
CHAR szBuffer[2048]={0};
cotstr(c,szBuffer); // 将c的16进制串表示写入szBuffer中
DbgPrint( szOrgData );
DbgPrint( szBuffer );
cinstr(d, szD );
powmod(c,d,n,m );//m=(c^d)mod n //解密
ZeroMemory( szBuffer, 2048 );
big_to_bytes(0,m,szBuffer,0);
DbgPrint( szBuffer );
mirkill(d);
mirkill(c);
mirkill(n);
mirkill(e);
mirkill(m);
mirexit();
}
voidMyTestTommath_Rsa()
{
mp_int n,d,e,c,m;
mp_init_multi(&n,&d,&e,&c,&m);
mp_read_radix(&n,szN,16 );
mp_read_radix(&d,szD,16 );
mp_read_radix(&e,szE,16 );
OutputDebugString( _T("\n"));
DbgPrint( szOrgData );
mp_read_unsigned_bin(&m,(byte*)szOrgData, strlen(szOrgData ) );
mp_exptmod(&m,&e,&n,&c);
char szBuffer[2048]={0};
mp_toradix(&c,szBuffer, 16);
DbgPrint(szBuffer );
mp_exptmod(&c,&d,&n,&m);
ZeroMemory(szBuffer,2048);
mp_to_unsigned_bin(&m,(BYTE*)szBuffer);
DbgPrint(szBuffer);
mp_clear_multi(&n,&d,&e,&c,&m);
}