openssl3.2 - 官方demo学习 - cipher - ariacbc.c

文章目录

    • openssl3.2 - 官方demo学习 - cipher - ariacbc.c
    • 概述
    • 笔记
    • END

openssl3.2 - 官方demo学习 - cipher - ariacbc.c

概述

ARIA-256-CBC
EVP_EncryptInit_x()的区别
EVP_EncryptInit() 参数为ctx, cipher, key, iv
EVP_EncryptInit_ex2() 参数为 ctx, cipher, key, iv, params[]
EVP_EncryptInit_ex() 参数为 ctx, cipher, engine_imp, key, iv
看了参数就知道:
如果不给params[], 就调用EVP_EncryptInit()
如果给params[], 就调用EVP_EncryptInit_ex2()
如果给engine, 就调用EVP_EncryptInit_ex()

笔记

/*! \file ariacbc.c
* \note
openssl3.2 - 官方demo学习 - cipher - ariacbc.c
ARIA-256-CBC
EVP_EncryptInit_x()的区别
EVP_EncryptInit() 参数为ctx, cipher, key, iv
EVP_EncryptInit_ex2() 参数为 ctx, cipher, key, iv, params[]
EVP_EncryptInit_ex() 参数为 ctx, cipher, engine_imp, key, iv
看了参数就知道:
如果不给params[], 就调用EVP_EncryptInit()
如果给params[], 就调用EVP_EncryptInit_ex2()
如果给engine, 就调用EVP_EncryptInit_ex()
*//** Copyright 2012-2023 The OpenSSL Project Authors. All Rights Reserved.** Licensed under the Apache License 2.0 (the "License").  You may not use* this file except in compliance with the License.  You can obtain a copy* in the file LICENSE in the source distribution or at* https://www.openssl.org/source/license.html*//** Simple ARIA CBC encryption demonstration program.*/#include <stdio.h>
#include <openssl/err.h>
#include <openssl/bio.h>
#include <openssl/evp.h>
#include <openssl/crypto.h>
#include <openssl/core_names.h>#include "my_openSSL_lib.h"/* ARIA key */
static const unsigned char cbc_key[] = {0xee, 0xbc, 0x1f, 0x57, 0x48, 0x7f, 0x51, 0x92, 0x1c, 0x04, 0x65, 0x66,0x5f, 0x8a, 0xe6, 0xd1, 0x65, 0x8b, 0xb2, 0x6d, 0xe6, 0xf8, 0xa0, 0x69,0xa3, 0x52, 0x02, 0x93, 0xa5, 0x72, 0x07, 0x8f
};/* Unique initialisation vector */
static const unsigned char cbc_iv[] = {0x99, 0xaa, 0x3e, 0x68, 0xed, 0x81, 0x73, 0xa0, 0xee, 0xd0, 0x66, 0x84,0x99, 0xaa, 0x3e, 0x68,
};/* Example plaintext to encrypt */
static const unsigned char cbc_pt[] = {0xf5, 0x6e, 0x87, 0x05, 0x5b, 0xc3, 0x2d, 0x0e, 0xeb, 0x31, 0xb2, 0xea,0xcc, 0x2b, 0xf2, 0xa5
};/* Expected ciphertext value */
static const unsigned char cbc_ct[] = {0x9a, 0x44, 0xe6, 0x85, 0x94, 0x26, 0xff, 0x30, 0x03, 0xd3, 0x7e, 0xc6,0xb5, 0x4a, 0x09, 0x66, 0x39, 0x28, 0xf3, 0x67, 0x14, 0xbc, 0xe8, 0xe2,0xcf, 0x31, 0xb8, 0x60, 0x42, 0x72, 0x6d, 0xc8
};/** A library context and property query can be used to select & filter* algorithm implementations. If they are NULL then the default library* context and properties are used.*/
OSSL_LIB_CTX* libctx = NULL;
const char* propq = NULL;int aria_cbc_encrypt(void)
{int ret = 0;EVP_CIPHER_CTX* ctx;EVP_CIPHER* cipher = NULL;int outlen, tmplen;size_t cbc_ivlen = sizeof(cbc_iv);unsigned char outbuf[1024];// unsigned char outtag[16];printf("ARIA CBC Encrypt:\n");printf("Plaintext:\n");BIO_dump_fp(stdout, cbc_pt, sizeof(cbc_pt));/* Create a context for the encrypt operation */if ((ctx = EVP_CIPHER_CTX_new()) == NULL)goto err;/* Fetch the cipher implementation */if ((cipher = EVP_CIPHER_fetch(libctx, "ARIA-256-CBC", propq)) == NULL)goto err;/** Initialise an encrypt operation with the cipher/mode, key and IV.* We are not setting any custom params so let params be just NULL.*//*! 如果不给param, 是否可以调用其他EVP_EncryptInit_X? *///if (!EVP_EncryptInit_ex2(ctx, cipher, cbc_key, cbc_iv, /* params */ NULL))//	goto err;/*! 如果不用给param[], 就直接用EVP_EncryptInit(), 不用调用EVP_EncryptInit_ex2() */if (!EVP_EncryptInit(ctx, cipher, cbc_key, cbc_iv))goto err;/*!* EVP_EncryptInit_x()的区别* EVP_EncryptInit() 参数为ctx, cipher, key, iv* EVP_EncryptInit_ex2() 参数为 ctx, cipher, key, iv, params[]* EVP_EncryptInit_ex() 参数为 ctx, cipher, engine_imp, key, iv* 看了参数就知道:* 如果不给params[], 就调用EVP_EncryptInit()* 如果给params[], 就调用EVP_EncryptInit_ex2()* 如果给engine, 就调用EVP_EncryptInit_ex()*//*!__owur int EVP_EncryptInit(EVP_CIPHER_CTX *ctx, const EVP_CIPHER *cipher,const unsigned char *key, const unsigned char *iv);__owur int EVP_EncryptInit_ex2(EVP_CIPHER_CTX *ctx, const EVP_CIPHER *cipher,const unsigned char *key,const unsigned char *iv,const OSSL_PARAM params[]);__owur int EVP_EncryptInit_ex(EVP_CIPHER_CTX *ctx,const EVP_CIPHER *cipher, ENGINE *impl,const unsigned char *key,const unsigned char *iv);*//* Encrypt plaintext */if (!EVP_EncryptUpdate(ctx, outbuf, &outlen, cbc_pt, sizeof(cbc_pt)))goto err;/* Finalise: there can be some additional output from padding */if (!EVP_EncryptFinal_ex(ctx, outbuf + outlen, &tmplen))goto err;outlen += tmplen;/* Output encrypted block */printf("Ciphertext (outlen:%d):\n", outlen);BIO_dump_fp(stdout, outbuf, outlen);if (sizeof(cbc_ct) == outlen && !CRYPTO_memcmp(outbuf, cbc_ct, outlen))printf("Final ciphertext matches expected ciphertext\n");elseprintf("Final ciphertext differs from expected ciphertext\n");ret = 1;
err:if (!ret)ERR_print_errors_fp(stderr);EVP_CIPHER_free(cipher);EVP_CIPHER_CTX_free(ctx);return ret;
}int aria_cbc_decrypt(void)
{int ret = 0;EVP_CIPHER_CTX* ctx;EVP_CIPHER* cipher = NULL;int outlen, tmplen /* rv */;size_t cbc_ivlen = sizeof(cbc_iv);unsigned char outbuf[1024];printf("ARIA CBC Decrypt:\n");printf("Ciphertext:\n");BIO_dump_fp(stdout, cbc_ct, sizeof(cbc_ct));if ((ctx = EVP_CIPHER_CTX_new()) == NULL)goto err;/* Fetch the cipher implementation */if ((cipher = EVP_CIPHER_fetch(libctx, "ARIA-256-CBC", propq)) == NULL)goto err;/** Initialise an encrypt operation with the cipher/mode, key and IV.* We are not setting any custom params so let params be just NULL.*/if (!EVP_DecryptInit_ex2(ctx, cipher, cbc_key, cbc_iv, /* params */ NULL))goto err;/* Decrypt plaintext */if (!EVP_DecryptUpdate(ctx, outbuf, &outlen, cbc_ct, sizeof(cbc_ct)))goto err;/* Finalise: there can be some additional output from padding */if (!EVP_DecryptFinal_ex(ctx, outbuf + outlen, &tmplen))goto err;outlen += tmplen;/* Output decrypted block */printf("Plaintext (outlen:%d):\n", outlen);BIO_dump_fp(stdout, outbuf, outlen);if (sizeof(cbc_pt) == outlen && !CRYPTO_memcmp(outbuf, cbc_pt, outlen))printf("Final plaintext matches original plaintext\n");elseprintf("Final plaintext differs from original plaintext\n");ret = 1;
err:if (!ret)ERR_print_errors_fp(stderr);EVP_CIPHER_free(cipher);EVP_CIPHER_CTX_free(ctx);return ret;
}int main(int argc, char** argv)
{if (!aria_cbc_encrypt())return EXIT_FAILURE;if (!aria_cbc_decrypt())return EXIT_FAILURE;return EXIT_SUCCESS;
}

