如何在 Openssl 中使用 AES 进行加密 [英] How to do encryption using AES in Openssl

查看:54
本文介绍了如何在 Openssl 中使用 AES 进行加密的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试编写一个示例程序来使用 Openssl 进行 AES 加密.我尝试浏览 Openssl 文档(这很痛苦),但无法弄清楚.我浏览了代码,找到了使用的 API,我编写了一个小程序,如下所示(请省略行号).我没有看到任何加密发生......我错过了什么吗?

I am trying to write a sample program to do AES encryption using Openssl. I tried going through Openssl documentation( it's a pain), could not figure out much. I went through the code and found the API's using which i wrote a small program as below (please omit the line numbers). I don't see any encryption happening... am i missing something?

PS:编译时我没有遇到任何错误.

PS: I don't get any errors upon compilation.

  1 #include <stdio.h> 
  2 #include <openssl/aes.h>   
  3 
  4 static const unsigned char key[] = {
  5   0x00, 0x11, 0x22, 0x33, 0x44, 0x55, 0x66, 0x77,
  6     0x88, 0x99, 0xaa, 0xbb, 0xcc, 0xdd, 0xee, 0xff,
  7       0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
  8         0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f
  9         };
 10 
 11 void main()
 12 {
 13     unsigned char text[]="virident";
 14     unsigned char out[10]; 
 15     unsigned char decout[10];
 16 
 17     AES_KEY wctx;
 18 
 19     AES_set_encrypt_key(key, 128, &wctx);
 20     AES_encrypt(text, out, &wctx);  
 21 
 22     printf("encryp data = %s
", out);
 23     
 24     AES_decrypt(out, decout, &wctx);
 25     printf(" Decrypted o/p: %s 
", decout);
 26 
 27 
 28 }

请帮我解决这个问题...

Please help me to figure this out...

推荐答案

查看此链接,其中包含使用 EVP API 使用 AES256CBC 加密/解密数据的示例代码.

Check out this link it has a example code to encrypt/decrypt data using AES256CBC using EVP API.

https://github.com/saju/misc/blob/master/misc/openssl_aes.c

您也可以在我开发的一个详细的开源项目中查看 AES256 CBC 的使用情况 https://github.com/llubu/mpro

Also you can check the use of AES256 CBC in a detailed open source project developed by me at https://github.com/llubu/mpro

代码足够详细并带有注释,如果您仍然需要对 API 本身进行大量解释,我建议您阅读这本书 Viega/Messier/Chandra 的《Network Security with OpenSSL》(谷歌它你会很容易找到这个的 pdf.)阅读第 6 章,它是特定于使用 EVP API 的对称密码的.这帮助我真正理解了使用 EVP 的各种功能和结构背后的原因.

The code is detailed enough with comments and if you still need much explanation about the API itself i suggest check out this book Network Security with OpenSSL by Viega/Messier/Chandra (google it you will easily find a pdf of this..) read chapter 6 which is specific to symmetric ciphers using EVP API.. This helped me a lot actually understanding the reasons behind using various functions and structures of EVP.

如果您想深入了解 Openssl 加密库,我建议从 openssl 网站(您机器上安装的版本)下载代码,然后查看 EVP 和 aeh 的实现api 实现.

and if you want to dive deep into the Openssl crypto library, i suggest download the code from the openssl website (the version installed on your machine) and then look in the implementation of EVP and aeh api implementation.

您上面发布的代码的另一个建议我看到您正在使用 aes.h 中的 api而是使用 EVP.在此处查看这样做的原因 使用 EVP 与算法的 OpenSSL用于对称加密的 API 由 Daniel 在我提出的一个问题中很好地解释了..

One more suggestion from the code you posted above i see you are using the api from aes.h instead use EVP. Check out the reason for doing this here OpenSSL using EVP vs. algorithm API for symmetric crypto nicely explained by Daniel in one of the question asked by me..

这篇关于如何在 Openssl 中使用 AES 进行加密的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