本文主要是介绍使用openssl中的加密函数AES、RC4、RSA对文件加密的一个例子,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
原文来自:http://blog.csdn.net/zengraoli/article/details/17047735
对加密有所了解的读者,相信对这三种加密算法也已经有了些许了解。
比如RSA是一种很慢的加密方式,他是非对称的,需要有公钥和私钥。对文件中的数据,不大适合用这种方式来加密。因为我使用的是对整个图片文件的每16个字节进行加密,要是每次都对取出来的16字节进行RSA加密,那速度,是相当慢的。
所以,提供一种思路,既可以达到安全,又可以做到加密。
比如我可以先把整个图片文件的每16个字节进行AES或者RC4加密,因为这两个加密函数是对称的,所以需要保存一个密钥,既可以解出来数据。对于这个密钥,我们只需要对他进行RSA加密,这样的安全性,就已经相当好了,速度也上得去了(这也是一般的思维方式)。
下面是所用到的对图片文件每16个字节进行AES、RC4加密的测试函数。附带一个简单的RSA加密测试,但是没区分公钥和私钥,对字符串进行RSA加密的这类资料网上太多了。
- // ConsoleApplication1.cpp : 定义控制台应用程序的入口点。
- //
- #include "stdafx.h"
- #include <openssl/ssl.h>
- #include <openssl/aes.h>
- #include <openssl/rsa.h>
- #include <openssl/rc4.h>
- #pragma comment(lib,"libeay32.lib")
- #pragma comment(lib,"ssleay32.lib")
- #include "iostream"
- using namespace std;
- #include "string"
- #include "fstream"
- #define RELESE(P) if (P) \
- { \
- delete P; \
- P = NULL; \
- }
- #define RELESE_ARRAY(P) if (P) \
- { \
- delete[] P; \
- P = NULL; \
- }
- // 测试使用aes加密算法的例子
- void TestAesEncrypt()
- {
- unsigned char buf[16];
- memset(buf,1,sizeof(buf));
- strcpy((char *)buf, "zengraoli");
- cout << "current buf value is :" << buf << endl;
- unsigned char buf2[16];
- unsigned char buf3[16];
- unsigned char aes_keybuf[32];
- memset(aes_keybuf,0,sizeof(aes_keybuf));
- strcpy((char *)aes_keybuf, "zeng");
- cout << "current aes_keybuf value is :" << aes_keybuf << endl;
- AES_KEY aeskey;
- AES_set_encrypt_key(aes_keybuf,256,&aeskey);
- AES_encrypt(buf,buf2,&aeskey);
- cout << "current buf2 value is :" << buf2 << endl;
- memset(aes_keybuf,0,sizeof(aes_keybuf));
- strcpy((char *)aes_keybuf, "zeng2");
- cout << "current aes_keybuf value is :" << aes_keybuf << endl;
- AES_set_decrypt_key(aes_keybuf,256,&aeskey);
- AES_decrypt(buf2,buf3,&aeskey);
- cout << "current buf3 value is :" << buf3 << endl;
- if(memcmp(buf,buf3,sizeof(buf))==0)
- printf("test success\r\n");
- else
- printf("test fail\r\n");
- }
- // 测试使用aes加密文件算法的例子
- int TestAesEncryptFile(std::string in_file_path, std::string out_file_path,
- const char *rc4_encrypt_key, int encrypt_chunk_size = 16)
- {
- ifstream fin(in_file_path.c_str(), ios::binary);
- ofstream fout(out_file_path, ios::binary);
- if(!fin)
- {
- cout << "Can not open fin file." << endl;
- return 1;
- }
- if(!fout)
- {
- cout << "Can not open fout file." << endl;
- return 1;
- }
- //用指定密钥对一段内存进行加密,结果放在outbuffer中
- unsigned char aes_keybuf[32];
- memset(aes_keybuf,0,sizeof(aes_keybuf));
- strcpy((char *)aes_keybuf, "zengraoli");
- AES_KEY aeskey;
- AES_set_encrypt_key(aes_keybuf, 256, &aeskey);
- char *in_data = new char[encrypt_chunk_size + 1];
- char *out_data = new char[encrypt_chunk_size + 1];
- while(!fin.eof())
- {
- fin.read(in_data, encrypt_chunk_size);
- AES_encrypt((const unsigned char *)in_data, (unsigned char *)out_data, &aeskey);
- fout.write(out_data, fin.gcount());
- };
- fout.close();
- fin.close();
- RELESE_ARRAY(in_data);
- RELESE_ARRAY(out_data);
- return 0;
- }
- // 测试使用aes解密文件算法的例子
- int TestAesDecryptFile(std::string in_file_path, std::string out_file_path,
- const char *rc4_dencrypt_key, int encrypt_chunk_size = 16)
- {
- ifstream fin(in_file_path.c_str(), ios::binary);
- ofstream fout(out_file_path, ios::binary);
- if(!fin)
- {
- cout << "Can not open fin file." << endl;
- return 1;
- }
- if(!fout)
- {
- cout << "Can not open fout file." << endl;
- return 1;
- }
- //用指定密钥对一段内存进行加密,结果放在outbuffer中
- unsigned char aes_keybuf[32];
- memset(aes_keybuf,0,sizeof(aes_keybuf));
- strcpy((char *)aes_keybuf, "zengraoli");
- AES_KEY aeskey;
- AES_set_decrypt_key(aes_keybuf, 256, &aeskey);
- char *in_data = new char[encrypt_chunk_size + 1];
- char *out_data = new char[encrypt_chunk_size + 1];
- while( ! fin.eof() )
- {
- fin.read(in_data, encrypt_chunk_size);
- AES_decrypt((unsigned char *)in_data, (unsigned char *)out_data, &aeskey);
- fout.write(out_data, fin.gcount());
- };
- fout.close();
- fin.close();
- RELESE_ARRAY(in_data);
- RELESE_ARRAY(out_data);
- return 0;
- }
- // 测试使用aes加密算法的例子
- void TestRsaEncrypt()
- {
- BIGNUM b={0};
- RSA* pRsa = RSA_generate_key(1024, RSA_F4, 0, 0);
- //pRsa中包含了N D,你这里自己修改就可以了
- char in_data[] = "zengraoli";
- cout << "current in_data value is : " << in_data << endl;
- int len = RSA_size(pRsa);
- char* out_data = new char[len];
- memset(out_data, 0, len);
- RSA_public_encrypt( sizeof(in_data), (unsigned char *)in_data,
- (unsigned char *)out_data, pRsa, RSA_PKCS1_PADDING);
- cout << "current out_data value is : " << out_data << endl;
- char out[1024] = {0};
- RSA_private_decrypt(len, (unsigned char *)out_data,
- (unsigned char *)out, pRsa, RSA_PKCS1_PADDING);
- RSA_free(pRsa);
- cout << "current out value is : " << out << endl;
- }
- // 测试使用rc4加密算法的例子
- void TestRc4Encrypt()
- {
- char code[64]={0};
- int codelen = sizeof(code);
- memcpy_s(code, 64, "This is secrect", 15);
- cout << "before encrypt :" << code << endl;
- unsigned char *outbuffer = new unsigned char[codelen];
- //用指定密钥对一段内存进行加密,结果放在outbuffer中
- RC4_KEY rc4_key;
- RC4_set_key(&rc4_key,7,(unsigned char *)"zenraoli");
- RC4(&rc4_key,codelen,(unsigned char *)code,outbuffer);
- cout << "after encrypt :" << outbuffer << endl;
- //用指定密钥对outbuffer中的密文进行解密,结果放回原来的内存
- memset(code,0,sizeof(code));
- RC4_set_key(&rc4_key,7,(unsigned char *)"zenraoli");//这里必须再次设置密钥
- RC4(&rc4_key,codelen,outbuffer,(unsigned char *)code);
- cout << "after decrypt :" << code << endl;
- if (outbuffer)
- {
- delete[] outbuffer;
- outbuffer = NULL;
- }
- }
- // 测试使用rc4加密文件算法的例子
- int TestRc4EncryptFile(std::string in_file_path, std::string out_file_path,
- const char *rc4_encrypt_key, int encrypt_chunk_size = 16)
- {
- ifstream fin(in_file_path.c_str(), ios::binary);
- ofstream fout(out_file_path, ios::binary);
- if(!fin)
- {
- cout << "Can not open fin file." << endl;
- return 1;
- }
- if(!fout)
- {
- cout << "Can not open fout file." << endl;
- return 1;
- }
- //用指定密钥对一段内存进行加密,结果放在outbuffer中
- char code[64] = {0};
- int codelen = sizeof(code);
- RC4_KEY rc4_key;
- RC4_set_key(&rc4_key, strlen(rc4_encrypt_key), (unsigned char *)rc4_encrypt_key);
- char *in_data = new char[encrypt_chunk_size + 1];
- char *out_data = new char[encrypt_chunk_size + 1];
- while(!fin.eof())
- {
- fin.read(in_data, encrypt_chunk_size);
- RC4(&rc4_key, (size_t)fin.gcount(),(unsigned char *)in_data, (unsigned char *)out_data);
- fout.write(out_data, fin.gcount());
- };
- fout.close();
- fin.close();
- RELESE_ARRAY(in_data);
- RELESE_ARRAY(out_data);
- return 0;
- }
- // 测试使用rc4解密文件算法的例子
- int TestRc4DecryptFile(std::string in_file_path, std::string out_file_path,
- const char *rc4_dencrypt_key, int encrypt_chunk_size = 16)
- {
- ifstream fin(in_file_path.c_str(), ios::binary);
- ofstream fout(out_file_path, ios::binary);
- if(!fin)
- {
- cout << "Can not open fin file." << endl;
- return 1;
- }
- if(!fout)
- {
- cout << "Can not open fout file." << endl;
- return 1;
- }
- //用指定密钥对一段内存进行加密,结果放在outbuffer中
- char code[64] = {0};
- int codelen = sizeof(code);
- RC4_KEY rc4_key;
- RC4_set_key(&rc4_key, strlen(rc4_dencrypt_key), (unsigned char *)rc4_dencrypt_key);
- char *in_data = new char[encrypt_chunk_size + 1];
- char *out_data = new char[encrypt_chunk_size + 1];
- while(!fin.eof())
- {
- fin.read(in_data, encrypt_chunk_size);
- RC4(&rc4_key, (size_t)fin.gcount(),(unsigned char *)in_data, (unsigned char *)out_data);
- fout.write(out_data, fin.gcount());
- };
- fout.close();
- fin.close();
- RELESE_ARRAY(in_data);
- RELESE_ARRAY(out_data);
- return 0;
- }
- int _tmain(int argc, _TCHAR* argv[])
- {
- // TestAesEncrypt();
- //
- // TestAesEncryptFile("1.gif", "2.gif", "zengraoli");
- //
- // TestAesDecryptFile("2.gif", "3.gif", "zengraoli");
- TestRsaEncrypt();
- TestRc4Encrypt();
- TestRc4EncryptFile("1.gif", "2.gif", "zengraoli");
- TestRc4DecryptFile("2.gif", "3.gif", "zengraoli");
- return 0;
- }
整个测试工程的地址是:
http://download.csdn.net/detail/zengraoli/6636897
这篇关于使用openssl中的加密函数AES、RC4、RSA对文件加密的一个例子的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!