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

OpenSSL测试-SM4

时间:2024-04-29 09:24:28浏览次数:18  
标签:ciphertext plaintext SM4 OpenSSL len unsigned char 测试 EVP

0. 在openEuler(推荐)或Ubuntu或Windows(不推荐)中完成下面任务
1. 使用OpenSSL的命令对你的8位学号(数字)进行加密解密,密钥的前8个字节为你的8位学号,提交过程截图(5')

 

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

 

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

void handleErrors(void) {
ERR_print_errors_fp(stderr);
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;

if(!(ctx = EVP_CIPHER_CTX_new())) handleErrors();

if(1 != EVP_EncryptInit_ex(ctx, EVP_sm4_cbc(), NULL, key, iv))
handleErrors();

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

if(1 != EVP_EncryptFinal_ex(ctx, ciphertext + len, &len)) handleErrors();
ciphertext_len += len;

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;

if(!(ctx = EVP_CIPHER_CTX_new())) handleErrors();

if(1 != EVP_DecryptInit_ex(ctx, EVP_sm4_cbc(), NULL, key, iv))
handleErrors();

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

if(1 != EVP_DecryptFinal_ex(ctx, plaintext + len, &len)) handleErrors();
plaintext_len += len;

EVP_CIPHER_CTX_free(ctx);

return plaintext_len;
}

int main (void) {
unsigned char *key = (unsigned char *)"20211106";
unsigned char *iv = (unsigned char *)"0000000000000000";

unsigned char *plaintext = (unsigned char *)"20211106";

unsigned char ciphertext[128];
unsigned char decryptedtext[128];

int decryptedtext_len, ciphertext_len;

ciphertext_len = encrypt(plaintext, strlen ((char *)plaintext), key, iv, ciphertext);

decryptedtext_len = decrypt(ciphertext, ciphertext_len, key, iv, decryptedtext);

decryptedtext[decryptedtext_len] = '\0';

printf("Ciphertext is:\n");
BIO_dump_fp (stdout, (const char *)ciphertext, ciphertext_len);

printf("Decrypted text is:\n");
printf("%s\n", decryptedtext);

return 0;
}

3. 使用龙脉智能钥匙完成 2的内容,并与OpenSSL的结果进行对比,提交代码和运行结果截图。((10’))

 

标签:ciphertext,plaintext,SM4,OpenSSL,len,unsigned,char,测试,EVP
From: https://www.cnblogs.com/sjd-sr/p/18164957

相关文章

  • SM4测试
    SM4代码如下: ``#include<openssl/evp.h>include<stdio.h>include<string.h>intmain(){EVP_CIPHER_CTX*ctx;intlen;intciphertext_len;unsignedchar*plaintext=(unsignedchar*)"20211122";unsignedcharkey[]="20211......
  • 自动化测试框架-数据读取
     1.准备数据:数据格式为{json},放在json文件内,例如这样:   2.工具类:读取.json文件,保存在Map<testCaseName,JSONObject>内publicstaticMap<String,JSONObject>jsonToSearchFilter(StringfileName)throwsIOException{Map<String,JSONObject>result=ne......
  • 软件测试面试题(四)
    1、在项目中如何保证软件质量?项目质量不仅仅是某个人或某个团队来保障的,而是整个团队一起努力的结果,因此,在公司级别需要有一个规范的项目流程。产品,保证迭代过程中的产品逻辑,对于可能的兼容,升级做出预判并给出方案架构设计,满足产品表达的同时,保证设计的延续性开发,产品细......
  • 实验三—软件测试
    一、实验题目:软件测试二、实验目的1、熟悉开发环境下的自动化测试工具;1、利用自动化测试工具进行自动化单元测试。三、实验内容1、选择开发环境,IDEA或PYCHARM任选其一;2、基于所选择的开发环境实现对输入的n个整数进行排序的代码;3、对所编写代码设计测试用例;4、基于所选择......
  • 接口测试学习111
    1、同步接口:2、异步接口:不需要等接口的调用结果也可以继续执行。轮询方式。3、回调接口:一、代理类型1、协议:http、https。代理fiddler2、协议:TCP协议簇,代理:socks43、协议:TCP、UDP协议簇,代理:socks5二、接口测试范围/类型1、接口功能测2、接口性能和安全测试3、接口兼容性......
  • 单元测试工具junit的安装配置和使用
    本次实验针对Junit单元测试工具展开,在老师给的文档里面采用的是eclipse继承Junit单元测试工具,但过程比较麻烦并且后续单元测试累的编写也不方便,个人认为idea的使用便捷程度和舒适度较高,并且配置Junit单元测试工具的过程非常简单:Junit4安装与使用(IDEA)_junit4下载教程-CSDN博客本......
  • httprunner 4.x学习 - 12 测试用例引用另一个测试用例
    前言把登录用例变 export导出变量,变成全局变量,这样其它用例可以引用登录用例config:name:登录variables:username:"admin1"password:"123456"teststeps:-name:postrequest:method:POSTurl:${ENV(base_url)}/loginjson:......
  • 实验三-软件测试
    目录一、实验题目:软件测试二、实验目的三、实验内容四、实验要求五、设计单元测试用例六、单元测试结果截图七、实验中遇到的问题及解决方法八、参考代码链接一、实验题目:软件测试二、实验目的1、熟悉开发环境下的自动化测试工具;2、利用自动化测试工具进行自动化单元测试。......
  • 实验三——软件测试
    一、实验题目:软件测试二、实验目的1、熟悉开发环境下的自动化测试工具;1、利用自动化测试工具进行自动化单元测试。三、实验内容1、选择开发环境,IDEA或PYCHARM任选其一;2、基于所选择的开发环境实现对输入的n个整数进行排序的代码;3、对所编写代码设计测试用例;4、基于所选择......
  • idea插件之apifox自动化测试
    /***测试apifox插件自动化读取*/@RestController@RequestMapping("/api")publicclassSwaggerApiFoxController{/***这是一个测试方法*@return*/@RequestMapping("/test")publicStringtest(){return"test";}}1......