Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

my file to file example #8

Open
l1t1 opened this issue Oct 17, 2020 · 0 comments
Open

my file to file example #8

l1t1 opened this issue Oct 17, 2020 · 0 comments

Comments

@l1t1
Copy link

l1t1 commented Oct 17, 2020

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;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant