openssl 测试加密卡_C++: 基于OpenSSL的AES256加解密测试

1 #include

2 #include

3 #include "iostream"

4 using namespacestd;5 #include "string"

6 #include "fstream"

7 #pragma comment(lib,"ws2_32.lib")

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

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

10

11 #define RELESE(P) if (P) \

12 { \13 deleteP; \14 P =NULL; \15 }16

17 #define RELESE_ARRAY(P) if (P) \

18 { \19 delete[] P; \20 P =NULL; \21 }22

23

24 //测试使用aes加密算法的例子(字符串)

25 /*

26 int main()27 {28 unsigned char buf[16];29 memset(buf, 1, sizeof(buf));30 strcpy((char *)buf, "this is mingwen");31

32 cout << "current buf value is :" << buf << endl;33

34 unsigned char buf2[16];35 unsigned char buf3[16];36 unsigned char aes_keybuf[32];37

38 memset(aes_keybuf, 0, sizeof(aes_keybuf));39 strcpy((char *)aes_keybuf, "key1");40 cout << "current aes_keybuf value is :" << aes_keybuf << endl;41 AES_KEY aeskey;42

43 AES_set_encrypt_key(aes_keybuf, 256, &aeskey);44 //cout << "AESkey is:" << aeskey << endl;45

46 AES_encrypt(buf, buf2, &aeskey);47 cout << "current buf2 value is :" << buf2 << endl;48

49 memset(aes_keybuf, 0, sizeof(aes_keybuf));50 strcpy((char *)aes_keybuf, "key2");51 cout << "current aes_keybuf value is :" << aes_keybuf << endl;52

53 AES_set_decrypt_key(aes_keybuf, 256, &aeskey);54

55 AES_decrypt(buf2, buf3, &aeskey);56 cout << "current buf2 value is :" << buf2 << endl;57 cout << "*********************************" << endl;58 cout << "current buf3 value is :" << buf3 << endl;59

60 if (memcmp(buf, buf3, sizeof(buf)) != 0)61 printf("AES256 test success\r\n");62 else63 printf("AES256 test fail\r\n");64 return 0;65 }66 */

67

68

69

70

71 //AES文件加密函数///

72 int TestAesEncryptFile(std::string in_file_path, std::string out_file_path, char Key[32])73 {74 int encrypt_chunk_size = 16;75

76 ifstream fin(in_file_path.c_str(), ios::binary);77 ofstream fout(out_file_path, ios::binary);78

79 if (!fin)80 {81 cout << "Can not open fin file." <

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

91 unsigned char aes_keybuf[32];92 memset(aes_keybuf, 0, sizeof(aes_keybuf));93 strcpy((char *)aes_keybuf, Key);94 AES_KEY aeskey;95 AES_set_encrypt_key(aes_keybuf, 256, &aeskey);96

97 char *in_data = new char[encrypt_chunk_size + 1];98 char *out_data = new char[encrypt_chunk_size + 1];99 while (!fin.eof())100 {101 fin.read(in_data, encrypt_chunk_size);102 if (fin.gcount()

107 {108 AES_encrypt((const unsigned char *)in_data, (unsigned char *)out_data, &aeskey);109 fout.write(out_data, fin.gcount());110 }111 };112

113 fout.close();114 fin.close();115

116 RELESE_ARRAY(in_data);117 RELESE_ARRAY(out_data);118

119 return 0;120 }121

122 //AES文件解密函数//123 int TestAesDecryptFile(std::string in_file_path, std::string out_file_path, char Key[32])124 {125 int encrypt_chunk_size = 16;126 ifstream fin(in_file_path.c_str(), ios::binary);127 ofstream fout(out_file_path, ios::binary);128

129 if (!fin)130 {131 cout << "Can not open fin file." <

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

141 unsigned char aes_keybuf[32];142 memset(aes_keybuf, 0, sizeof(aes_keybuf));143 strcpy((char *)aes_keybuf, Key);144 AES_KEY aeskey;145 AES_set_decrypt_key(aes_keybuf, 256, &aeskey);146

147 char *in_data = new char[encrypt_chunk_size + 1];148 char *out_data = new char[encrypt_chunk_size + 1];149 int i = 0;150 while (!fin.eof())151 {152 fin.read(in_data, encrypt_chunk_size);153 if (fin.gcount()

158 {159 AES_decrypt((unsigned char *)in_data, (unsigned char *)out_data, &aeskey);160 fout.write(out_data, fin.gcount());161 }162 };163

164 fout.close();165 fin.close();166

167 RELESE_ARRAY(in_data);168 RELESE_ARRAY(out_data);169

170 return 0;171 }172

173 intmain()174 {175 time_t t1, t2, t3, t4;176 t1 =time(NULL);177 printf("加解密起始时间: %s\n", ctime(&t1));178 TestAesEncryptFile("D://model.bin", "D://model.bin.enc", "xxyy1234567890");179 t2 =time(NULL);180 printf("AES256加密成功!\n");181 printf("加密用时: %lld秒\n", (t2 -t1));182 t3 =time(NULL);183 TestAesDecryptFile("D://model.bin.enc", "D://modelnew.bin", "xxyy1234567890");184 t4 =time(NULL);185 printf("AES256解密成功!\n");186 printf("解密用时: %lld秒\n", (t4 -t3));187

188 return 0;189 }

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值