encode.c

来自「十六位的RSA加解密程序。程序中包括生成公钥和私钥的生成」· C语言 代码 · 共 49 行

C
49
字号
#include "encode.h"

int main()
{
	FILE *pf1,*pf2,*pf3;
	unsigned long n,e;
	unsigned char PText[2],CText[3];

	pf1=fopen("PK.bin","r");		//取公钥
	fscanf(pf1,"%X-%X",&n,&e);
	fclose(pf1);

	pf2=fopen("plaint.txt","rb");
	pf3=fopen("Cipher.txt","wb");

	PText[0]=0;
	PText[1]=0;
	while(fread(PText,1,1,pf2)!=0)
	{
		encode(PText,CText,n,e);	//加密一段明文
		PText[0]=0;
		PText[1]=0;
		fwrite(CText,1,2,pf3);
	}
	fclose(pf2);
	fclose(pf3);

	return 0;
}

void encode(unsigned char *pT,unsigned char *cT,unsigned long n,unsigned long e)
{
	unsigned long i;
	unsigned long p,c;
	p=(unsigned long)(pT[0]);

	c=1;
	for (i=0;i<16;i++)		//快速幂模求法
	{
		if ((e&0x0001)!=0)
			c=(c*p)%n;
		p=(p*p)%n;
		e=e>>1;
	}
	cT[0]=(unsigned char)(c&0xff);
	cT[1]=(unsigned char)((c&0xff00)>>8);

	return;
}

⌨️ 快捷键说明

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