OpenSSL测试-SM4

任务详情:0. 在openEuler(推荐)或Ubuntu或Windows(不推荐)中完成下面任务,参考别人代码要给出链接,使用AI工具要给出过程,否则成绩归零。1. 使用OpenSSL的命令对你的8位学号(字符串)进行加密解密,密钥的前8个字节为你的8位学号,提交过程截图(5')2. 使用OpenSSL编程对对"你的8位学号(数字)"进行加密解密,提交代码和运行结果截图。(10’)3. 使用龙脉智能钥匙完成 2的内容,并与OpenSSL的结果进行对比,提交代码和运行结果截图。((10’))

AI工具和链接

image
image

1. 使用OpenSSL的命令对你的8位学号(字符串)进行加密解密,密钥的前8个字节为你的8位学号,提交过程截图(5')

image

2. 使用OpenSSL编程对对"你的8位学号(数字)"进行加密解密,提交代码和运行结果截图。(10’)

代码

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

// 定义密钥长度为16字节(128位)
#define KEY_LENGTH 16

// 加密函数
int sm4_encrypt(unsigned char *plaintext, int plaintext_len, unsigned char *key, unsigned char *ciphertext) {
    EVP_CIPHER_CTX *ctx;

    int len;
    int ciphertext_len;

    // 创建并初始化一个上下文对象
    if(!(ctx = EVP_CIPHER_CTX_new())) {
        return -1;
    }

    // 初始化加密操作,设置SM4算法和密钥
    if(1 != EVP_EncryptInit_ex(ctx, EVP_sm4_ecb(), NULL, key, NULL)) {
        return -1;
    }

    // 执行加密操作
    if(1 != EVP_EncryptUpdate(ctx, ciphertext, &len, plaintext, plaintext_len)) {
        return -1;
    }
    ciphertext_len = len;

    // 结束加密操作
    if(1 != EVP_EncryptFinal_ex(ctx, ciphertext + len, &len)) {
        return -1;
    }
    ciphertext_len += len;

    // 释放上下文对象
    EVP_CIPHER_CTX_free(ctx);

    return ciphertext_len;
}

// 解密函数
int sm4_decrypt(unsigned char *ciphertext, int ciphertext_len, unsigned char *key,  unsigned char *plaintext) {
    EVP_CIPHER_CTX *ctx;

    int len;
    int plaintext_len;

    // 创建并初始化一个上下文对象
    if(!(ctx = EVP_CIPHER_CTX_new())) {
        return -1;
    }

    // 初始化解密操作,设置SM4算法和密钥
    if(1 != EVP_DecryptInit_ex(ctx, EVP_sm4_ecb(), NULL, key, NULL)) {
        return -1;
    }

    // 执行解密操作
    if(1 != EVP_DecryptUpdate(ctx, plaintext, &len, ciphertext, ciphertext_len)) {
        return -1;
    }
    plaintext_len = len;

    // 结束解密操作
    if(1 != EVP_DecryptFinal_ex(ctx, plaintext + len, &len)) {
        return -1;
    }
    plaintext_len += len;

    // 释放上下文对象
    EVP_CIPHER_CTX_free(ctx);

    return plaintext_len;
}

int main() {
    // 设置密钥
    unsigned char key[KEY_LENGTH] = "e348ca9e5d98ce4d91b6d4f532207586"; // 

    // 原始字符串
    unsigned char plaintext[] = "20211318";

    // 计算原始字符串长度
    int plaintext_len = strlen((char *)plaintext);

    // 分配内存空间存放加密后的数据
    unsigned char *ciphertext = malloc(plaintext_len + EVP_MAX_BLOCK_LENGTH);
    if (ciphertext == NULL) {
        fprintf(stderr, "malloc() failed\n");
        return -1;
    }

    // 加密
    int ciphertext_len = sm4_encrypt(plaintext, plaintext_len, key, ciphertext);
    if (ciphertext_len == -1) {
        fprintf(stderr, "Encryption failed\n");
        return -1;
    }

    // 打印加密结果
    printf("加密后的数据:");
    for (int i = 0; i < ciphertext_len; i++) {
        printf("%02x", ciphertext[i]);
    }
    printf("\n");

    // 分配内存空间存放解密后的数据
    unsigned char *decryptedtext = malloc(ciphertext_len + EVP_MAX_BLOCK_LENGTH);
    if (decryptedtext == NULL) {
        fprintf(stderr, "malloc() failed\n");
        return -1;
    }

    // 解密
    int decryptedtext_len = sm4_decrypt(ciphertext, ciphertext_len, key, decryptedtext);
    if (decryptedtext_len == -1) {
        fprintf(stderr, "Decryption failed\n");
        return -1;
    }

    // 打印解密结果
    printf("解密后的数据:%s\n", decryptedtext);
    // 释放内存空间
    free(ciphertext);
    free(decryptedtext);
    return 0;
}

运行截图:
image

posted @ 2024-05-13 14:11  ahuahauhau  阅读(490)  评论(0)    收藏  举报