?? unit1.cpp
字號(hào):
//---------------------------------------------------------------------------
#include <vcl.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>
#pragma hdrstop
#include "Unit1.h"
#include "rsa.h"
#include "vlong.h"
char *Key_file="rsavar.txt";
int VL;
int find_string_head(unsigned char c,FILE *fp);
int get_vlong(char c,vlong *nu);
vlong _Encrypt(vlong m);
vlong _Decrypte(vlong c);
//---------------------------------------------------------------------------
#pragma package(smart_init)
#pragma resource "*.dfm"
TForm1 *Form1;
//---------------------------------------------------------------------------
__fastcall TForm1::TForm1(TComponent* Owner)
: TForm(Owner)
{
}
//---------------------------------------------------------------------------
void __fastcall TForm1::Button1Click(TObject *Sender)
{
private_key pkey;
char prand[2][128],vername[3],tc;
DWORD i,j,nn, nCount;
vlong *tmp[3];
FILE *fp;
Button1->Enabled=false;
Button2->Enabled=false;
Button3->Enabled=false;
srand((unsigned)time(NULL)); //產(chǎn)生隨機(jī)數(shù),當(dāng)然設(shè)個(gè)種子先
//nCount = 32; //生成512位
nCount = 64; //生成1024位
//nCount = 16; //生成256位
//先產(chǎn)生兩個(gè)大的隨機(jī)數(shù)
for(i=0; i<2; i++)
{
for(j=0; j<nCount; j++)
{
tc = (char)(0x41+rand()%0xAF); //這是什么意思
prand[i][j] = tc;
}
prand[i][j]=0;
}
pkey.create(prand[0], prand[1]); //公鑰、私鑰一次搞掂
tmp[1] = &pkey.e; //把他們寫到一個(gè)文件里面去
tmp[2] = &pkey.d;
tmp[0] = &pkey.m;
strcpy(vername, "med");
fp = fopen(Key_file, "w");
for(j=0; j<3; j++)
{
nn=tmp[j]->value->n;
fprintf(fp, "[%c]\n%d\n", vername[j], nn);
for(i=0; i<nn; i++)
{
fprintf(fp,"%08lX\n",tmp[j]->value->a[i]);
}
}
fclose(fp); //打完收工!
Button1->Enabled=true;
Button2->Enabled=true;
Button3->Enabled=true;
}
//---------------------------------------------------------------------------
//*************************************************
// 不知道叫什么名字好:)
// 功能就是到密鑰文件中間去找你要的密鑰
// 返回密鑰長度并將文件指針指向數(shù)據(jù)開始的地方
//*************************************************
int find_string_head(unsigned char c,FILE *fp)
{
unsigned char tc;
int len;
tc=' ';
while(1)
{
while(tc!='[' && !feof(fp)) tc=fgetc(fp);
if(feof(fp)) return 0;
tc=fgetc(fp);
if(tc==c) break;
}
while(tc!=0x0a) tc=fgetc(fp);
fscanf(fp,"%d\n",&len);
return len;
}
//***********************************************
// 從密鑰文件中間讀取指定的密鑰,并返回密鑰長度
// c就是指定的密鑰標(biāo)志
// 返回的密鑰在nu里面
//***********************************************
int get_vlong(char c,vlong *nu)
{
FILE *fp;
char tc;
int len,i;
DWORD temp[64];
fp=fopen(Key_file,"r");
len=find_string_head(c,fp);
if(len==0) {fclose(fp);return 0;}
for(i=0;i<len;i++)
{
fscanf(fp,"%8lx\n",temp+i);
}
nu->load(temp,len);
return len;
}
//***********************************************
// 這個(gè)就是用私鑰編碼
// 不要去看名字,認(rèn)為是編碼就對(duì)了
//***********************************************
vlong _Encrypt(vlong m)
{
private_key pkey;
VL=get_vlong('m',&pkey.m);
get_vlong('d',&pkey.d);
return pkey.decrypt(m);
}
//***********************************************
// 這個(gè)是用公鑰編碼
//***********************************************
vlong _Decrypte(vlong c)
{
public_key pkey;
VL=get_vlong('m',&pkey.m);
get_vlong('e',&pkey.e);
return pkey.encrypt(c);
}
void __fastcall TForm1::Button2Click(TObject *Sender)
{
int i;
DWORD a[64];
public_key pkey;
Button1->Enabled=false;
Button2->Enabled=false;
Button3->Enabled=false;
VL=get_vlong('m',&pkey.m); //取得密鑰的長度
memset(a,0,VL*sizeof(DWORD));
//printf("Please Input CardNo & PCID (Fmt %%X-%%X):\n");
//scanf("%lX-%lX",&a[0], &a[1]);
a[0]=0x12345678;
a[1]=0xabcdef12;
vlong m;
m.load(a, 2); //這樣 m 中就保存有要加密的數(shù)
vlong c = _Encrypt(m);
c.store(a, VL);
FILE *fp = fopen("nwnb.txt", "w");
if(fp != NULL)
{
for(i=0;i<VL;i++)
{
fprintf(fp,"%08lX\n",a[i]);
}
fclose(fp);
Button1->Enabled=true;
Button2->Enabled=true;
Button3->Enabled=true;
}
}
//---------------------------------------------------------------------------
void __fastcall TForm1::Button3Click(TObject *Sender)
{
int i;
public_key pkey;
Button1->Enabled=false;
Button2->Enabled=false;
Button3->Enabled=false;
VL=get_vlong('m',&pkey.m);
DWORD a[64];
memset(a,0,VL*sizeof(long));
FILE *fp = fopen("nwnb.txt", "r");
if(fp != NULL)
{
for(i=0;i<VL;i++)
{
fscanf(fp,"%08lX\n",&a[i]);
}
fclose(fp);
}
//加密的數(shù)據(jù)保存到m中
vlong m;
m.load(a,VL);
//計(jì)算加密前的數(shù)據(jù),保存在c中
vlong c=_Decrypte(m);
//下面兩句話僅僅是特定的情況,其實(shí)長度不應(yīng)該是2的
memset(a,0,8*sizeof(long));
c.store(a,2);
// a[0], a[1]就是加密前的數(shù)據(jù)
//printf("Decrypt Data:\n");
//printf("%08lX-%08lX\n", a[0], a[1]);
Button1->Enabled=true;
Button2->Enabled=true;
Button3->Enabled=true;
}
//---------------------------------------------------------------------------
?? 快捷鍵說明
復(fù)制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號(hào)
Ctrl + =
減小字號(hào)
Ctrl + -