首页 > 其他分享 >openssl api使用

openssl api使用

时间:2023-10-20 11:49:05浏览次数:31  
标签:const openssl len api 使用 EVP data tc out

实验要求

参考 https://blog.csdn.net/bruce135lee/article/details/81811403 调用OpenSSL API
0 推荐在openEuler中实现 ,参考https://www.cnblogs.com/rocedu/p/6012545.html第三节
1 提交相关代码码云(或github)链接
2 提交不少于6张编译测试过程截图
3 至少包含SM4,SM3的测试

代码链接(main.c)

代码: 点这里

实验代码

#include <stddef.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "openssl/err.h"
#include "openssl/evp.h"
 
/* Before OpenSSL 1.1.1-pre1, we did not have EVP_sm4_ecb() */
#if defined(OPENSSL_VERSION_NUMBER) \
    && OPENSSL_VERSION_NUMBER < 0x10101001L
static const EVP_CIPHER *(*EVP_sm4_ecb)()=EVP_aes_128_ecb;
#endif
 
typedef struct {
    const unsigned char *in_data;
    size_t in_data_len;
    int in_data_is_already_padded;
    const unsigned char *in_ivec;
    const unsigned char *in_key;
    size_t in_key_len;
} test_case_t;
 
 
void test_encrypt_with_cipher(const test_case_t *in, const EVP_CIPHER *cipher)
{
    unsigned char *out_buf = NULL;
    int out_len;
    int out_padding_len;
    EVP_CIPHER_CTX *ctx;
 
    ctx = EVP_CIPHER_CTX_new();
    EVP_EncryptInit_ex(ctx, cipher, NULL, in->in_key, in->in_ivec);
 
    if (in->in_data_is_already_padded)
    {
        /* Check whether the input data is already padded.
        And its length must be an integral multiple of the cipher's block size. */
        const size_t bs = EVP_CIPHER_block_size(cipher);
        if (in->in_data_len % bs != 0)
        {
            printf("ERROR-1: data length=%d which is not added yet; block size=%d\n", (int) in->in_data_len, (int) bs);
            /* Warning: Remember to do some clean-ups */
            EVP_CIPHER_CTX_free(ctx);
            return;
        }
        /* Disable the implicit PKCS#7 padding defined in EVP_CIPHER */
        EVP_CIPHER_CTX_set_padding(ctx, 0);
    }
 
    out_buf = (unsigned char *) malloc(((in->in_data_len>>4)+1) << 4);
    out_len = 0;
    EVP_EncryptUpdate(ctx, out_buf, &out_len, in->in_data, in->in_data_len);
    if (1)
    {
        printf("Debug: out_len=%d\n", out_len);
    }
 
    out_padding_len = 0;
    EVP_EncryptFinal_ex(ctx, out_buf+out_len, &out_padding_len);
    if (1)
    {
        printf("Debug: out_padding_len=%d\n", out_padding_len);
    }
 
    EVP_CIPHER_CTX_free(ctx);
    if (1)
    {
        int i;
        int len;
        len = out_len + out_padding_len;
        for (i=0; i<len; i++)
        {
            printf("%02x ", out_buf[i]);
        }
        printf("\n");
    }
 
    if (out_buf)
    {
        free(out_buf);
        out_buf = NULL;
    }
}
 
void main()
{
    int have_sm4 = (OPENSSL_VERSION_NUMBER >= 0x10101001L);
    int have_aes = 1;
    const unsigned char data[]=
    {
        0x01, 0x23, 0x45, 0x67, 0x89, 0xab, 0xcd, 0xef,
        0xfe, 0xdc, 0xba, 0x98, 0x76, 0x54, 0x32, 0x10,
    };
    unsigned char ivec[EVP_MAX_IV_LENGTH]; ///< IV 向量
    const unsigned char key1[16] = ///< key_data, 密钥内容, 至少16字节
    {
        0x01, 0x23, 0x45, 0x67, 0x89, 0xab, 0xcd, 0xef,
        0xfe, 0xdc, 0xba, 0x98, 0x76, 0x54, 0x32, 0x10,
    };
    test_case_t tc;
 
    tc.in_data = data;
    tc.in_data_len = sizeof(data);
    tc.in_data_is_already_padded = (tc.in_data_len % 16)==0; // Hard coded 16 as the cipher's block size
    tc.in_key = key1;
    tc.in_key_len = sizeof(key1);
    memset(ivec, 0x00, EVP_MAX_IV_LENGTH);
    tc.in_ivec = ivec;
 
#if defined(OPENSSL_NO_SM4)
    have_sm4 = 0;
#endif
    if (have_sm4)
    {
        printf("[1]\n");
        printf("Debug: EVP_sm4_ecb() test\n");
        test_encrypt_with_cipher(&tc, EVP_sm4_ecb());
    }
#if defined(OPENSSL_NO_AES)
    have_aes = 0;
#endif
    if (have_aes)
    {
        printf("[2]\n");
        printf("Debug: EVP_aes_128_ecb() test\n");
        test_encrypt_with_cipher(&tc, EVP_aes_128_ecb());
    }
}

