Get the App
SLTechnology News&Howtos  ›  Internet Technology  › 

What is the usage of aes encryption and decryption and base64 encoding and decoding based on mbedtls-1.3.14 library?

Shulou Source: shulou.com Published: 2022-06-01 04:45:34 10月04日 Update

This article introduces aes encryption and decryption based on mbedtls-1.3.14 library and what is the usage of base64 encoding and decoding. The content is very detailed. Interested friends can use it for reference. I hope it will be helpful to you.

1.1 recent project will use aes and base64, based on the mbedtls-1.3.14 library to introduce the use of aes and base64, and write a test case 1.2 usage

Aes encryption, base64 output

Void AesEncryptAndOutBase64 (const unsigned char * InBuf,int InBufLen,const unsigned char * AesKey,unsigned char * OutBuf) {unsigned char IV [IV _ SIZE] = {0}; memcpy (iv,AesKey,IV_SIZE); aes_context aes_enc; aes_init (& aes_enc); unsigned char pt_buf [8000] = {0}; int I = 0; for (I; I)

< InBufLen; i++) { pt_buf[i] = InBuf[i]; } //不够16字节的剩余部分,填充 int index = InBufLen; int pading = 16 - (index % 16); for(i = 0; i < pading; i++) { pt_buf[index + i] = pading; } int ret = -1; ret = aes_setkey_enc(&aes_enc,AesKey,AES_KEY_SIZE); if (ret != 0) { DEBUG_INFO("set aes key failed,ret=%d",ret); return; } ret = aes_crypt_cbc(&aes_enc,AES_ENCRYPT,index+pading,iv,pt_buf,pt_buf); if (ret != 0) { DEBUG_INFO("aes cbc en failed,ret=%d",ret); return; } #if 1 printf("aes en after:"); for(i = 0; i < index + pading; i++) { printf("x",pt_buf[i]); } printf("\n"); #endif size_t n = 0; //第一个参数为NULL,表示获取密文长度 base64_encode(NULL,&n,pt_buf,index + pading); //第二次真正解码 ret = base64_encode(OutBuf,&n,pt_buf,index + pading); if (ret != 0) { DEBUG_INFO("baae64 encode failed,ret=%d",ret); return; } DEBUG_INFO("aes en,base64 encode out:%s",OutBuf);} base解码,aes解密 int DecryptBas64Aes(const unsigned char *Base64 ,const unsigned char* AesKey , unsigned char * OutBuf){ size_t n = 0; int ret = -1; unsigned char base64_de_out[8000] = {0}; //第一个参数为NULL,表示获取密文长度 base64_decode(NULL,&n,Base64,strlen(Base64)); //第二次真正解码 ret = base64_decode(base64_de_out,&n,Base64,strlen(Base64)); if (ret != 0) { DEBUG_INFO("base de failed,ret=%d",ret); return -1; } #if 1 DEBUG_INFO("base de after:"); int i = 0; for(i = 0; i < n; i++) { printf("x",base64_de_out[i]); } printf("\n"); #endif unsigned char iv[IV_SIZE] = {0}; memcpy(iv,AesKey,IV_SIZE); aes_context aes_dec; aes_init(&aes_dec); ret = aes_setkey_dec(&aes_dec,AesKey,AES_KEY_SIZE); if (ret != 0) { DEBUG_INFO("set aes key dec failed,ret=%d",ret); return -1; } //aes解密 ret = aes_crypt_cbc(&aes_dec,AES_DECRYPT,n,iv,base64_de_out,OutBuf); if (ret != 0) { DEBUG_INFO("aes en failed,ret=%d",ret); return -1; } //有时解密失败,会导致填充部分的index异常(大于整个数组长度,或者小于0),为了防止数组越界,这里作一下判断 if(OutBuf[n-1] >

N | | OutBuf [n-1]

< 0) { OutBuf[0]='\0'; return -1; } //最后一个字符填充'\0',不然用strcmp不能比较 int Num = n-OutBuf[n-1]; OutBuf[Num]='\0'; DEBUG_INFO("aes de %s",OutBuf); return Num;} 测试 void TestAes(void){ char AesKey[17] = {"1234567887654321"}; char *InBuf = "abcdef明文"; char EnOutBuf[1024] = {0}; char DeOutBuf[1024] = {0}; AesEncryptAndOutBase64(InBuf,strlen(InBuf),AesKey,EnOutBuf); DecryptBas64Aes(EnOutBuf,AesKey,DeOutBuf); DEBUG_INFO("%s",DeOutBuf);}int main(void){ TestAes(); return 0;}1.3运行结果

On the mbedtls-1.3.14 library based on aes encryption and decryption and base64 encoding and decoding usage is shared here, I hope the above content can be of some help to you, can learn more knowledge. If you think the article is good, you can share it for more people to see.

Tags: Code content parameters ciphertext more length help test good not enough interest characters bytes guys buddies articles plaintext knowledge articles results Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno Huawei OPPO Reno vpn NVidia macOS