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

OpenSSL测试-HMAC

时间:2024-05-13 11:10:11浏览次数:14  
标签:GetLastError return 测试 OpenSSL else --------------------------------------------

任务详情: 0 参考别人代码要给出链接,使用AI工具要给出过程,否则成绩归零 1 在openEuler(推荐)或Ubuntu中,使用OpenSSL命令行工具,对消息“你的学号(数字)重复10遍”使用HMAC-SM3进行验证。密钥为随机数,自己生成。提交命令行操作过程截图和结果。 (5分) 2 使用C语言和OpenSSL库,在openEuler或Ubuntu上编写程序对字符串“你的学号(数字)重复10遍”进行HMAC-SM3加密。密钥同上。提交完整代码和程序运行结果截图以及两次结果的对比。(10分)3 使用龙脉智能钥匙编程完成2的内容,提交完整代码和程序运行结果截图以及三次结果的对比。(10分)

0 AI工具和参考链接:

AI工具:
image
image
参考链接:
https://blog.csdn.net/weixin_66422501/article/details/138301797

1 在openEuler(推荐)或Ubuntu中,使用OpenSSL命令行工具,对消息“你的学号(数字)重复10遍”使用HMAC-SM3进行验证。密钥为随机数,自己生成。提交命令行操作过程截图和结果。

image

2使用C语言和OpenSSL库,在openEuler或Ubuntu上编写程序对字符串“你的学号(数字)重复10遍”进行HMAC-SM3加密。密钥同上。提交完整代码和程序运行结果截图以及两次结果的对比。

完整代码:

#include <stdio.h>
#include <string.h>
#include <openssl/evp.h>
#include <openssl/hmac.h>
int main() {
    char *key = "3510d81b46d1c8a908733c21ac85d4727e427a8fbd83c3784121d475324aac78"; // 设置HMAC密钥
    char *data ="20211318202113182021131820211318202113182021131820211318202113182021131820211318" ; // 要加密的数据
    unsigned char digest[EVP_MAX_MD_SIZE];
    unsigned int digest_len;
    HMAC(EVP_sm3(), key, strlen(key), (unsigned char *)data, strlen(data), digest, &digest_len);
    printf("HMAC-SM3 加密结果: ");
    for (int i = 0; i < digest_len; i++) {
        printf("%02x", digest[i]);
    }
    printf("\n");
    return 0;
}

运行结果截图:
image

3 使用龙脉智能钥匙编程完成2的内容,提交完整代码和程序运行结果截图以及三次结果的对比。

#include "generic.h"
#include <cstdio>
#include <iostream>
#include <string>
#include <vector>

using namespace std;

bool EncryptFile(const string &strSource,
const string &strDestination,
const string &strPassword);

//--------------------------------------------------------------------
// Begin main.

void main(void)
{
char szSrcFile[1024] = {0}; //sourcefile
char szTgtFile[1024] = {0}; //encryptfile
char szPassword[1024] = {0}; //password

printf("\nEncrypt a file.\n\nEnter the name of the file to be encrypt:\n");
scanf("%s", szSrcFile);

printf("Enter the name of the output file:\n");
scanf("%s", szTgtFile);

printf("Enter the password to encrypt the file:\n");
scanf("%s", szPassword);

if(EncryptFile(szSrcFile, szTgtFile, szPassword))
{
printf("Encrypt file successfully.\n");
}
else
{
printf("Encrypt file failed.\n");
}
}

