首页 > 其他分享 >实验二 OpenSSL API使用

实验二 OpenSSL API使用

时间:2023-10-19 16:14:03浏览次数:42  
标签:ciphertext ctx OpenSSL len unsigned char API 实验 EVP

SM3测试代码

#include <stdio.h>
#include <string.h>
#include "openssl/evp.h"
#include "err.h"

void tDigest(){
        unsigned char md_value[EVP_MAX_MD_SIZE];
        int md_len, i;
        EVP_MD_CTX *mdctx;
        char msg1[] = "20211113";
        char msg2[] = "kcf";
        mdctx = EVP_MD_CTX_new();
        EVP_MD_CTX_init(mdctx);

        EVP_DigestInit_ex(mdctx, EVP_sm3(), NULL);
        EVP_DigestUpdate(mdctx, msg1, strlen(msg1));
        EVP_DigestUpdate(mdctx, msg2, strlen(msg2));
        EVP_DigestFinal_ex(mdctx, md_value, &md_len);
        EVP_MD_CTX_free(mdctx);

        printf("%s%s的SM3摘要值:\n", msg1, msg2);
        for(i = 0; i < md_len; i++){
                printf("%02x", md_value[i]);
        }
        printf("\n");
}

int main(){
        OpenSSL_add_all_algorithms();
        tDigest();
        return 0;
}

SM4测试代码

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <openssl/evp.h>

void handleErrors()
{
    printf("An error occurred\n");
    abort();
}

int encrypt(unsigned char *plaintext, int plaintext_len, unsigned char *key,
  unsigned char *iv, unsigned char *ciphertext)
{
    EVP_CIPHER_CTX *ctx;
    int len;
    int ciphertext_len;

    /* Create and initialize the context */
    if(!(ctx = EVP_CIPHER_CTX_new())) handleErrors();
    if(1 != EVP_EncryptInit_ex(ctx, EVP_sm4_cbc(), NULL, key, iv)) handleErrors();

    /* Encrypt the plaintext */
    if(1 != EVP_EncryptUpdate(ctx, ciphertext, &len, plaintext, plaintext_len))
        handleErrors();
    ciphertext_len = len;

    /* Finalize the encryption */
    if(1 != EVP_EncryptFinal_ex(ctx, ciphertext + len, &len)) handleErrors();
    ciphertext_len += len;

    /* Clean up the context */
    EVP_CIPHER_CTX_free(ctx);

    return ciphertext_len;
}

int decrypt(unsigned char *ciphertext, int ciphertext_len, unsigned char *key,
  unsigned char *iv, unsigned char *plaintext)
{
    EVP_CIPHER_CTX *ctx;
    int len;
    int plaintext_len;

    /* Create and initialize the context */
    if(!(ctx = EVP_CIPHER_CTX_new())) handleErrors();
    if(1 != EVP_DecryptInit_ex(ctx, EVP_sm4_cbc(), NULL, key, iv)) handleErrors();

    /* Decrypt the ciphertext */
    if(1 != EVP_DecryptUpdate(ctx, plaintext, &len, ciphertext, ciphertext_len))
        handleErrors();
    plaintext_len = len;

    /* Finalize the decryption */
    if(1 != EVP_DecryptFinal_ex(ctx, plaintext + len, &len)) handleErrors();
    plaintext_len += len;

    /* Clean up the context */
    EVP_CIPHER_CTX_free(ctx);

    return plaintext_len;
}

int main(int arc, char *argv[]) {
    unsigned char *key = (unsigned char *)"0123456789abcdef";
    unsigned char *iv = (unsigned char *)"0123456789abcdef";
    unsigned char *plaintext = (unsigned char *)"Hello World!";
    int plaintext_len = strlen((char *)plaintext);
    unsigned char ciphertext[128];
    unsigned char decryptedtext[128];
    int decryptedtext_len, ciphertext_len;

    OpenSSL_add_all_algorithms();
    ERR_load_crypto_strings();

    ciphertext_len = encrypt(plaintext, plaintext_len, key, iv, ciphertext);
    printf("Ciphertext is:\n");
    BIO_dump_fp(stdout, (const char *)ciphertext, ciphertext_len);

    decryptedtext_len = decrypt(ciphertext, ciphertext_len, key, iv, decryptedtext);
    decryptedtext[decryptedtext_len] = '\0';
    printf("Decrypted text is:\n");
    printf("%s\n", decryptedtext);

    return 0;
}


AES-256-CBC测试代码

#include <stdio.h>
#include <string.h>
#include <openssl/evp.h>

