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

来源:互联网 发布:刺客信条钩刃淘宝 编辑:程序博客网 时间:2024/06/01 08:36

原文来自:http://blog.csdn.net/zengraoli/article/details/17047735

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


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


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


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


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

[cpp] view plaincopyprint?在CODE上查看代码片派生到我的代码片
  1. // ConsoleApplication1.cpp : 定义控制台应用程序的入口点。  
  2. //  
  3.   
  4. #include "stdafx.h"  
  5. #include <openssl/ssl.h>  
  6. #include <openssl/aes.h>  
  7. #include <openssl/rsa.h>  
  8. #include <openssl/rc4.h>  
  9. #pragma comment(lib,"libeay32.lib")  
  10. #pragma comment(lib,"ssleay32.lib")  
  11. #include "iostream"  
  12. using namespace std;  
  13. #include "string"  
  14. #include "fstream"  
  15.   
  16.   
  17. #define RELESE(P) if (P)        \  
  18. {                               \  
  19.     delete P;                   \  
  20.     P = NULL;                   \  
  21. }  
  22.   
  23. #define RELESE_ARRAY(P) if (P)  \  
  24. {                               \  
  25.     delete[] P;                 \  
  26.     P = NULL;                   \  
  27. }  
  28.   
  29.   
  30. // 测试使用aes加密算法的例子  
  31. void TestAesEncrypt()  
  32. {  
  33.     unsigned char buf[16];  
  34.     memset(buf,1,sizeof(buf));  
  35.     strcpy((char *)buf, "zengraoli");  
  36.   
  37.     cout << "current buf value is :" << buf << endl;  
  38.   
  39.     unsigned char buf2[16];  
  40.     unsigned char buf3[16];  
  41.     unsigned char aes_keybuf[32];  
  42.   
  43.     memset(aes_keybuf,0,sizeof(aes_keybuf));  
  44.     strcpy((char *)aes_keybuf, "zeng");  
  45.     cout << "current aes_keybuf value is :" << aes_keybuf << endl;  
  46.     AES_KEY aeskey;  
  47.   
  48.     AES_set_encrypt_key(aes_keybuf,256,&aeskey);  
  49.     AES_encrypt(buf,buf2,&aeskey);  
  50.   
  51.     cout << "current buf2 value is :" << buf2 << endl;  
  52.   
  53.     memset(aes_keybuf,0,sizeof(aes_keybuf));  
  54.     strcpy((char *)aes_keybuf, "zeng2");  
  55.     cout << "current aes_keybuf value is :" << aes_keybuf << endl;  
  56.     AES_set_decrypt_key(aes_keybuf,256,&aeskey);  
  57.   
  58.     AES_decrypt(buf2,buf3,&aeskey);  
  59.   
  60.     cout << "current buf3 value is :" << buf3 << endl;  
  61.   
  62.     if(memcmp(buf,buf3,sizeof(buf))==0)  
  63.         printf("test success\r\n");  
  64.     else  
  65.         printf("test fail\r\n");  
  66. }  
  67.   
  68.   
  69. // 测试使用aes加密文件算法的例子  
  70. int TestAesEncryptFile(std::string in_file_path, std::string out_file_path,   
  71.                        const char *rc4_encrypt_key, int encrypt_chunk_size = 16)  
  72. {  
  73.     ifstream fin(in_file_path.c_str(), ios::binary);  
  74.     ofstream fout(out_file_path, ios::binary);  
  75.   
  76.     if(!fin)  
  77.     {  
  78.         cout << "Can not open fin file." << endl;  
  79.         return 1;  
  80.     }  
  81.     if(!fout)  
  82.     {  
  83.         cout << "Can not open fout file." << endl;  
  84.         return 1;  
  85.     }  
  86.   
  87.     //用指定密钥对一段内存进行加密,结果放在outbuffer中  
  88.     unsigned char aes_keybuf[32];  
  89.     memset(aes_keybuf,0,sizeof(aes_keybuf));  
  90.     strcpy((char *)aes_keybuf, "zengraoli");  
  91.     AES_KEY aeskey;  
  92.     AES_set_encrypt_key(aes_keybuf, 256, &aeskey);  
  93.   
  94.     char *in_data  = new char[encrypt_chunk_size + 1];  
  95.     char *out_data = new char[encrypt_chunk_size + 1];  
  96.     while(!fin.eof())  
  97.     {  
  98.         fin.read(in_data, encrypt_chunk_size);  
  99.         AES_encrypt((const unsigned char *)in_data, (unsigned char *)out_data, &aeskey);  
  100.         fout.write(out_data, fin.gcount());  
  101.     };  
  102.   
  103.     fout.close();  
  104.     fin.close();  
  105.   
  106.     RELESE_ARRAY(in_data);  
  107.     RELESE_ARRAY(out_data);  
  108.   
  109.     return 0;  
  110. }  
  111.   
  112. // 测试使用aes解密文件算法的例子  
  113. int TestAesDecryptFile(std::string in_file_path, std::string out_file_path,   
  114.                        const char *rc4_dencrypt_key, int encrypt_chunk_size = 16)  
  115. {  
  116.     ifstream fin(in_file_path.c_str(), ios::binary);  
  117.     ofstream fout(out_file_path, ios::binary);  
  118.   
  119.     if(!fin)  
  120.     {  
  121.         cout << "Can not open fin file." << endl;  
  122.         return 1;  
  123.     }  
  124.     if(!fout)  
  125.     {  
  126.         cout << "Can not open fout file." << endl;  
  127.         return 1;  
  128.     }  
  129.   
  130.     //用指定密钥对一段内存进行加密,结果放在outbuffer中  
  131.     unsigned char aes_keybuf[32];  
  132.     memset(aes_keybuf,0,sizeof(aes_keybuf));  
  133.     strcpy((char *)aes_keybuf, "zengraoli");  
  134.     AES_KEY aeskey;  
  135.     AES_set_decrypt_key(aes_keybuf, 256, &aeskey);  
  136.   
  137.     char *in_data  = new char[encrypt_chunk_size + 1];  
  138.     char *out_data = new char[encrypt_chunk_size + 1];  
  139.     while( ! fin.eof() )  
  140.     {  
  141.         fin.read(in_data, encrypt_chunk_size);  
  142.         AES_decrypt((unsigned char *)in_data, (unsigned char *)out_data, &aeskey);  
  143.         fout.write(out_data, fin.gcount());  
  144.     };  
  145.   
  146.     fout.close();  
  147.     fin.close();  
  148.   
  149.     RELESE_ARRAY(in_data);  
  150.     RELESE_ARRAY(out_data);  
  151.   
  152.     return 0;  
  153. }  
  154.   
  155.   
  156. // 测试使用aes加密算法的例子  
  157. void TestRsaEncrypt()  
  158. {  
  159.     BIGNUM b={0};  
  160.     RSA* pRsa = RSA_generate_key(1024, RSA_F4, 0, 0);  
  161.   
  162.     //pRsa中包含了N D,你这里自己修改就可以了  
  163.     char in_data[] = "zengraoli";  
  164.     cout << "current in_data value is : " << in_data << endl;  
  165.   
  166.     int len = RSA_size(pRsa);  
  167.     char* out_data = new char[len];  
  168.     memset(out_data, 0, len);  
  169.     RSA_public_encrypt( sizeof(in_data), (unsigned char *)in_data,  
  170.                         (unsigned char *)out_data, pRsa, RSA_PKCS1_PADDING);  
  171.     cout << "current out_data value is : " << out_data << endl;  
  172.   
  173.     char out[1024] = {0};  
  174.     RSA_private_decrypt(len, (unsigned char *)out_data,   
  175.                         (unsigned char *)out, pRsa, RSA_PKCS1_PADDING);  
  176.     RSA_free(pRsa);  
  177.   
  178.     cout << "current out value is : " << out << endl;  
  179. }  
  180.   
  181.   
  182. // 测试使用rc4加密算法的例子  
  183. void TestRc4Encrypt()  
  184. {  
  185.     char code[64]={0};  
  186.     int codelen = sizeof(code);  
  187.     memcpy_s(code, 64, "This is secrect", 15);  
  188.   
  189.     cout << "before encrypt :" << code << endl;  
  190.     unsigned char *outbuffer = new unsigned char[codelen];  
  191.   
  192.     //用指定密钥对一段内存进行加密,结果放在outbuffer中  
  193.     RC4_KEY rc4_key;  
  194.     RC4_set_key(&rc4_key,7,(unsigned char *)"zenraoli");  
  195.     RC4(&rc4_key,codelen,(unsigned char *)code,outbuffer);  
  196.     cout << "after encrypt :" << outbuffer << endl;  
  197.   
  198.     //用指定密钥对outbuffer中的密文进行解密,结果放回原来的内存  
  199.     memset(code,0,sizeof(code));  
  200.     RC4_set_key(&rc4_key,7,(unsigned char *)"zenraoli");//这里必须再次设置密钥  
  201.     RC4(&rc4_key,codelen,outbuffer,(unsigned char *)code);  
  202.     cout << "after decrypt :" << code << endl;  
  203.   
  204.     if (outbuffer)  
  205.     {  
  206.         delete[] outbuffer;  
  207.         outbuffer = NULL;  
  208.     }  
  209. }  
  210.   
  211.   
  212. // 测试使用rc4加密文件算法的例子  
  213. int TestRc4EncryptFile(std::string in_file_path, std::string out_file_path,   
  214.                        const char *rc4_encrypt_key, int encrypt_chunk_size = 16)  
  215. {  
  216.     ifstream fin(in_file_path.c_str(), ios::binary);  
  217.     ofstream fout(out_file_path, ios::binary);  
  218.   
  219.     if(!fin)  
  220.     {  
  221.         cout << "Can not open fin file." << endl;  
  222.         return 1;  
  223.     }  
  224.     if(!fout)  
  225.     {  
  226.         cout << "Can not open fout file." << endl;  
  227.         return 1;  
  228.     }  
  229.   
  230.     //用指定密钥对一段内存进行加密,结果放在outbuffer中  
  231.     char code[64] = {0};  
  232.     int codelen = sizeof(code);  
  233.     RC4_KEY rc4_key;  
  234.     RC4_set_key(&rc4_key, strlen(rc4_encrypt_key), (unsigned char *)rc4_encrypt_key);  
  235.       
  236.     char *in_data  = new char[encrypt_chunk_size + 1];  
  237.     char *out_data = new char[encrypt_chunk_size + 1];  
  238.     while(!fin.eof())  
  239.     {  
  240.         fin.read(in_data, encrypt_chunk_size);  
  241.         RC4(&rc4_key, (size_t)fin.gcount(),(unsigned char *)in_data, (unsigned char *)out_data);  
  242.         fout.write(out_data, fin.gcount());  
  243.     };  
  244.   
  245.     fout.close();  
  246.     fin.close();  
  247.   
  248.     RELESE_ARRAY(in_data);  
  249.     RELESE_ARRAY(out_data);  
  250.   
  251.     return 0;  
  252. }  
  253.   
  254. // 测试使用rc4解密文件算法的例子  
  255. int TestRc4DecryptFile(std::string in_file_path, std::string out_file_path,   
  256.                        const char *rc4_dencrypt_key, int encrypt_chunk_size = 16)  
  257. {  
  258.     ifstream fin(in_file_path.c_str(), ios::binary);  
  259.     ofstream fout(out_file_path, ios::binary);  
  260.   
  261.     if(!fin)  
  262.     {  
  263.         cout << "Can not open fin file." << endl;  
  264.         return 1;  
  265.     }  
  266.     if(!fout)  
  267.     {  
  268.         cout << "Can not open fout file." << endl;  
  269.         return 1;  
  270.     }  
  271.   
  272.     //用指定密钥对一段内存进行加密,结果放在outbuffer中  
  273.     char code[64] = {0};  
  274.     int codelen = sizeof(code);  
  275.     RC4_KEY rc4_key;  
  276.     RC4_set_key(&rc4_key, strlen(rc4_dencrypt_key), (unsigned char *)rc4_dencrypt_key);  
  277.   
  278.     char *in_data  = new char[encrypt_chunk_size + 1];  
  279.     char *out_data = new char[encrypt_chunk_size + 1];  
  280.     while(!fin.eof())  
  281.     {  
  282.         fin.read(in_data, encrypt_chunk_size);  
  283.         RC4(&rc4_key, (size_t)fin.gcount(),(unsigned char *)in_data, (unsigned char *)out_data);  
  284.         fout.write(out_data, fin.gcount());  
  285.     };  
  286.   
  287.     fout.close();  
  288.     fin.close();  
  289.   
  290.     RELESE_ARRAY(in_data);  
  291.     RELESE_ARRAY(out_data);  
  292.   
  293.     return 0;  
  294. }  
  295.   
  296.   
  297. int _tmain(int argc, _TCHAR* argv[])  
  298. {  
  299. //  TestAesEncrypt();  
  300. //   
  301. //  TestAesEncryptFile("1.gif", "2.gif", "zengraoli");  
  302. //   
  303. //  TestAesDecryptFile("2.gif", "3.gif", "zengraoli");  
  304.   
  305.     TestRsaEncrypt();  
  306.   
  307.     TestRc4Encrypt();  
  308.   
  309.     TestRc4EncryptFile("1.gif""2.gif""zengraoli");  
  310.   
  311.     TestRc4DecryptFile("2.gif""3.gif""zengraoli");  
  312.   
  313.     return 0;  
  314. }  



整个测试工程的地址是:

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


0 0