varch/doc/encrypt.en.md

4.3 KiB

Introduction

During data transmission, some sensitive data, such as usernames and passwords, need to be encrypted to ensure security.

Encryption and decryption algorithms are generally classified into symmetric encryption algorithms and asymmetric encryption algorithms. This module provides several basic encryption and decryption algorithms:

  • DES encryption and decryption
  • DES3 encryption and decryption
  • AES encryption and decryption
  • SHA1 encryption
  • MD5 encryption
  • HMAC calculation
  • blowfish encryption and decryption
  • RSA encryption and decryption, public key, signature, conversion

Interface

DES

int des_set_key(const uint8_t key[8]);
int des_crypt_ecb(const uint8_t input[8], uint8_t output[8], uint8_t mode);
int des_crypt_cbc(const uint8_t *input, uint8_t *output, uint32_t length, uint8_t mode);

The DES algorithm commonly has ECB (Electronic Codebook) and CBC (Cipher Block Chaining) modes. In the ECB mode, each encryption block is calculated independently, while in the CBC mode, the calculation depends on the previous encryption block.

Before performing encryption or decryption, it is necessary to set the key first using the des_set_key function. The key has a default length of 8 bytes.

Both encryption and decryption algorithms use the des_crypt_ecb function, and whether it is encryption or decryption is set through the mode parameter.

The des_crypt_cbc function is different from des_crypt_ecb. The lengths of input and output need to be the same and a multiple of 8, which is passed in through the length parameter.

Example:

void test_des(void)
{
	uint8_t key[8] = "hello";
	uint8_t data_block[8] = {1, 2, 3, 4, 5, 6, 7, 9};
	uint8_t processed_block[8];
	int i;

	des_set_key(key);

	printf("des ecb encrypt: ");
	des_crypt_ecb(data_block, processed_block, DES_ENCRYPT);
	for (i = 0; i < 8; i++)
	{
		printf("%d ", processed_block[i]);
	}
	printf("\r\n");

	printf("des ecb decrypt: ");
	des_crypt_ecb(processed_block, data_block, DES_DECRYPT);
	for (i = 0; i < 8; i++)
	{
		printf("%d ", data_block[i]);
	}
	printf("\r\n");

	///////////////////////////////////////
	printf("des cbc encrypt: ");
	des_crypt_cbc(data_block, processed_block, 8, DES_ENCRYPT);
	for (i = 0; i < 8; i++)
	{
		printf("%d ", processed_block[i]);
	}
	printf("\r\n");

	printf("des cbc decrypt: ");
	des_crypt_cbc(processed_block, data_block, 8, DES_DECRYPT);
	for (i = 0; i < 8; i++)
	{
		printf("%d ", data_block[i]);
	}
	printf("\r\n");
}

Result:

des ecb encrypt: 156 151 171 0 235 148 83 44 
des ecb decrypt: 1 2 3 4 5 6 7 9 
des cbc encrypt: 156 151 171 0 235 148 83 44 
des cbc decrypt: 1 2 3 4 5 6 7 9

DES3

int des3_set_key2(const uint8_t key[16]);
int des3_set_key3(const uint8_t key[24]);
int des3_crypt_ecb(const uint8_t input[8], uint8_t output[8], uint8_t mode);
int des3_crypt_cbc(const uint8_t *input, uint8_t *output, uint32_t length, uint8_t mode);

In terms of usage, DES3 is consistent with DES. It performs triple encryption on DES, and the length of the key also increases accordingly. Keys of 16 bytes and 24 bytes can be set. The usage of other encryption and decryption modes such as ECB and CBC is the same as that of DES.

Example:

void test_des3(void)
{
	uint8_t key[24] = "hello world";
	uint8_t data_block[8] = {1, 2, 3, 4, 5, 6, 7, 9};
	uint8_t processed_block[8];
	int i;

	des3_set_key2(key);

	printf("des3 ecb encrypt: ");
	des3_crypt_ecb(data_block, processed_block, DES_ENCRYPT);
	for (i = 0; i < 8; i++)
	{
		printf("%d ", processed_block[i]);
	}
	printf("\r\n");

	printf("des3 ecb decrypt: ");
	des3_crypt_ecb(processed_block, data_block, DES_DECRYPT);
	for (i = 0; i < 8; i++)
	{
		printf("%d ", data_block[i]);
	}
	printf("\r\n");

	///////////////////////////////////////
	printf("des3 cbc encrypt: ");
	des3_crypt_cbc(data_block, processed_block, 8, DES_ENCRYPT);
	for (i = 0; i < 8; i++)
	{
		printf("%d ", processed_block[i]);
	}
	printf("\r\n");

	printf("des3 cbc decrypt: ");
	des3_crypt_cbc(processed_block, data_block, 8, DES_DECRYPT);
	for (i = 0; i < 8; i++)
	{
		printf("%d ", data_block[i]);
	}
	printf("\r\n");
}

Result:

des3 ecb encrypt: 48 251 201 178 251 3 6 54 
des3 ecb decrypt: 1 2 3 4 5 6 7 9 
des3 cbc encrypt: 48 251 201 178 251 3 6 54 
des3 cbc decrypt: 1 2 3 4 5 6 7 9