//--------------------------------------------------------------------
// Code for the function EncryptFile called by main.
bool EncryptFile(const string &strSource,
const string &strDestination,
const string &strPassword)
{
//--------------------------------------------------------------------
// Declare and initialize local variables.

FILE *hSource = NULL;
FILE *hDestination = NULL;

HCRYPTPROV hCryptProv = NULL;
HCRYPTKEY hKey = NULL;
HCRYPTHASH hHash = NULL;

//--------------------------------------------------------------------
// Open source file.
BeginAction("Open source file for read");
if(NULL != (hSource = fopen(strSource.c_str(), "rb")))
{
ActionSuccess();
}
else
{
ActionFailed(GetLastError());
return FALSE;
}

//--------------------------------------------------------------------
// Open destination file.
BeginAction("Open target file for write");
if(NULL != (hDestination = fopen(strDestination.c_str(), "wb")))
{
ActionSuccess();
}
else
{
ActionFailed(GetLastError());
return FALSE;
}

// Get a CSP handle.
BeginAction("CryptAcquireContext()");
if(CryptAcquireContext(&hCryptProv,
TEST_CONTAINER,
CSP_NAME,
PROV_RSA_FULL,
0))
{
ActionSuccess();
}
else // Container does not exists, let us create a new one.
{
ActionFailed(GetLastError());

BeginAction("CryptAcquireContext() CRYPT_NEWKEYSET");
if(CryptAcquireContext(&hCryptProv,
TEST_CONTAINER,
CSP_NAME,
PROV_RSA_FULL,
CRYPT_NEWKEYSET))
{
ActionSuccess();
}
else
{
ActionFailed(GetLastError());
return FALSE;
}
}

HCRYPTPROV_Holder holder(hCryptProv);

BeginAction("CryptCreateHash()");
if(CryptCreateHash(hCryptProv, CALG_MD5, 0, 0, &hHash))
{
ActionSuccess();
}
else
{
ActionFailed(GetLastError());
return FALSE;
}

BeginAction("CryptHashData()");
if(CryptHashData(hHash,
(BYTE *) strPassword.c_str(),
strPassword.length(),
0))
{
ActionSuccess();
}
else
{
ActionFailed(GetLastError());
return FALSE;
}

BeginAction("CryptDeriveKey()");
if(CryptDeriveKey(hCryptProv, ENCRYPT_ALGORITHM, hHash, KEYLENGTH, &hKey))
{
ActionSuccess();
}
else
{
ActionFailed(GetLastError());
return FALSE;
}

BeginAction("CryptDestroyHash()");
if(CryptDestroyHash(hHash))
{
hHash = NULL;
ActionSuccess();
}
else
{
ActionFailed(GetLastError());
return FALSE;
}

DWORD dwBlockLen = 0;
DWORD dwBufferLen = 0;
DWORD dwCount = 0;

dwBlockLen = 1000 - 1000 % ENCRYPT_BLOCK_SIZE;

//--------------------------------------------------------------------
// Determine the block size. If a block cipher is used,
// it must have room for an extra block.

if(ENCRYPT_BLOCK_SIZE > 1)
{
dwBufferLen = dwBlockLen + ENCRYPT_BLOCK_SIZE;
}
else
{
dwBufferLen = dwBlockLen;
}

vector<BYTE> pbBuffer;
pbBuffer.resize(dwBufferLen);

//--------------------------------------------------------------------
// In a do loop, encrypt the source file and write to the source file.
do
{
//--------------------------------------------------------------------
// Read up to dwBlockLen bytes from the source file.
dwCount = fread(&pbBuffer[0], 1, dwBlockLen, hSource);
if(ferror(hSource))
{
ShowSysError("Read plain text", GetLastError());
return FALSE;
}
//--------------------------------------------------------------------
// Encrypt
if(!CryptEncrypt(hKey,
0,
feof(hSource), //file end
0,
&pbBuffer[0], // [in] sourcefile; [out] encryptdata
&dwCount, //encryptdatalength
dwBufferLen)) // encrypt data length
{
ShowSysError("CryptEncrypt()", GetLastError());
return FALSE;
}

//--------------------------------------------------------------------
// Write data to the destination file.
fwrite(&pbBuffer[0], 1, dwCount, hDestination);
if(ferror(hDestination))
{
ShowSysError("Write cipher text", GetLastError());
return FALSE;
}
}
while(!feof(hSource));
//--------------------------------------------------------------------
// End the do loop when the last block of the source file has been
// read, encrypted, and written to the destination file.

//--------------------------------------------------------------------
// Close files.
if(hSource)
{
fclose(hSource);
}
if(hDestination)
{
fclose(hDestination);
}

if(hKey)
{
CryptDestroyKey(hKey);
}

return TRUE;
} // End of Encryptfile

