⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 eax_main.c

📁 一些封装好的加密函数的源代码
💻 C
字号:
#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 + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -