草稿

发布时间 2023-04-18 19:02:48作者: 20201327刘谨铭
// Include necessary headers
#include <openssl/evp.h>
#include <openssl/err.h>
#include <openssl/sm4.h>

// Define the key and IV
unsigned char key[16] = "20201327";
unsigned char iv[16] = "20201327";

// Define the plaintext and ciphertext buffers
unsigned char plaintext[] = "20201327";
unsigned char ciphertext[SM4_BLOCK_SIZE];

// Create an SM4 context
SM4_KEY sm4_key;
SM4_set_key(key, &sm4_key);

// Encrypt the plaintext using SM4
SM4_cbc_encrypt(plaintext, ciphertext, sizeof(plaintext), &sm4_key, iv, SM4_ENCRYPT);

// Decrypt the ciphertext using SM4
SM4_cbc_encrypt(ciphertext, plaintext, sizeof(ciphertext), &sm4_key, iv, SM4_DECRYPT);