三次结果比较
image

标签:GetLastError,return,测试,OpenSSL,else,--------------------------------------------
From: https://www.cnblogs.com/wjmbk123/p/18188820

相关文章

  • 性能测试实战训练营 - 慧测
    红框中的就是堆和栈 资源消耗大,那么就要分析栈,对栈内存做dump,一个线程对应一个栈,分析消耗cpu过高的线程(runnable状态的) 具体的例子,下面是一段java程序代码, 只有一个runnable的线程Thread1,该线程循环输出一句话, 运行该程序 该线程运行时会非常消耗cpu 通过to......
  • 一个PowerShell内网渗透测试的大纲:
    一个PowerShell内网渗透测试的大纲:介绍内网渗透测试:什么是内网渗透测试?内网渗透测试的目的和方法。内网渗透测试的法律和道德问题。PowerShell基础知识回顾:PowerShell的基本语法和命令。PowerShell的常见用途和功能。PowerShell在内网渗透测试中的重要性和作用......
  • ESP32-S3-WROOM-1 arduino测试选项
    1、2、3、 ......
  • 实验三 软件测试
    一、实验题目:软件测试二、实验目的 1、熟悉开发环境下的自动化测试工具;1、利用自动化测试工具进行自动化单元测试。三、实验内容1、选择开发环境,IDEA或PYCHARM任选其一;2、基于所选择的开发环境实现对输入的n个整数进行排序的代码;3、对所编写代码设计测试用例;4、基于所选......
  • pivot 分组案例测试
    droptableStudentScores;CREATETABLEStudentScores(schoolvarchar(20),UserNameVARCHAR(20),--学生姓名SubjectVARCHAR(30),--科目ScoreFLOAT--成绩);INSERTINTOStudentScoresSELECT'人民大学......
  • C# 单元测试工具选择
    由于最近在尝试开发桌面应用,技术选用的是WPF,但是开发起来有点卡手,发现原来缺少了测试工具,花了两天,把主要把主要的测试工具尝试了一下。现有的单元测试工具查阅资料,已知在vsstudio上主要为3款,分别是xUnit,NUnit,以及SMTest,三款工具各有优劣。现有问题三款单元测试都存在无法实......
  • checkpoint防火墙测试授权申请
    本文介绍如何在线申请checkpoint防火墙的测试授权请先确保已注册官网账号并能正常登录ProductCenter,并安装好checkpoint并配置好管理IP(授权申请需要用到设备IP地址,不需要连网)(官网账号最好使用公司邮箱申请)ProductCenter链接正常登录后可看到如下图内容其中selecta......
  • Playwright安装与Python集成:探索跨浏览器测试的奇妙世界
    前言web自动化测试是我们工作中的重要一环,随着Web应用程序和网站的复杂性不断增加,跨浏览器测试变得越来越必要。而Playwright作为一种新兴的跨浏览器测试工具,为开发人员提供了一种轻松愉快的方式来进行自动化测试。本文将带您深入探索Playwright的安装过程,并结合Python语言,揭示......
  • autoit au3 IT管理员使用指南(一)基础安装、测试、编译
    简介AutoIt是一款完全免费的Windows自动化工具,支持各种Windows操作系统,可以用于自动运行基于GUI和非GUI程序,与系统进行交互,以及创建自定义的GUI窗体,完成各种自动化任务。对我们IT管理员来说,什么办公自动化就算了,我们用的最多的其实是安装软件。曾到处收集软件安装时的静默......
  • Openssl 设置 双向认证证书的过程
    Openssl设置双向认证证书的过程openssl的安装安装openssl大部分操作系统都会带openssl只是版本略有不同.因为不带openssl连基本的openssh可能都没法用.安装方法yuminstallopenssl-y查看版本:opensslversionOpenSSL1.1.1kFIPS25Mar2021OpenEuler2203......