END

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/news/618472.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

什么是激励函数?

激励函数&#xff08;激活函数&#xff09;在神经网络中扮演着至关重要的角色。它们是用于决定一个神经元是否应该被激活的函数&#xff0c;帮助神经网络学习复杂的模式。激活函数对输入信号进行非线性变换&#xff0c;然后输出到下一层。 不同的激励函数具有不同的数学特性&a…

ArchVizPRO Interior Vol.8 URP

ArchVizPRO Interior Vol.8 URP是一个在URP中制作的建筑可视化项目。这是一个完全可导航的现代公寓,包括一个带开放式厨房的客厅、休息区、两间卧室和两间浴室。从头开始构建每一个细节,这个室内有130多件家具和道具、自定义着色器和4K纹理。所有家具和道具都非常详细,可以在…

aigc 局部动画

ComfyUIAnimateDiffControlNet的Inpainting生成局部重绘动画_哔哩哔哩_bilibili 动图&#xff1a; 【Stable Diffusion】SD生成超稳定丝滑卡通动画&#xff08;附安装包&#xff09;&#xff0c;一分钟就能搞定动图&#xff01;小白轻松上手&#xff01;_哔哩哔哩_bilibili

基于 LangChain+大模型,我打造一款自己的LLM应用

本文共计1.7w字&#xff0c;梳理不易&#xff0c;喜欢点赞、收藏、关注。需要技术交流&#xff0c;可以加入我们 目录 通俗易懂讲解大模型系列技术交流一、LangChain是什么二、LangChain核心组件2.1 Models2.2 Indexes2.2.1 Document Loaders2.2.2 Text Splitters2.2.3 Vectors…

