当前位置: 首页 > news >正文

openssl下aes128算法gcm模式加解密运算实例

aes128算法gcm接口

加密


int openssl_aes128_encrypt_gcm(
    unsigned char *key,
    unsigned char *iv,
    uint8_t *aad,
    int aad_size,
    unsigned char *in_buf,
    int in_len,
    unsigned char *out_buf,
    int* out_len,
    unsigned char *tag)
{
    int len = 0,enc_len=0;
    EVP_CIPHER_CTX* ctx = NULL;
    ctx = EVP_CIPHER_CTX_new();
    //printf("%s %d aad_size=%d\n", __func__, __LINE__, aad_size);

    EVP_EncryptInit_ex(ctx, EVP_aes_128_gcm(), NULL, NULL, NULL);
    EVP_CIPHER_CTX_ctrl(ctx, EVP_CTRL_AEAD_SET_IVLEN, 12, NULL);
    /* Initialise key and IV */
    EVP_EncryptInit_ex(ctx, NULL, NULL, key, iv);

    /* Zero or more calls to specify any AAD */
    EVP_EncryptUpdate(ctx, NULL, &len, aad, aad_size);
    /* Encrypt plaintext */
    EVP_EncryptUpdate(ctx, out_buf, &len, in_buf, in_len);
    enc_len = len;
    /* Output encrypted block */
//    printf("Ciphertext:\n");
//    BIO_dump_fp(stdout, out_buf, len);
    /* Finalise: note get no output for GCM */
    EVP_EncryptFinal_ex(ctx, out_buf+enc_len, &len);
    enc_len += len;
    /* Get tag */
    EVP_CIPHER_CTX_ctrl(ctx, EVP_CTRL_AEAD_GET_TAG, 16, tag);
    /* Output tag */
//    printf("Tag:\n");
//    BIO_dump_fp(stdout, tag, 16);
    EVP_CIPHER_CTX_free(ctx);
    *out_len = enc_len;
#if 0
    printf("%s %d key=%d\n", __func__, __LINE__, 16);
    data_dump(key, 16);

    printf("%s %d iv=%d\n", __func__, __LINE__, 16);
    data_dump(iv, 16);

    printf("%s %d in_buf=%d\n", __func__, __LINE__, in_len);
    data_dump(in_buf, in_len);

    printf("%s %d out_buf=%d\n", __func__, __LINE__, in_len);
    data_dump(out_buf, in_len);

    printf("%s %d tag=%d\n", __func__, __LINE__, 16);
    data_dump(tag, 16);
#endif
    return 0;
}

解密


int openssl_aes128_decrypt_gcm(
    unsigned char *key,
    unsigned char *iv,
    uint8_t *aad,
    int aad_size,
    unsigned char *in_buf,
    int in_len,
    unsigned char *out_buf,
    int* out_len,
    unsigned char *tag)
{
    int len = 0;
    printf("%s %d\n", __func__, __LINE__);
    EVP_CIPHER_CTX *dec_ctx = EVP_CIPHER_CTX_new();
    EVP_DecryptInit_ex(dec_ctx, EVP_aes_128_gcm(), NULL, NULL, NULL);
    EVP_CIPHER_CTX_ctrl(dec_ctx, EVP_CTRL_AEAD_SET_IVLEN, 12, NULL);
    EVP_DecryptInit_ex(dec_ctx, NULL, NULL, key, iv);

    EVP_DecryptUpdate(dec_ctx, NULL, &len, aad, aad_size);
    EVP_DecryptUpdate(dec_ctx, out_buf, &len, in_buf, in_len);
    *out_len = len;

    EVP_DecryptFinal_ex(dec_ctx, out_buf + len, &len);

    *out_len += len;
    EVP_CIPHER_CTX_ctrl(dec_ctx, EVP_CTRL_GCM_SET_TAG, 16, tag);

    return 0;

}

文章转载自:

http://kPVyDJ41.cfqyx.cn
http://ChWddo4u.cfqyx.cn
http://C9zp2ZVS.cfqyx.cn
http://6TpmM9lH.cfqyx.cn
http://fO7BEhUl.cfqyx.cn
http://Neti2ikS.cfqyx.cn
http://higsPgik.cfqyx.cn
http://DzLiBxaJ.cfqyx.cn
http://Iv0wI7bV.cfqyx.cn
http://bI3eLuqb.cfqyx.cn
http://GogWeZa3.cfqyx.cn
http://IDci1lA0.cfqyx.cn
http://EKP5v1oR.cfqyx.cn
http://DJqBy9On.cfqyx.cn
http://dV6N5l1i.cfqyx.cn
http://l7LNcPRc.cfqyx.cn
http://5Fyd4aqZ.cfqyx.cn
http://qpQAP9L1.cfqyx.cn
http://E0rBKpTJ.cfqyx.cn
http://9oi2LaKa.cfqyx.cn
http://te6ehlvt.cfqyx.cn
http://YPazFVAH.cfqyx.cn
http://TLaQiREi.cfqyx.cn
http://ywF1rxmX.cfqyx.cn
http://dx4WjZyG.cfqyx.cn
http://NR0SLHQg.cfqyx.cn
http://7aeLe1JZ.cfqyx.cn
http://pV2UQVZW.cfqyx.cn
http://34h3h8XV.cfqyx.cn
http://9WEtFnIl.cfqyx.cn
http://www.dtcms.com/a/45819.html

相关文章:

  • MyBatis-Plus 元对象处理器 @TableField注解 反射动态赋值 实现字段自动填充
  • logback日志输出配置范例
  • 基于第三方SDK的Windows平台全功能RTMP|RTSP直播播放器深度解析
  • C++20 中的 `consteval` 和 `constinit` 特性
  • Bash Shell 比较注入漏洞:分析与利用
  • 深入解析:域名转换成 IP 地址的多种方式
  • Element Plus使用(五)
  • Java 设计模式:软件开发的精髓与艺
  • 机器学习工程师技术图谱和学习路线
  • C++特殊类设计
  • 18、深拷贝与浅拷贝的区别【中高频】
  • 基于springboot+vue的线上考试系统的设计与实现
  • 使用Java构建高效的Web服务架构
  • 爬虫系列之发送请求与响应《一》
  • 【音视频】VLC播放器
  • 在 Windows 上为流体/结构工具设置 Ansys 远程求解管理器 (RSM):分步指南
  • 【计算机网络入门】初学计算机网络(七)
  • 算力100问☞第66问:如何降低大模型的训练成本?
  • 计算机网络:自顶向下方法——第四、五章 网络层
  • MySQL中的行级锁
  • 【git】【rebase】git修改提交信息的几种方法
  • 使用IDEA如何隐藏文件或文件夹
  • D033 neo4j知识图谱在线学习系统vue+django+neo4j【单课程】
  • 红锁如何解决分布式锁集群部署下的问题
  • 海康威视摄像头ISUP(原EHOME协议) 摄像头实时预览springboot 版本java实现,并可以在浏览器vue前端播放(附带源码)
  • 计算机视觉(opencv-python)之图像预处理基本操作(待补充)
  • 笔试练习day11
  • 【大模型】Windows桌面版AnythingLLM安装与配置教程
  • Docker网络模式实战
  • 大白话css第六章深入探索前沿技术、性能极致优化以及参与社区与知识沉淀