md5校验

static void MDFile(char *filename)
{
	FILE *file;
	MD5_CTX context;
	int len;
	unsigned char buffer[1024], digest[16];

	if ((file = fopen(filename, "rb")) == NULL)
		printf("%s can't be opened\n", filename);

	else {
		MD5Init(&context);
		while (len = fread(buffer, 1, 1024, file))
			MD5Update(&context, buffer, len);
		MD5Final(&context, digest);

		for (int i = 0; i < 16; i++)
		{
			printf("%02x", digest[i]); 
		}

		fclose(file);

	}
}

static void MDString()
{
	MD5_CTX md5;
	MD5Init(&md5);
	int i;
	unsigned char encrypt[] = "admin";//21232f297a57a5a743894a0e4a801fc3  
	unsigned char decrypt[16];
	MD5Update(&md5, encrypt, strlen((char *)encrypt));
	MD5Final(&md5, decrypt);
	printf("加密前:%s\n加密后16位:", encrypt);
	for (i = 0; i < 8; i++)
	{
		printf("%02x", decrypt[i]);  //02x前需要加上 %  
	}

	printf("\n加密前:%s\n加密后32位:", encrypt);
	for (i = 0; i < 16; i++)
	{
		printf("%02x", decrypt[i]);  //02x前需要加上 %  
	}

	getchar();

	return ;
}

int main(int argc, char *argv[])
{
	char* pfile = "test.txt";
	MDFile(pfile);
	return 0;
}

你可能感兴趣的:(c,MD5)