eax_main.c

来自「一些封装好的加密函数的源代码」· C语言 代码 · 共 99 行

C
99
字号
#include "tomcrypt.h"
int main(void)
{
	int err,i;
	eax_state eax;
	unsigned char pt[64],pt2[64], ct[64], tag[16],nonce[16]="aaaaaaaaaaaaaaaa", key[16]="1234567890abcdef";
	unsigned long taglen;
	if (register_cipher(&rijndael_desc) == -1) 
	{
		printf("Error registering Rijndael");
		return EXIT_FAILURE;
	}
/* ... make up random nonce and key ... */
	for(i=0;i<64;i++)
	{
		pt[i]=i;
	}
	for(i=0;i<64;i++)
	{
		printf("%x",pt[i]);
	}
	printf("\n\n");
/* initialize context */

	if ((err = eax_init( &eax, /* the context */
		find_cipher("rijndael"), /* cipher we want to use */
		key,16,
		nonce, /* our state nonce */
		16, /* none is 16 bytes */
		"TestApp", /* example header, identifies this program */
		7) /* length of the header */
		) != CRYPT_OK) 
	{
		printf("Error eax_init: %s", error_to_string(err));
		return EXIT_FAILURE;
	}
/* now encrypt data, say in a loop or whatever */
	if ((err = eax_encrypt( &eax, /* eax context */
		pt, /* plaintext (source) */
		ct, /* ciphertext (destination) */
		sizeof(pt) /* size of plaintext */
		) != CRYPT_OK))
	{
		printf("Error eax_encrypt: %s", error_to_string(err));
		return EXIT_FAILURE;
	}
	for(i=0;i<64;i++)
	{
		printf("%x",ct[i]);
	}
	printf("\n\n");

/* initialize context */

	if ((err = eax_init( &eax, /* the context */
		find_cipher("rijndael"), /* cipher we want to use */
		key,16,
		nonce, /* our state nonce */
		16, /* none is 16 bytes */
		"TestApp", /* example header, identifies this program */
		7) /* length of the header */
		) != CRYPT_OK) 
	{
		printf("Error eax_init: %s", error_to_string(err));
		return EXIT_FAILURE;
	}

	if ((err = eax_decrypt( &eax, /* eax context */
		ct, /* claintext (source) */
		pt2, /* ciphertext (destination) */
		sizeof(ct) /* size of plaintext */
		) != CRYPT_OK))
	{
		printf("Error eax_decrypt: %s", error_to_string(err));
		return EXIT_FAILURE;
	}
	for(i=0;i<64;i++)
	{
		printf("%x",pt2[i]);
	}
		printf("\n\n");
/* finish message and get authentication tag */
	taglen = sizeof(tag);
	if ((err = eax_done( &eax, /* eax context */
		tag, /* where to put tag */
		&taglen /* length of tag space */
		) != CRYPT_OK))
	{
		printf("Error eax_done: %s", error_to_string(err));
		return EXIT_FAILURE;
	}
	for(i=0;i<16;i++)
	{
		printf("%x",tag[i]);
	}
		printf("\n");
		return 1;
/* now we have the authentication tag in "tag" and it's taglen bytes long */
}

⌨️ 快捷键说明

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