Example #1
0
File: c_aes.c Project: aikko/crypto
// Test AES
int main(void)
{
  byte key[] = {0x12,0x34,0x56,0x12,0x34,0x56,0x12,0x34,0x56,0x12,0x34,0x56,0x12,0x34,0x56,0x12};
  byte msg[] = {0xab,0xcd,0xef,0xab,0xcd,0xef,0xab,0xcd,0xef,0xab,0xcd,0xef,0xab,0xcd,0xef,0xab};
  byte encrypted[16], decrypted[16];

  printf("Test AES\r\n\n");
  Pretty(key,16,"Key:         ");
  Pretty(msg,16,"Original:    ");

  EncryptAES(msg,key,encrypted);
  printf("Encrypted should be: 85E5A3D7356A61E29A8AFA559AD67102\r\n");
  Pretty(encrypted,16,"Encrypted:   ");

  DecryptAES(encrypted,key,decrypted);
  Pretty(decrypted,16,"Decrypted:   ");
  
  return 0;
}
Example #2
0
int TestAES()
{
	printf("This is AES test\n");
	//要加密的数据
	char encrypt_input_data[] = "hello suiui, this is a test for AES!";

	
	//加密数据的长度
	size_t encrypt_input_length = 0;

	encrypt_input_length = strlen(encrypt_input_data);

	printf("要加密的数据: \n");
	printf("%s\n", encrypt_input_data);
	
	//创建密钥
	unsigned char key[20];
	int ret = 0;
	memset(key, 0, 20 * sizeof(unsigned char));


	ret = CreateSecretKey(key, 20);
	if (ret == 0)
	{
		printf("create AES key failed \n");
		exit(0);//TODO
	}
	else
	{
		printf("Curent AES key len: %d , The AES key; %s\n", ret, (char *)key);
	}

	//分配相应的buffer 存储加密数据和解密数据 
	//需要分配的buffer大小, 
	int len = 0;
	unsigned char * outdata_e = NULL;
	unsigned char * outdata_d = NULL;

	len = sizeof(encrypt_input_data) + 32;
	//printf("encrypt input data len = %d\n", len);
	
	outdata_e = (unsigned char *)malloc(len * sizeof(unsigned char));
	if (!outdata_e)
	{	
		printf("malloc outdata_e failed\n");
		exit(0);//TODO
	}
	outdata_d = (unsigned char *)malloc(len* sizeof(unsigned char));
	if (!outdata_d)
	{
		
		printf("malloc outdata_d failed\n");
		exit(0);//TODO
	}

	memset(outdata_e, 0, len * sizeof(unsigned char));
	
	size_t encrypt_outdata_e_length = 0;

	printf("加密中.............\n");
	if (0 != EncryptAES(key, (const unsigned char *)encrypt_input_data, encrypt_input_length, outdata_e, encrypt_outdata_e_length))
	{
		printf("EncryptAES error, key:%s\n", key);
		exit(0); //TODO
	}

	size_t decrypt_outdata_d_length = 0;

	memset(outdata_d, 0, len * sizeof(unsigned char));
	printf("解密中.............\n");
	if (0 != DecryptAES(key, outdata_e, encrypt_outdata_e_length, outdata_d, decrypt_outdata_d_length))
	{
		printf("DecryptAES error, key:%s\n", key);
		exit(0); //TODO
	}

	printf("解密数据长度:\n");

	for (int i = 0; i < decrypt_outdata_d_length; i++)
	{
		printf("%c", outdata_d[i]);
	}
	printf("\n");


	free(outdata_d);
	free(outdata_e);
	return 0;
}