SMx icon indicating copy to clipboard operation
SMx copied to clipboard

my file to file example

Open l1t1 opened this issue 5 years ago • 0 comments

has a problem, how to trim the padding chars? and I do not want to pad files whose sizes are 16 times.

#include <string.h>
#include <stdio.h>
#include "sm4.h"

int main(int argc, char** argv)
{
	unsigned char key[16] = {0x01, 0x23, 0x45, 0x67, 0x89, 0xab, 0xcd, 0xef, 0xfe, 0xdc, 0xba, 0x98, 0x76, 0x54, 0x32, 0x10};
	unsigned char input[16] = {0x01, 0x23, 0x45, 0x67, 0x89, 0xab, 0xcd, 0xef, 0xfe, 0xdc, 0xba, 0x98, 0x76, 0x54, 0x32, 0x10};
	unsigned char output[16];
	sm4_context ctx;
	unsigned long i,n;
	FILE *fp,*fo;
	//if (argc!=4 || argv[1][0]!='d' || argv[1][0]!='e' ) {printf("usage: a e|d inputfile outputfile\n%d %s %s %s",argc,argv[1],argv[2],argv[3]);return 0;}
	fp = fopen(argv[2],"rb");
	fo = fopen(argv[3],"wb");
	
	i = 0;
	if(argv[1][0]=='e')sm4_setkey_enc(&ctx, key);
	if(argv[1][0]=='d')sm4_setkey_dec(&ctx, key);
	while (!feof(fp))
	{
		n=fread(input,sizeof(char),16,fp);
		//for (i = 0; i < 16; i++)printf("%c ", input[i]);
		if (n==0) break;
		if (n<16) for(i=0;i<16-n;i++)input[n+i]=16-n;
		if(argv[1][0]=='e')
		sm4_crypt_ecb(&ctx, 1, 16, input, output);
		if(argv[1][0]=='d')
		sm4_crypt_ecb(&ctx, 0, 16, input, output);
		fwrite(output,sizeof(char),16,fo);
		//i++;
	}
	fclose(fp);
	fclose(fo);
	printf("sm4 %s to %s .\n",argv[2],argv[3]);
	return 0;
}

l1t1 avatar Oct 17 '20 12:10 l1t1