网络安全B模块(笔记详解)- MYSQL信息收集

MYSQL信息收集 1.通过渗透机场景Kali中的渗透测试工具对服务器场景MySQL03进行服务信息扫描渗透测试(使用工具Nmap,使用必须要使用的参数),并将该操作显示结果中数据库版本信息作为Flag提交; Flag:MySQL 5.5.12 2.通过渗透机场景Kali中的渗透测试工具对服务器场景MySQL0…

【光波电子学】基于MATLAB的多模光纤模场分布的仿真分析

基于MATLAB的多模光纤模场分布的仿真分析 一、引言 &#xff08;1&#xff09;多模光纤的概念 多模光纤&#xff08;MMF&#xff09;是一种具有较大纤芯直径的光纤结构&#xff0c;其核心直径通常在10-50微米范围内。与单模光纤&#xff08;SMF&#xff09;相比&#xff0c;…

【基础数据结构】栈和队列

例题1 化栈为队 实现一个MyQueue类&#xff0c;该类用两个栈来实现一个队列。 示例&#xff1a; MyQueue queue new MyQueue(); queue.push(1); queue.push(2); queue.peek(); // 返回 1 queue.pop(); // 返回 1 queue.empty(); // 返回 false说明&#xff1a; 你只能使用标…

Linux QT以太网配置及相关知识

Linux QT以太网配置及相关知识 平台和内容概述安装Qt Creator设计用户界面编辑源代码自定义LineEdit创建槽函数以太网逻辑功能实现静态配置ui逻辑:功能概述代码实现DNS退出程序输入框中的ip规范保存数据和读取数据构建文件编译运行平台注意点开机自动配置以太网总结平台和内容…

FreeRtos Queue (一)

本篇主要讲队列的数据结构和初始化 一、队列的数据结构 二、队列初始化完是什么样子的 队列初始化的函数调用关系&#xff1a;xQueueGenericCreate->prvInitialiseNewQueue->xQueueGenericReset 所以&#xff0c;最终初始化完的队列是这样的 假设申请了4个消息体&…

SQL:一行中存在任一指标就显示出来

