分享

使用openssl中的加密函数AES、RC4、RSA对文件加密的一个例子

 心不留意外尘 2016-05-06

2013

对加密有所了解的读者,相信对这三种加密算法也已经有了些许了解。

比如RSA是一种很慢的加密方式,他是非对称的,需要有公钥和私钥。对文件中的数据,不大适合用这种方式来加密。因为我使用的是对整个图片文件的每16个字节进行加密,要是每次都对取出来的16字节进行RSA加密,那速度,是相当慢的。

所以,提供一种思路,既可以达到安全,又可以做到加密。

比如我可以先把整个图片文件的每16个字节进行AES或者RC4加密,因为这两个加密函数是对称的,所以需要保存一个密钥,既可以解出来数据。对于这个密钥,我们只需要对他进行RSA加密,这样的安全性,就已经相当好了,速度也上得去了(这也是一般的思维方式)。

下面是所用到的对图片文件每16个字节进行AES、RC4加密的测试函数。附带一个简单的RSA加密测试,但是没区分公钥和私钥,对字符串进行RSA加密的这类资料网上太多了。

  1. // ConsoleApplication1.cpp : 定义控制台应用程序的入口点。  

  2. //  

  3. #include "stdafx.h"  

  4. #include <openssl/ssl.h>  

  5. #include <openssl/aes.h>  

  6. #include <openssl/rsa.h>  

  7. #include <openssl/rc4.h>  

  8. #pragma comment(lib,"libeay32.lib")  

  9. #pragma comment(lib,"ssleay32.lib")  

  10. #include "iostream"  

  11. using namespace std;  

  12. #include "string"  

  13. #include "fstream"  

  14. #define RELESE(P) if (P)        \  

  15. {                               \  

  16.     delete P;                   \  

  17.     P = NULL;                   \  

  18. }  

  19. #define RELESE_ARRAY(P) if (P)  \  

  20. {                               \  

  21.     delete[] P;                 \  

  22.     P = NULL;                   \  

  23. }  

  24. // 测试使用aes加密算法的例子  

  25. void TestAesEncrypt()  

  26. {  

  27.     unsigned char buf[16];  

  28.     memset(buf,1,sizeof(buf));  

  29.     strcpy((char *)buf, "zengraoli");  

  30.     cout << "current buf value is :" << buf << endl;  

  31.     unsigned char buf2[16];  

  32.     unsigned char buf3[16];  

  33.     unsigned char aes_keybuf[32];  

  34.     memset(aes_keybuf,0,sizeof(aes_keybuf));  

  35.     strcpy((char *)aes_keybuf, "zeng");  

  36.     cout << "current aes_keybuf value is :" << aes_keybuf << endl;  

  37.     AES_KEY aeskey;  

  38.     AES_set_encrypt_key(aes_keybuf,256,&aeskey);  

  39.     AES_encrypt(buf,buf2,&aeskey);  

  40.     cout << "current buf2 value is :" << buf2 << endl;  

  41.     memset(aes_keybuf,0,sizeof(aes_keybuf));  

  42.     strcpy((char *)aes_keybuf, "zeng2");  

  43.     cout << "current aes_keybuf value is :" << aes_keybuf << endl;  

  44.     AES_set_decrypt_key(aes_keybuf,256,&aeskey);  

  45.     AES_decrypt(buf2,buf3,&aeskey);  

  46.     cout << "current buf3 value is :" << buf3 << endl;  

  47.     if(memcmp(buf,buf3,sizeof(buf))==0)  

  48.         printf("test success\r\n");  

  49.     else  

  50.         printf("test fail\r\n");  

  51. }  

  52. // 测试使用aes加密文件算法的例子  

  53. int TestAesEncryptFile(std::string in_file_path, std::string out_file_path,   

  54.                        const char *rc4_encrypt_key, int encrypt_chunk_size = 16)  

  55. {  

  56.     ifstream fin(in_file_path.c_str(), ios::binary);  

  57.     ofstream fout(out_file_path, ios::binary);  

  58.     if(!fin)  

  59.     {  

  60.         cout << "Can not open fin file." << endl;  

  61.         return 1;  

  62.     }  

  63.     if(!fout)  

  64.     {  

  65.         cout << "Can not open fout file." << endl;  

  66.         return 1;  

  67.     }  

  68.     //用指定密钥对一段内存进行加密,结果放在outbuffer中  

  69.     unsigned char aes_keybuf[32];  

  70.     memset(aes_keybuf,0,sizeof(aes_keybuf));  

  71.     strcpy((char *)aes_keybuf, "zengraoli");  

  72.     AES_KEY aeskey;  

  73.     AES_set_encrypt_key(aes_keybuf, 256, &aeskey);  

  74.     char *in_data  = new char[encrypt_chunk_size + 1];  

  75.     char *out_data = new char[encrypt_chunk_size + 1];  

  76.     while(!fin.eof())  

  77.     {  

  78.         fin.read(in_data, encrypt_chunk_size);  

  79.         AES_encrypt((const unsigned char *)in_data, (unsigned char *)out_data, &aeskey);  

  80.         fout.write(out_data, fin.gcount());  

  81.     };  

  82.     fout.close();  

  83.     fin.close();  

  84.     RELESE_ARRAY(in_data);  

  85.     RELESE_ARRAY(out_data);  

  86.     return 0;  

  87. }  

  88. // 测试使用aes解密文件算法的例子  

  89. int TestAesDecryptFile(std::string in_file_path, std::string out_file_path,   

  90.                        const char *rc4_dencrypt_key, int encrypt_chunk_size = 16)  

  91. {  

  92.     ifstream fin(in_file_path.c_str(), ios::binary);  

  93.     ofstream fout(out_file_path, ios::binary);  

  94.     if(!fin)  

  95.     {  

  96.         cout << "Can not open fin file." << endl;  

  97.         return 1;  

  98.     }  

  99.     if(!fout)  

  100.     {  

  101.         cout << "Can not open fout file." << endl;  

  102.         return 1;  

  103.     }  

  104.     //用指定密钥对一段内存进行加密,结果放在outbuffer中  

  105.     unsigned char aes_keybuf[32];  

  106.     memset(aes_keybuf,0,sizeof(aes_keybuf));  

  107.     strcpy((char *)aes_keybuf, "zengraoli");  

  108.     AES_KEY aeskey;  

  109.     AES_set_decrypt_key(aes_keybuf, 256, &aeskey);  

  110.     char *in_data  = new char[encrypt_chunk_size + 1];  

  111.     char *out_data = new char[encrypt_chunk_size + 1];  

  112.     while( ! fin.eof() )  

  113.     {  

  114.         fin.read(in_data, encrypt_chunk_size);  

  115.         AES_decrypt((unsigned char *)in_data, (unsigned char *)out_data, &aeskey);  

  116.         fout.write(out_data, fin.gcount());  

  117.     };  

  118.     fout.close();  

  119.     fin.close();  

  120.     RELESE_ARRAY(in_data);  

  121.     RELESE_ARRAY(out_data);  

  122.     return 0;  

  123. }  

  124. // 测试使用aes加密算法的例子  

  125. void TestRsaEncrypt()  

  126. {  

  127.     BIGNUM b={0};  

  128.     RSA* pRsa = RSA_generate_key(1024, RSA_F4, 0, 0);  

  129.     //pRsa中包含了N D,你这里自己修改就可以了  

  130.     char in_data[] = "zengraoli";  

  131.     cout << "current in_data value is : " << in_data << endl;  

  132.     int len = RSA_size(pRsa);  

  133.     char* out_data = new char[len];  

  134.     memset(out_data, 0, len);  

  135.     RSA_public_encrypt( sizeof(in_data), (unsigned char *)in_data,  

  136.                         (unsigned char *)out_data, pRsa, RSA_PKCS1_PADDING);  

  137.     cout << "current out_data value is : " << out_data << endl;  

  138.     char out[1024] = {0};  

  139.     RSA_private_decrypt(len, (unsigned char *)out_data,   

  140.                         (unsigned char *)out, pRsa, RSA_PKCS1_PADDING);  

  141.     RSA_free(pRsa);  

  142.     cout << "current out value is : " << out << endl;  

  143. }  

  144. // 测试使用rc4加密算法的例子  

  145. void TestRc4Encrypt()  

  146. {  

  147.     char code[64]={0};  

  148.     int codelen = sizeof(code);  

  149.     memcpy_s(code, 64, "This is secrect", 15);  

  150.     cout << "before encrypt :" << code << endl;  

  151.     unsigned char *outbuffer = new unsigned char[codelen];  

  152.     //用指定密钥对一段内存进行加密,结果放在outbuffer中  

  153.     RC4_KEY rc4_key;  

  154.     RC4_set_key(&rc4_key,7,(unsigned char *)"zenraoli");  

  155.     RC4(&rc4_key,codelen,(unsigned char *)code,outbuffer);  

  156.     cout << "after encrypt :" << outbuffer << endl;  

  157.     //用指定密钥对outbuffer中的密文进行解密,结果放回原来的内存  

  158.     memset(code,0,sizeof(code));  

  159.     RC4_set_key(&rc4_key,7,(unsigned char *)"zenraoli");//这里必须再次设置密钥  

  160.     RC4(&rc4_key,codelen,outbuffer,(unsigned char *)code);  

  161.     cout << "after decrypt :" << code << endl;  

  162.     if (outbuffer)  

  163.     {  

  164.         delete[] outbuffer;  

  165.         outbuffer = NULL;  

  166.     }  

  167. }  

  168. // 测试使用rc4加密文件算法的例子  

  169. int TestRc4EncryptFile(std::string in_file_path, std::string out_file_path,   

  170.                        const char *rc4_encrypt_key, int encrypt_chunk_size = 16)  

  171. {  

  172.     ifstream fin(in_file_path.c_str(), ios::binary);  

  173.     ofstream fout(out_file_path, ios::binary);  

  174.     if(!fin)  

  175.     {  

  176.         cout << "Can not open fin file." << endl;  

  177.         return 1;  

  178.     }  

  179.     if(!fout)  

  180.     {  

  181.         cout << "Can not open fout file." << endl;  

  182.         return 1;  

  183.     }  

  184.     //用指定密钥对一段内存进行加密,结果放在outbuffer中  

  185.     char code[64] = {0};  

  186.     int codelen = sizeof(code);  

  187.     RC4_KEY rc4_key;  

  188.     RC4_set_key(&rc4_key, strlen(rc4_encrypt_key), (unsigned char *)rc4_encrypt_key);  

  189.     char *in_data  = new char[encrypt_chunk_size + 1];  

  190.     char *out_data = new char[encrypt_chunk_size + 1];  

  191.     while(!fin.eof())  

  192.     {  

  193.         fin.read(in_data, encrypt_chunk_size);  

  194.         RC4(&rc4_key, (size_t)fin.gcount(),(unsigned char *)in_data, (unsigned char *)out_data);  

  195.         fout.write(out_data, fin.gcount());  

  196.     };  

  197.     fout.close();  

  198.     fin.close();  

  199.     RELESE_ARRAY(in_data);  

  200.     RELESE_ARRAY(out_data);  

  201.     return 0;  

  202. }  

  203. // 测试使用rc4解密文件算法的例子  

  204. int TestRc4DecryptFile(std::string in_file_path, std::string out_file_path,   

  205.                        const char *rc4_dencrypt_key, int encrypt_chunk_size = 16)  

  206. {  

  207.     ifstream fin(in_file_path.c_str(), ios::binary);  

  208.     ofstream fout(out_file_path, ios::binary);  

  209.     if(!fin)  

  210.     {  

  211.         cout << "Can not open fin file." << endl;  

  212.         return 1;  

  213.     }  

  214.     if(!fout)  

  215.     {  

  216.         cout << "Can not open fout file." << endl;  

  217.         return 1;  

  218.     }  

  219.     //用指定密钥对一段内存进行加密,结果放在outbuffer中  

  220.     char code[64] = {0};  

  221.     int codelen = sizeof(code);  

  222.     RC4_KEY rc4_key;  

  223.     RC4_set_key(&rc4_key, strlen(rc4_dencrypt_key), (unsigned char *)rc4_dencrypt_key);  

  224.     char *in_data  = new char[encrypt_chunk_size + 1];  

  225.     char *out_data = new char[encrypt_chunk_size + 1];  

  226.     while(!fin.eof())  

  227.     {  

  228.         fin.read(in_data, encrypt_chunk_size);  

  229.         RC4(&rc4_key, (size_t)fin.gcount(),(unsigned char *)in_data, (unsigned char *)out_data);  

  230.         fout.write(out_data, fin.gcount());  

  231.     };  

  232.     fout.close();  

  233.     fin.close();  

  234.     RELESE_ARRAY(in_data);  

  235.     RELESE_ARRAY(out_data);  

  236.     return 0;  

  237. }  

  238. int _tmain(int argc, _TCHAR* argv[])  

  239. {  

  240. //  TestAesEncrypt();  

  241. //   

  242. //  TestAesEncryptFile("1.gif", "2.gif", "zengraoli");  

  243. //   

  244. //  TestAesDecryptFile("2.gif", "3.gif", "zengraoli");  

  245.     TestRsaEncrypt();  

  246.     TestRc4Encrypt();  

  247.     TestRc4EncryptFile("1.gif", "2.gif", "zengraoli");  

  248.     TestRc4DecryptFile("2.gif", "3.gif", "zengraoli");  

  249.     return 0;  

  250. }  



整个测试工程的地址是:

http://download.csdn.net/detail/zengraoli/6636897

    本站是提供个人知识管理的网络存储空间,所有内容均由用户发布,不代表本站观点。请注意甄别内容中的联系方式、诱导购买等信息,谨防诈骗。如发现有害或侵权内容,请点击一键举报。
    转藏 分享 献花(0

    0条评论

    发表

    请遵守用户 评论公约

    类似文章 更多