首页 > 其他分享 >OpenSSL测试-SM4

OpenSSL测试-SM4

时间:2023-04-12 11:11:10浏览次数:39  
标签:enc 测试 SM4 OpenSSL exit file printf EVP buf

任务详情

使用OpenSSL编程对内容为"所有同学的8位学号(数字)"的文件进行加密解密,密钥要包含你的8位学号,提交代码和运行结果截图。(选做(10’))

  • 编译:gcc -g sm4txtcode.c -o sm4_en_de_txt -L/usr/lib -lssl -lcrypto
    就像这样

  • 运行:enout.txt是加密后的输出、deout.txt是解密后的输出、in.txt是输入、里面写的有学号

  • 关于密钥:你不要急,密钥内嵌在代码里面,虽然不安全,但是这是测试。

  • 操作环境ubuntu最新版(2023)

  • 这里是源代码

点击查看代码
#include <openssl/evp.h>
#include <openssl/err.h>
#include <string.h>

#define BUF_SIZE 1024

int main(int argc, char *argv[]) {
    // 检查命令行参数
    if (argc != 4) {
        printf("Usage: %s <input file> <encrypted output file> <decrypted output file>\n", argv[0]);
        exit(EXIT_FAILURE);
    }

    // 初始化 OpenSSL
    OpenSSL_add_all_algorithms();
    ERR_load_crypto_strings();

    // 设置加密上下文
    EVP_CIPHER_CTX *ctx = EVP_CIPHER_CTX_new();
    if (!ctx) {
        printf("Error creating context\n");
        exit(EXIT_FAILURE);
    }
    const EVP_CIPHER *cipher = EVP_sm4_ecb();
    unsigned char key[EVP_MAX_KEY_LENGTH] = {0};
    unsigned char iv[EVP_MAX_IV_LENGTH] = {0};
    char *student_id = "20201326"; // 用你自己的8位学号替换
    printf("学号%s\n",student_id);
    strncpy(key, student_id, 8);
    if (EVP_EncryptInit_ex(ctx, cipher, NULL, key, iv) != 1) {
        printf("Error setting up encryption context\n");
        exit(EXIT_FAILURE);
    }

    // 打开输入文件
    FILE *in_file = fopen(argv[1], "rb");
    if (!in_file) {
        printf("Error opening input file\n");
        exit(EXIT_FAILURE);
    }

    // 打开加密输出文件
    FILE *enc_file = fopen(argv[2], "wb");
    if (!enc_file) {
        printf("Error opening encrypted output file\n");
        exit(EXIT_FAILURE);
    }

    // 加密输入文件并写入加密输出文件
    unsigned char in_buf[BUF_SIZE] = {0};
    unsigned char enc_buf[BUF_SIZE + EVP_MAX_BLOCK_LENGTH] = {0};
    int num_bytes_read, enc_len;
    while ((num_bytes_read = fread(in_buf, sizeof(unsigned char), BUF_SIZE, in_file)) > 0) {
        if (EVP_EncryptUpdate(ctx, enc_buf, &enc_len, in_buf, num_bytes_read) != 1) {
            printf("Error encrypting data\n");
            exit(EXIT_FAILURE);
        }
        fwrite(enc_buf, sizeof(unsigned char), enc_len, enc_file);
    }
    if (num_bytes_read < 0) {
        printf("Error reading input file\n");
        exit(EXIT_FAILURE);
    }

    // 完成加密
    if (EVP_EncryptFinal_ex(ctx, enc_buf, &enc_len) != 1) {
        printf("Error finalizing encryption\n");
        exit(EXIT_FAILURE);
    }
    fwrite(enc_buf, sizeof(unsigned char), enc_len, enc_file);

    // 清理加密上下文
    EVP_CIPHER_CTX_free(ctx);

    // 关闭输入和输出文件
    fclose(in_file);
    fclose(enc_file);

    // 设置解密上下文
    ctx = EVP_CIPHER_CTX_new();
    if (!ctx) {
        printf("Error creating context\n");
        exit(EXIT_FAILURE);
    }
    if (EVP_DecryptInit_ex(ctx, cipher, NULL, key, iv) != 1) {
        printf("Error setting up decryption context\n");
        exit(EXIT_FAILURE);
    }

    // 打开加密输入文件
    enc_file = fopen(argv[2], "rb");
    if (!enc_file) {
        printf("Error opening encrypted input file\n");
        exit(EXIT_FAILURE);
    }

    // 打开解密输出文件
    FILE *dec_file = fopen(argv[3], "wb");
    if (!dec_file) {
        printf("Error opening decrypted output file\n");
        exit(EXIT_FAILURE);
    }

    // 解密加密输入文件并写入解密输出文件
    unsigned char dec_buf[BUF_SIZE + EVP_MAX_BLOCK_LENGTH] = {0};
    int dec_len;
    while ((num_bytes_read = fread(enc_buf, sizeof(unsigned char), BUF_SIZE + EVP_MAX_BLOCK_LENGTH, enc_file)) > 0) {
        if (EVP_DecryptUpdate(ctx, dec_buf, &dec_len, enc_buf, num_bytes_read) != 1) {
            printf("Error decrypting data\n");
            exit(EXIT_FAILURE);
        }
        fwrite(dec_buf, sizeof(unsigned char), dec_len, dec_file);
    }
    if (num_bytes_read < 0) {
        printf("Error reading encrypted input file\n");
        exit(EXIT_FAILURE);
    }

    // 完成解密
    if (EVP_DecryptFinal_ex(ctx, dec_buf, &dec_len) != 1) {
        printf("Error finalizing decryption\n");
        exit(EXIT_FAILURE);
    }
    fwrite(dec_buf, sizeof(unsigned char), dec_len, dec_file);

    // 清理解密上下文
    EVP_CIPHER_CTX_free(ctx);

    // 关闭输入和输出文件
    fclose(enc_file);
    fclose(dec_file);

    // 清理 OpenSSL
    EVP_cleanup();
    ERR_free_strings();

    return 0;
}

