实验二 OpenSSL API使用

发布时间 2023-10-19 16:08:23作者: 梁小轩

SM3测试代码

#include <stdio.h>
#include <string.h>
#include "openssl/evp.h"
#include "err.h"

void tDigest(){
        unsigned char md_value[EVP_MAX_MD_SIZE];
        int md_len, i;
        EVP_MD_CTX *mdctx;
        char msg1[] = "20211113";
        char msg2[] = "kcf";
        mdctx = EVP_MD_CTX_new();
        EVP_MD_CTX_init(mdctx);

        EVP_DigestInit_ex(mdctx, EVP_sm3(), NULL);
        EVP_DigestUpdate(mdctx, msg1, strlen(msg1));
        EVP_DigestUpdate(mdctx, msg2, strlen(msg2));
        EVP_DigestFinal_ex(mdctx, md_value, &md_len);
        EVP_MD_CTX_free(mdctx);

        printf("%s%s的SM3摘要值:\n", msg1, msg2);
        for(i = 0; i < md_len; i++){
                printf("%02x", md_value[i]);
        }
        printf("\n");
}

int main(){
        OpenSSL_add_all_algorithms();
        tDigest();
        return 0;
}

SM4测试代码

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <openssl/evp.h>

void handleErrors()
{
    printf("An error occurred\n");
    abort();
}

int encrypt(unsigned char *plaintext, int plaintext_len, unsigned char *key,
  unsigned char *iv, unsigned char *ciphertext)
{
    EVP_CIPHER_CTX *ctx;
    int len;
    int ciphertext_len;

    /* Create and initialize the context */
    if(!(ctx = EVP_CIPHER_CTX_new())) handleErrors();
    if(1 != EVP_EncryptInit_ex(ctx, EVP_sm4_cbc(), NULL, key, iv)) handleErrors();

    /* Encrypt the plaintext */
    if(1 != EVP_EncryptUpdate(ctx, ciphertext, &len, plaintext, plaintext_len))
        handleErrors();
    ciphertext_len = len;

    /* Finalize the encryption */
    if(1 != EVP_EncryptFinal_ex(ctx, ciphertext + len, &len)) handleErrors();
    ciphertext_len += len;

    /* Clean up the context */
    EVP_CIPHER_CTX_free(ctx);

    return ciphertext_len;
}

int decrypt(unsigned char *ciphertext, int ciphertext_len, unsigned char *key,
  unsigned char *iv, unsigned char *plaintext)
{
    EVP_CIPHER_CTX *ctx;
    int len;
    int plaintext_len;

    /* Create and initialize the context */
    if(!(ctx = EVP_CIPHER_CTX_new())) handleErrors();
    if(1 != EVP_DecryptInit_ex(ctx, EVP_sm4_cbc(), NULL, key, iv)) handleErrors();

    /* Decrypt the ciphertext */
    if(1 != EVP_DecryptUpdate(ctx, plaintext, &len, ciphertext, ciphertext_len))
        handleErrors();
    plaintext_len = len;

    /* Finalize the decryption */
    if(1 != EVP_DecryptFinal_ex(ctx, plaintext + len, &len)) handleErrors();
    plaintext_len += len;

    /* Clean up the context */
    EVP_CIPHER_CTX_free(ctx);

    return plaintext_len;
}

int main(int arc, char *argv[]) {
    unsigned char *key = (unsigned char *)"0123456789abcdef";
    unsigned char *iv = (unsigned char *)"0123456789abcdef";
    unsigned char *plaintext = (unsigned char *)"Hello World!";
    int plaintext_len = strlen((char *)plaintext);
    unsigned char ciphertext[128];
    unsigned char decryptedtext[128];
    int decryptedtext_len, ciphertext_len;

    OpenSSL_add_all_algorithms();
    ERR_load_crypto_strings();

    ciphertext_len = encrypt(plaintext, plaintext_len, key, iv, ciphertext);
    printf("Ciphertext is:\n");
    BIO_dump_fp(stdout, (const char *)ciphertext, ciphertext_len);

    decryptedtext_len = decrypt(ciphertext, ciphertext_len, key, iv, decryptedtext);
    decryptedtext[decryptedtext_len] = '\0';
    printf("Decrypted text is:\n");
    printf("%s\n", decryptedtext);

    return 0;
}


AES-256-CBC测试代码

#include <stdio.h>
#include <string.h>
#include <openssl/evp.h>

int main() {
    OpenSSL_add_all_algorithms();

    // 加密
    EVP_CIPHER_CTX *ctx = EVP_CIPHER_CTX_new();
    EVP_CipherInit_ex(ctx, EVP_aes_256_cbc(), NULL, NULL, NULL, 1);
    
    // 设置加密密钥和IV(初始化向量)
    unsigned char key[32] = "01234567890123456789012345678901";
    unsigned char iv[16] = "0123456789012345";
    EVP_CipherInit_ex(ctx, NULL, NULL, key, iv, 1);
    
    // 待加密的数据
    unsigned char plaintext[] = "Hello, OpenSSL!";
    unsigned char ciphertext[128];
    
    int len;
    EVP_CipherUpdate(ctx, ciphertext, &len, plaintext, strlen((char *)plaintext));
    int ciphertext_len = len;
    
    EVP_CipherFinal_ex(ctx, ciphertext + len, &len);
    ciphertext_len += len;
    
    printf("Ciphertext: ");
    for (int i = 0; i < ciphertext_len; i++) {
        printf("%02x", ciphertext[i]);
    }
    printf("\n");
    
    EVP_CIPHER_CTX_free(ctx);
    
    // 解密
    ctx = EVP_CIPHER_CTX_new();
    EVP_CipherInit_ex(ctx, EVP_aes_256_cbc(), NULL, NULL, NULL, 0);
    EVP_CipherInit_ex(ctx, NULL, NULL, key, iv, 0);
    
    unsigned char decryptedtext[128];
    
    EVP_CipherUpdate(ctx, decryptedtext, &len, ciphertext, ciphertext_len);
    int decryptedtext_len = len;
    
    EVP_CipherFinal_ex(ctx, decryptedtext + len, &len);
    decryptedtext_len += len;
    
    printf("Decrypted Text: %s\n", decryptedtext);
    
    EVP_CIPHER_CTX_free(ctx);

    return 0;
}