当想要统计的两个指标不在一张表中时&#xff0c;需要做关联。但很多情况下&#xff0c;也没有办法保证其中一张表的维度是全的&#xff0c;用left join或right join可能会导致数据丢失。所以借助full join处理。 如&#xff0c;将下面的数据处理成表格中的效果&#xff08;维…

天下没有免费的午餐:AI技术进步与代价辩证分析

“天下没有免费的午餐”这一经济学原理揭示了在资源有限的世界中&#xff0c;任何收益或优势都必然伴随着相应的成本和牺牲。当我们将这一观点引入人工智能领域时&#xff0c;我们可以深入探讨AI技术的进步与发展所带来的巨大变革背后所隐藏的成本、挑战以及伦理道德问题。 一、…

AI副业拆解:人像卡通化,赋予你的形象全新生命力

大家好我是在看&#xff0c;记录普通人学习探索AI之路。 &#x1f525;让你的形象瞬间穿越二次元&#xff01;&#x1f680;人像卡通化&#xff0c;捕捉你的独特魅力&#xff0c;让真实与梦幻在此刻交融。&#x1f3a8; 今天为大家介绍如何免费把人像卡通化--漫画风 https://w…

【征稿进行时|见刊、检索快速稳定】2024年经济发展与旅游管理国际学术会议(ICEDTM 2024)

【征稿进行时|见刊、检索快速稳定】2024年经济发展与旅游管理国际学术会议(ICEDTM 2024) 2024 International Conference Economic Development and Tourism Management(ICEDTM 2024) 一、【会议简介】 ICEDTM 2024将围绕"旅游管理”“经济发展”的最新研究领域&#xff…

大模型开启应用时代 数钉科技一锤定音

叮叮叮叮&#xff01;数钉智造大模型&#xff0c;“定音”强势发布&#xff01; 随着科技的飞速发展&#xff0c;大模型技术已逐渐成为推动产业变革的核心力量。在这一浪潮中&#xff0c;数钉科技凭借深厚的技术积累和敏锐的市场洞察力&#xff0c;成功利用大模型技术搭建起智能…

<蓝桥杯软件赛>零基础备赛20周--第13周--DFS剪枝

报名明年4月蓝桥杯软件赛的同学们&#xff0c;如果你是大一零基础&#xff0c;目前懵懂中&#xff0c;不知该怎么办&#xff0c;可以看看本博客系列&#xff1a;备赛20周合集 20周的完整安排请点击&#xff1a;20周计划 每周发1个博客&#xff0c;共20周。 在QQ群上答疑&#x…

在ubuntu上的18个非常实用的命令行工具软件

使用Ubuntu的过程中&#xff0c;在终端上使用命令行工具是非常常见的事情&#xff0c;熟练地掌握命令行工具是使用ubuntu必不可少的技能&#xff0c;即便是Ubuntu的初学者&#xff0c;通常也很熟悉诸如ls、rm、cp等一些文件操作工具&#xff0c;当浏览/bin目录时&#xff0c;你…

Shell编程自动化之if、for、while和函数

一、if语句 1.单分支格式 if [ 条件判断式 ]; then当条件判断成立时&#xff0c;执行的命令内容 fi if [ 条件判断式 ] then当条件判断成立时&#xff0c;执行的命令内容 fi 2.双分支格式 if [ 条件判断式 ]; then当条件判断成立时&#xff0c;执行的命令内…

【CCNet】《CCNet:Criss-Cross Attention for Semantic Segmentation》

ICCV-2019 文章目录 1 Background and Motivation2 Related Work3 Advantages / Contributions4 Method5 Experiments5.1 Datasets and Metrics5.2 Experiments on Cityscapess5.3 Experiments on ADE20K5.4 Experiments on COCO 6 Conclusion&#xff08;own&#xff09; 1 Ba…

MAC通过终端,使用python3建立本地Web服务

实现局域网Web服务&#xff0c;很简单几句命令&#xff0c;一起看看。 1. 我相信你已经有 brew(Homebrew 包管理器) 了对么? 如果没有可以执行这个方法 /bin/bash -c "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/HEAD/install.sh)"2. 安…

网页的介绍

目录 什么是网页&#xff1a; 网页的组成&#xff1a; 什么是HTML&#xff1a; 网页的总结&#xff1a; 浏览器&#xff1a; web标准&#xff1a; 为什么需要Web标准&#xff1a; web标准的构成&#xff1a; 什么是网页&#xff1a; 1.网站是指在因特网上根据一定的规…