标签:const,openssl,len,api,使用,EVP,data,tc,out
From: https://www.cnblogs.com/summerjam-land/p/17776678.html

相关文章

  • Linux升级openssl、openssh
     在项目中,我们经常会发现Linux系统中OpenSSH、OpenSSL存在高危漏洞,如OpenSSL“心脏出血”漏洞,利用该漏洞,黑客可以获取约30%的https开头网址的用户登录账号密码,其中包括购物、网银、社交、门户等类型的知名网站等。以及OpenSSH漏洞,如“OpenSSH远程代码执行漏洞CVE-2016-10009......
  • Golang泛型的简单使用
    packagemainimport"fmt"//MyInt~表示不仅支持int8,还支持int8的衍生类型int8A和int8BtypeMyIntinterface{ int|~int8|int16|int32|int64}funcgetMaxNum[TMyInt](a,bT)T{ ifa>b{ returna } returnb}//结构体typeAgeTinterface{......
  • 实验二 OpenSSL API使用
    sm3代码#include<stdio.h>#include<openssl/evp.h>#include<openssl/err.h>#include<openssl/rand.h>voidhandleErrors(void){ERR_print_errors_fp(stderr);abort();}voidtest_sm3(){unsignedchardata[]="Hel......
  • 使用DOM4J的一个小例子
    packagecom.dujiali.test;importjava.io.File;importjava.io.FileNotFoundException;importjava.io.FileOutputStream;importjava.io.IOException;importjava.io.OutputStream;importjava.io.OutputStreamWriter;importjava.util.List;importorg.dom4j.Doc......
  • 使用卷积神经网络训练手写数字识别模型(CNN)
    https://www.cnblogs.com/zylyehuo/效果展示目录结构README.md#BasicMNISTExamplepipinstall-rrequirements.txtpythonmain.py#CUDA_VISIBLE_DEVICES=2pythonmain.py#tospecifyGPUidtoex.2requirements.txttorchtorchvisionmain.pyfrom......
  • 【Java 进阶篇】使用 JDBC 更新数据详解
    在关系型数据库中,更新数据是一项常见的任务。通过JavaJDBC(JavaDatabaseConnectivity),我们可以使用Java编程语言来执行更新操作,例如修改、删除或插入数据。本文将详细介绍如何使用JDBC来进行数据更新操作,包括示例代码和必要的概念。JDBC更新操作概述在JDBC中,更新操作通常分为以下......
  • MounRiver使用技巧及配置6
    1、使用MounRiver仿真时仅擦除程序代码部分flash空间配置(页擦)关于MounRiver仿真时仅擦除程序代码部分flash空间配置 2、使用MounRiver调试时如何配置不下载程序关于MounRiver调试时如何配置不下载程序 3、使用MounRiver下载时如何选择配置部分擦除不全擦关于MounRiver......
  • Android入门教程 | RecyclerView使用入门
    想必大家对列表的表现形式已经不再陌生。手机上有联系人列表,文件列表,短信列表等等。本文讲述的是在Android开发中用RecyclerView来实现列表效果。使用步骤引入RecyclerView在app的build.gradle文件中添加引用。我们使用的是androidx包。gradle:dependencies{//........
  • 使用 Flutter 制作一个简单的笑话生成器应用程序
    在本教程中,我将向您展示如何使用Flutter制作一个简单的笑话生成器应用程序对于这个项目,我们将从RESTfulAPI获取数据API的链接:随机笑话对于这个项目,我不会关注应用程序的UI,我们只会关注如何从URL中获取数据,以及如何显示它们在我们开始之前,您必须将此包添加到您的pubspec.y......
  • 使用Pytorch Geometric 进行链接预测代码示例
    PyTorchGeometric(PyG)是构建图神经网络模型和实验各种图卷积的主要工具。在本文中我们将通过链接预测来对其进行介绍。链接预测答了一个问题:哪两个节点应该相互链接?我们将通过执行“转换分割”,为建模准备数据。为批处理准备专用的图数据加载器。在TorchGeometric中构建一个......