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

📄 ocb_main.c

📁 一些封装好的加密函数的源代码
💻 C
字号:
#include "tomcrypt.h"
int main(void)
{
	int err,i;
	unsigned long ptlen=34,tagbuflen;
	ocb_state ocb;
	unsigned char *tagbuf,*pt,pt1[32],pt2[32],*ct, ct1[32],tag[16],nonce[16]={0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,  0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01 }, key[16]={ 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f };
	pt=pt1;
	ct=ct1;
	tagbuf=tag;
//	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<32;i++)
	{
		pt[i]=i;
	}
	for(i=0;i<32;i++)
	{
		printf("%x,",pt[i]);
	}
	printf("\n\n");
/* initialize context */

	if ((err = ocb_init( &ocb, /* the context */
		find_cipher("rijndael"), /* cipher we want to use */
		key,16,
		nonce)
		) != CRYPT_OK) 
	{
		printf("Error ocb_init: %s", error_to_string(err));
		return EXIT_FAILURE;
	}
/* now encrypt data, say in a loop or whatever */

	if (err= ocb_encrypt( &ocb, // ocb context  
		pt, // plaintext (source)  
		ct // ciphertext (destination) 
		)!=CRYPT_OK)
	{
		printf("Error ocb_encrypt: %s", error_to_string(err));
		return EXIT_FAILURE;
	}
	ct=ct1+ocb.block_len;
	pt=pt1+ocb.block_len;
	printf("%d\n",sizeof(pt1));
	if (err= ocb_encrypt( &ocb, // ocb context  
		pt, // plaintext (source)  
		ct // ciphertext (destination)  
		)!=CRYPT_OK)
	{
		printf("Error ocb_encrypt: %s", error_to_string(err));
		return EXIT_FAILURE;
	}



//LBL_ERR:
//output the ct
	for(i=0;i<32;i++)
	{
		printf("%x,",ct1[i]);
	}
	printf("\n\n");

/* initialize context */

	if ((err = ocb_init( &ocb, /* the context */
		find_cipher("rijndael"), /* cipher we want to use */
		key,16,
		nonce)
		) != CRYPT_OK) 
	{
		printf("Error ocb_init: %s", error_to_string(err));
		return EXIT_FAILURE;
	}

	/*
 while (1) {//ptlen > (unsigned long)ocb.block_len
        if ((err = ocb_encrypt(&ocb, pt, ct)) != CRYPT_OK) {
//           goto LBL_ERR;
			printf("error");
        }
        ptlen =ptlen- ocb.block_len;
		if(ptlen>0){
			pt      += ocb.block_len;
			ct      += ocb.block_len;
		}
		else break;
   }
*/
ct=ct1;
pt=pt2;
//decrypt the context

	if ((err = ocb_decrypt( &ocb, /* ocb context */
		ct, /* claintext (source) */
		pt /* ciphertext (destination) */
		) != CRYPT_OK))
	{
		printf("Error ocb_decrypt: %s", error_to_string(err));
		return EXIT_FAILURE;
	}
	pt=pt+ocb.block_len;
	ct=ct+ocb.block_len;
	if ((err = ocb_decrypt( &ocb, /* ocb context */
		ct, /* claintext (source) */
		pt /* ciphertext (destination) */
		) != CRYPT_OK))
	{
		printf("Error ocb_decrypt: %s", error_to_string(err));
		return EXIT_FAILURE;
	}
	for(i=0;i<32;i++)
	{
		printf("%x,",pt2[i]);
	}
	printf("\n\n");
	ct=ct1;
	pt=pt1;

return 1;
}

⌨️ 快捷键说明

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