下面是加密的代码,对于输入的字符,每个字符单独进行加密,但是加密之后的位数不一样,是因为没有填充的关系吗,那要如何解决呢
void RSA::Encrypt(const char *filename, const char *output, const char *pub_key_file)
{
char in[1024];
CStringA strN, strE;
FILE *old_file,*new_file,*pub; //源文件、目标文件、公钥文件
__int64 pub_key,len,i,total;
unsigned char cleartext[10005];
pub=fopen(pub_key_file,"rb"); //公钥文件加载公钥
if(pub==NULL)
return;
fscanf(pub, "%s\n", in);
strE = in; //读入e
strcpy(in, "");
fscanf(pub, "%s\n,", in);
strN = in; //读入n
old_file=fopen(filename,"rb");
if(old_file==NULL)
return;
total=0;
len=fread(cleartext,1,10000,old_file);
total = len;
fclose(old_file);
old_file=fopen(filename,"rb");
if(old_file==NULL)
return;
new_file=fopen(output,"wb");
if(new_file==NULL)
return;
fwrite(&total,sizeof(__int64),1,new_file);
CBigInt m, e, n, c; //构造大数对象并初始化为零
CStringA strC;
e.Get(strE, 10);
n.Get(strN, 10);
memset(cleartext,0x0000,sizeof(cleartext));//清零
len=fread(cleartext,1,10000,old_file);
for (i = 0; i < len; i++)
{
m.Mov(cleartext[i]);
c.Mov(m.RsaTrans(e, n)); //c=m^e mod n
c.Put(strC, 10);
char out[1024] = "";
strncpy(out, strC, strC.GetLength());
out[strC.GetLength()] = '\0';
fprintf(new_file, "%s\n", out);
}
fclose(old_file);
fclose(new_file);
}