int main() {
    OpenSSL_add_all_algorithms();

    // 加密
    EVP_CIPHER_CTX *ctx = EVP_CIPHER_CTX_new();
    EVP_CipherInit_ex(ctx, EVP_aes_256_cbc(), NULL, NULL, NULL, 1);
    
    // 设置加密密钥和IV(初始化向量)
    unsigned char key[32] = "01234567890123456789012345678901";
    unsigned char iv[16] = "0123456789012345";
    EVP_CipherInit_ex(ctx, NULL, NULL, key, iv, 1);
    
    // 待加密的数据
    unsigned char plaintext[] = "Hello, OpenSSL!";
    unsigned char ciphertext[128];
    
    int len;
    EVP_CipherUpdate(ctx, ciphertext, &len, plaintext, strlen((char *)plaintext));
    int ciphertext_len = len;
    
    EVP_CipherFinal_ex(ctx, ciphertext + len, &len);
    ciphertext_len += len;
    
    printf("Ciphertext: ");
    for (int i = 0; i < ciphertext_len; i++) {
        printf("%02x", ciphertext[i]);
    }
    printf("\n");
    
    EVP_CIPHER_CTX_free(ctx);
    
    // 解密
    ctx = EVP_CIPHER_CTX_new();
    EVP_CipherInit_ex(ctx, EVP_aes_256_cbc(), NULL, NULL, NULL, 0);
    EVP_CipherInit_ex(ctx, NULL, NULL, key, iv, 0);
    
    unsigned char decryptedtext[128];
    
    EVP_CipherUpdate(ctx, decryptedtext, &len, ciphertext, ciphertext_len);
    int decryptedtext_len = len;
    
    EVP_CipherFinal_ex(ctx, decryptedtext + len, &len);
    decryptedtext_len += len;
    
    printf("Decrypted Text: %s\n", decryptedtext);
    
    EVP_CIPHER_CTX_free(ctx);

    return 0;
}


标签:ciphertext,ctx,OpenSSL,len,unsigned,char,API,实验,EVP
From: https://www.cnblogs.com/20211110lyx/p/17774944.html

相关文章

  • 在.net core 6.0 中 使用WebAPI进行QQ的邮件发送
    首先,是在工作中遇到的发邮件问题,但是自己还没有去实现,就先写了一个Demo。主要的内容是在网上搜的。下面进入正文。首先发邮件,第一步要确认发送的邮件的邮箱是那个邮箱的邮箱号,比较绕。就像我是拿QQ邮箱作为发件人,那么我就需要登录QQ邮箱,点开设置(由于我登录的是网站版的所以设置......
  • Util应用框架Web Api开发快速入门
    本文是使用Util应用框架开发WebApi项目快速入门教程.前面已经详细介绍了环境搭建,如果你还未准备好,请参考前文.开发流程概述创建代码生成专用数据库.Util应用框架需要专门用来生成代码的数据库,该数据库仅用于代码生成.约定:代码生成数据库应以.Generator结尾.当......
  • 实验二测试1—— OpenSSL命令测试1
       ......
  • 20211102尹子扬 实验二 openssl命令测试
    点击查看代码openssldgst-sm3-outsn.sm3sn.txt3.(用od打印时发现有\n换行符,所以在第4步时不加-n,否则会生成错误的hash值)点击查看代码od-tc-Ansn.sm34.(正确的是不带-n的hash值)点击查看代码echo"20211102"|openssldgst-sm3代码截图:......
  • 快速展示原型之Minimal API开发
    MinimalAPI官网地址:https://learn.microsoft.com/zh-cn/aspnet/core/fundamentals/minimal-apis/security?view=aspnetcore-7.0MinimalAPI背景介绍MinimalAPIs是指在ASP.NETCore中引入的一种轻量级的API开发模式。它的产生背景是为了简化API的创建和开发流程,减少样......
  • Kubernetes:kube-apiserver 之 scheme(二)
    接Kubernetes:kube-apiserver之scheme(一)。2.2资源convert上篇说到资源版本之间通过内部版本__internal进行资源转换。这里进一步扩展介绍资源转换内容,以加深理解。同样以例子开始,通过kubectl将apps/v1beta1/Deployment转换为apps/v1/Deployment。apiVersion:a......
  • 爱尔兰中央银行对Investing Capitals发出警告!
    上周五,爱尔兰中央银行(CBI)发布了一则警告,提醒公众小心一家名为“InvestingCapitals”的欺诈实体。该实体运营了一个网站,并通过该网站声称自己是一家投资公司,却并没有得到应有的授权。爱尔兰中央银行负责执行该国相关金融法律规定,维护金融市场秩序。CBI表示,未经授权的公司若在爱尔......
  • 实验2
    1#include<stdio.h>2#include<stdlib.h>3#include<time.h>45#defineN56#defineN13747#defineN246589intmain()10{11intnumber;12inti;1314srand(time(0));1516for(i=0;i<N;++......
  • 实验1 类和对象
     #include<iostream>#include<string>#include<vector>#include<array>template<typenameT>voidoutput1(constT&obj){for(autoi:obj)std::cout<<i<<",";std::cout<<"\b\b......
  • 实验二
    一、实验二作业①:要求:在中国气象网(http://www.weather.com.cn)给定城市集的7日天气预报,并保存在数据库。输出信息序号地区日期天气信息温度1北京7日(今天)多云转晴31℃/17℃2北京8日(明天)晴34℃/20℃3北京9日(后天)多云转晴32℃/19℃4北京10日(......