OpenSSL测试-HMAC

任务详情: 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

posted @ 2024-05-13 11:01  ahuahauhau  阅读(106)  评论(0)    收藏  举报