当前位置: 首页 > 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;

}

相关文章:

  • 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中的行级锁
  • 徐州建站推广/seo顾问什么职位
  • 如何建设简易网站/手机系统流畅神器
  • 网站管理公司/怎样精准搜索关键词
  • 奥鹏作业代做靠谱的网站/软文的本质是什么
  • 网站建设方式有哪些/今日国内新闻头条
  • 网络托管/谷歌seo排名