首页 > 其他分享 >实验二测试结果——openssl API使用

实验二测试结果——openssl API使用

时间:2023-10-20 18:46:30浏览次数:34  
标签:ciphertext plaintext openssl len unsigned char API 测试 EVP

sm3加密代码及测试截图

sm3代码:用sm3加密“20211115fanyiqing”生成摘要值作为输出。

 1 #include <stdio.h>
 2 #include <string.h>
 3 #include "openssl/evp.h"
 4 #include "err.h"
 5 
 6 void tDigest(){
 7         unsigned char md_value[EVP_MAX_MD_SIZE];
 8         int md_len, i;
 9         EVP_MD_CTX *mdctx;
10         char msg1[] = "20211115";
11         char msg2[] = "fanyiqing";
12         mdctx = EVP_MD_CTX_new();
13         EVP_MD_CTX_init(mdctx);
14 
15         EVP_DigestInit_ex(mdctx, EVP_sm3(), NULL);
16         EVP_DigestUpdate(mdctx, msg1, strlen(msg1));
17         EVP_DigestUpdate(mdctx, msg2, strlen(msg2));
18         EVP_DigestFinal_ex(mdctx, md_value, &md_len);
19         EVP_MD_CTX_free(mdctx);
20 
21         printf("%s%s的SM3摘要值:\n", msg1, msg2);
22         for(i = 0; i < md_len; i++){
23                 printf("%02x", md_value[i]);
24         }
25         printf("\n");
26 }
27 
28 int main(){
29         OpenSSL_add_all_algorithms();
30         tDigest();
31         return 0;
32 }

用此代码对不同明文加密,重复测试了3次,测试截图如下:

测试1-1:明文为20211115fanyiqing

测试1-2:明文为20211115fan

 

 

测试1-3:明文为20211115asdfgh

 

sm4加密代码及测试截图

sm4代码:用sm4加密“20211115fanyiqing”生成摘要值作为输出。

 1 #include <stdio.h>
 2 #include <stdlib.h>
 3 #include <string.h>
 4 #include <openssl/evp.h>
 5 
 6 void handleErrors()
 7 {
 8     printf("An error occurred\n");
 9     abort();
10 }
11 
12 int encrypt(unsigned char *plaintext, int plaintext_len, unsigned char *key,
13   unsigned char *iv, unsigned char *ciphertext)
14 {
15     EVP_CIPHER_CTX *ctx;
16     int len;
17     int ciphertext_len;
18 
19     /* Create and initialize the context */
20     if(!(ctx = EVP_CIPHER_CTX_new())) handleErrors();
21     if(1 != EVP_EncryptInit_ex(ctx, EVP_sm4_cbc(), NULL, key, iv)) handleErrors();
22 
23     /* Encrypt the plaintext */
24     if(1 != EVP_EncryptUpdate(ctx, ciphertext, &len, plaintext, plaintext_len))
25         handleErrors();
26     ciphertext_len = len;
27 
28     /* Finalize the encryption */
29     if(1 != EVP_EncryptFinal_ex(ctx, ciphertext + len, &len)) handleErrors();
30     ciphertext_len += len;
31 
32     /* Clean up the context */
33     EVP_CIPHER_CTX_free(ctx);
34 
35     return ciphertext_len;
36 }
37 
38 int decrypt(unsigned char *ciphertext, int ciphertext_len, unsigned char *key,
39   unsigned char *iv, unsigned char *plaintext)
40 {
41     EVP_CIPHER_CTX *ctx;
42     int len;
43     int plaintext_len;
44 
45     /* Create and initialize the context */
46     if(!(ctx = EVP_CIPHER_CTX_new())) handleErrors();
47     if(1 != EVP_DecryptInit_ex(ctx, EVP_sm4_cbc(), NULL, key, iv)) handleErrors();
48 
49     /* Decrypt the ciphertext */
50     if(1 != EVP_DecryptUpdate(ctx, plaintext, &len, ciphertext, ciphertext_len))
51         handleErrors();
52     plaintext_len = len;
53 
54     /* Finalize the decryption */
55     if(1 != EVP_DecryptFinal_ex(ctx, plaintext + len, &len)) handleErrors();
56     plaintext_len += len;
57 
58     /* Clean up the context */
59     EVP_CIPHER_CTX_free(ctx);
60 
61     return plaintext_len;
62 }
63 
64 int main(int arc, char *argv[]) {
65     unsigned char *key = (unsigned char *)"0123456789qwerty";
66     unsigned char *iv = (unsigned char *)"0123456789qwerty";
67     unsigned char *plaintext = (unsigned char *)"20211115fanyiqing";
68     int plaintext_len = strlen((char *)plaintext);
69     unsigned char ciphertext[128];
70     unsigned char decryptedtext[128];
71     int decryptedtext_len, ciphertext_len;
72 
73     OpenSSL_add_all_algorithms();
74     ERR_load_CRYPTO_strings;
75 
76     ciphertext_len = encrypt(plaintext, plaintext_len, key, iv, ciphertext);
77     printf("Ciphertext is:\n");
78     BIO_dump_fp(stdout, (const char *)ciphertext, ciphertext_len);
79 
80     decryptedtext_len = decrypt(ciphertext, ciphertext_len, key, iv, decryptedtext);
81     decryptedtext[decryptedtext_len] = '\0';
82     printf("Decrypted text is:\n");
83     printf("%s\n", decryptedtext);
84 
85     return 0;
86 }