标签:enc,测试,SM4,OpenSSL,exit,file,printf,EVP,buf
From: https://www.cnblogs.com/JIANGJININ/p/17309113.html

相关文章

  • Vulnhub之KB Vuln 3靶机详细测试过程
    KBVuln3作者:jasonhuawen识别目标主机IP地址─(kali㉿kali)-[~/Vulnhub/KBVuln3]└─$sudonetdiscover-ieth1-r192.168.56.0/24Currentlyscanning:192.168.56.0/24|ScreenView:UniqueHosts......
  • sm4
    1.使用OpenSSL的命令对你的8位学号(数字)进行加密解密,密钥的前8个字节为你的8位学号,提交过程截图(5') 2.使用OpenSSL编程对对"你的8位学号(数字)"进行加密解密,提交代码和运行结果截图。(10’)  代码:#include<stdio.h>#include<stdlib.h>#include<string.h>#include<ope......
  • 性能测试了解服务架构
    软件架构与部署性能测试软件服务架构一般分为,最外层lvs-nginx-tomcat-redis-mysqlmermaidgraphLRlvs-->nginx主1-->tomcat1-->redis1-->mysql1nginx主1-->tomcat2-->redis1lvs-->nginx主2-->tomcat3-->redis2-->mysql2nginx主2-->tomcat4-->redis2本文永久更新地址:......
  • OpenSSL测试-SM4
    在openEuler(推荐)或Ubuntu或Windows(不推荐)中完成下面任务使用OpenSSL的命令对你的8位学号(数字)进行加密解密,密钥的前8个字节为你的8位学号,提交过程截图(5')使用OpenSSL编程对对"你的8位学号(数字)"进行加密解密,提交代码和运行结果截图。(10’)使用OpenSSL编程对内容为"所有同学......
  • OpenSSL测试-SM3
    OpenSSL测试-SM3任务详情在openEuler(推荐)或Ubuntu或Windows(不推荐)中完成下面任务使用OpenSSL的命令计算你的8位学号的摘要值(SM3),提交截图(5')使用OpenSSL编程对计算"你的8位学号"SM3摘要值,提交代码和运行结果截图。(10’)使用OpenSSL编程对计算内容为"所有同学的8位学号"的文......
  • OpenSSL测试-随机数
    任务详情在openEuler(推荐)或Ubuntu或Windows(不推荐)中完成下面任务使用OpenSSL定义一个私有函数staticintgetRandom(char*r,intlength),获取length个字节的随机数(5‘)把上述函数集成到src中的sdf.c中的SDF_GenerateRandom中(5')在test中的main.c调用SDF_GenerateRandom......
  • OpenSSL测试-大数
    任务详情在openEuler(推荐)或Ubuntu或Windows(不推荐)中完成下面任务基于OpenSSL的大数库计算2的N次方,N为你学号的后四位(5‘)基于OpenSSL的大数库计算你以及前面5位同学和后面5位同学的8位学号的乘积,N为你学号的后四位(5‘)用Python或bc命令验证计算的正确性(5’)提交代码(或代码链......
  • 使用RunnerGo做接口性能、接口自动化测试
    最近在gitee上看见一款获得GVP(最有价值开源项目)的测试平台RunnerGo,看他们官网介绍包含了接口测试、性能测试、自动化测试。知道他们有saas版可以试用,果断使用了一下,对其中场景管理和性能测试印象深刻,之后也在公司自己安装使用,接下来和大家介绍一下RunnerGo的整体使用情况。登录后的......
  • 软件测试综合面试题
    项目中会对接第三方系统吗,哪些系统在项目中对接第三方系统是非常常见的。常见的对接的第三方系统包括但不限于以下几种:1.支付系统:例如支付宝、微信支付、银联支付等。如果项目需要接入支付功能,就需要对接相应的支付系统。2.物流系统:例如顺丰、圆通、中通等。如果项目中需要使......
  • 安全测试前置实践1-白盒&黑盒扫描
    作者:京东物流 陈维一、引言G.J.Myers在《软件测试的艺术》中提出:从心理学角度来说,测试是一个为了寻找错误而运行程序的过程。那么安全测试则是一个寻找系统潜在安全问题的过程,通过测试手段发现系统中可能存在的安全问题和风险,分析并进行优化,保障系统的安全质量。从应用安全维......