用此代码对不同明文加密,重复测试了3次,测试截图如下:

测试2-1:明文为20211115fanyiqing

 测试2-2:明文为20211115

测试2-3:明文为20211115asdfgh

标签:ciphertext,plaintext,openssl,len,unsigned,char,API,测试,EVP
From: https://www.cnblogs.com/20211115fyq/p/17777784.html

相关文章

  • web基础漏洞-xss手工测试与自动测试
    手工测试,一般是指结合浏览器和burp的重放进行。1、反射型xss手工测试1.1测试是否返回如果测试参数在响应的体部中并未返回,那么基本可以判断不存在反射型xss。问题1:测试参数在响应中固有如果测试参数除了包含返回之外,还存在固有。这样的话,直接根据测试参数是否在响应体部中......
  • RunnerGo 支持UI自动化的测试平台
    RunnerGo提供从API管理到API性能再到可视化的API自动化、UI自动化测试功能模块,覆盖了整个产品测试周期。RunnerGoUI自动化基于Selenium浏览器自动化方案构建,内嵌高度可复用的测试脚本,测试团队无需复杂的代码编写即可开展低代码的自动化测试。 以一条简单的搜索场景为例,本文......
  • 5、单元测试、反射、注解、动态代理
    单元测试、反射、注解、动态代理一、单元测试1.1Junit单元测试所谓单元测试,就是针对最小的功能单元,编写测试代码对其进行正确性测试。我们想想,咱们之前是怎么进行测试的呢?比如说我们写了一个学生管理系统,有添加学生、修改学生、删除学生、查询学生等这些功能。要对这些功能......
  • Mock 测试快速上手 :什么是 Mock 测试?
    Mock测试什么是Mock?Mock的意思就是,当你很难拿到源数据时,你可以使用某些手段,去获取到跟源数据相似的假数据,拿着这些假数据,前端可以先行开发,而不需要等待后端给了数据后再开发。Mock的重要性在现在前后端分离的时代,前后端如何效率更高地去完成协同开发,是一个大问题。举个例子:后......
  • 软件测试|网安学院举办第二届“火焰杯”软件测试高校就业选拔赛颁奖典礼
    软件测试就业联盟主办的第二届“火焰杯”软件测试高校就业选拔赛颁奖典礼于11月25日下午在9A206报告厅举行,庄严隆重。网络空间安全学院首次参与了这一赛事,派出了6位同学参加,他们都成功入围了决赛。其中,19级软件工程2班的梁垧同学获得了决赛的三等奖,奖金为2000元,而其......
  • 软件测试|计算机科学与工程学院举行火焰杯颁奖仪式
    11月25日下午,在S楼会议室举行了"火焰杯"软件测试开发选拔赛和三江-慧科卓越工程师班暑期编程能力训练营的颁奖仪式。仪式的重要参与者包括计算机科学与工程学院的领导,刘亚军院长、叶传标副院长、曹阳副院长、吴德副院长、以及院党总支的副书记王兰英、系主任杨少雄,慧科企业代......
  • Linux 实现OpenSSL 服务器端客户端通信
    1.OpenSSL安装详情参考博文:https://blog.csdn.net/qq_39521181/article/details/964576732.SSL在学习openssl编程之前,先了解一下什么是SSL,有助于后续的学习。SSL是一个缩写,代表的是SecureSocketsLayer。它是支持在Internet上进行安全通信的标准,并且将数据密码术集成到了......
  • openssl基础使用(密码学 linux)
    目录       实验原理       实验过程           一、对称加密               1、使用rc4加解密               2、使用AES加解密               3、使用3DES加解密           二、非对......
  • 云图说|初识API中心APIHub
    阅识风云是华为云信息大咖,擅长将复杂信息多元化呈现,其出品的一张图(云图说)、深入浅出的博文(云小课)或短视频(云视厅)总有一款能让您快速上手华为云。更多精彩内容请单击此处。摘要:API中心是为API开发者和应用开发者构建的海量API的汇聚运营平台。通过统一汇聚和持续运营,贯通......
  • Eplan API 初始化
    Eplan支持的开发方式一共有3种脚本dll文件形式exe离线程式形式虽然eplan二次开发也支持vb语言,但这里只讨论c#脚本(script)Eplan脚本支持的功能有限,有限的原因在于其支持的程序集有限c#中的System;System.XML;System.Drawing;System.Windows.FormsEpalnAPI中的Names......