摘要:本文主要向大家介绍了 C/C++知识点之C++: 基于OpenSSL的AES256加解密测试,通过具体的内容向大家展示,希望对大家学习C/C++知识点有所帮助。
本文主要向大家介绍了 C/C++知识点之C++: 基于OpenSSL的AES256加解密测试,通过具体的内容向大家展示,希望对大家学习C/C++知识点有所帮助。
暑假要求专业实习,要有周记和工作总结。之前老早就有过写博客的想法,因为可以让自己的学习生涯有迹可循。不过租的服务器即将到期,就不自己建站了。希望通过博客园这个平台记录下自己的成长吧。希望自己能坚持下去,也希望能留下一些对别人有用的东西。
第一篇内容是关于使用OpenSSL库对字符串和文件进行AES256加解密:
环境: 操作系统:windows 10
开发工具:Visual Studio 2015
OpenSSL简介
OpenSSL是一个基于密码学的安全开发包,OpenSSL提供的功能相当强大和全面,囊括了主要的密码算法、常用的密钥和证书封装管理功能以及SSL协议,并提供了丰富的应用程序供测试或其它目的使用。具体包含以下功能:
1.对称加密算法
OpenSSL一共提供了8种对称加密算法,其中7种是分组加密算法,仅有的一种流加密算法是RC4。这7种分组加密算法分别是AES、DES、Blowfish、CAST、IDEA、RC2、RC5,都支持电子密码本模式(ECB)、加密分组链接模式(CBC)、加密反馈模式(CFB)和输出反馈模式(OFB)四种常用的分组密码加密模式。其中,AES使用的加密反馈模式(CFB)和输出反馈模式(OFB)分组长度是128位,其它算法使用的则是64位。事实上,DES算法里面不仅仅是常用的DES算法,还支持三个密钥和两个密钥3DES算法。
2.非对称加密算法
OpenSSL一共实现了4种非对称加密算法,包括DH算法、RSA算法、DSA算法和椭圆曲线算法(EC)。DH算法一般用户密钥交换。RSA算法既可以用于密钥交换,也可以用于数字签名,当然,如果你能够忍受其缓慢的速度,那么也可以用于数据加密。DSA算法则一般只用于数字签名。
3.信息摘要算法
OpenSSL实现了5种信息摘要算法,分别是MD2、MD5、MDC2、SHA(SHA1)和RIPEMD。SHA算法事实上包括了SHA和SHA1两种信息摘要算法,此外,OpenSSL还实现了DSS标准中规定的两种信息摘要算法DSS和DSS1。
4.密钥和证书管理
密钥和证书管理是PKI的一个重要组成部分,OpenSSL为之提供了丰富的功能,支持多种标准。 首先,OpenSSL实现了ASN.1的证书和密钥相关标准,提供了对证书、公钥、私钥、证书请求以及CRL等数据对象的DER、PEM和BASE64的编解码功能。OpenSSL提供了产生各种公开密钥对和对称密钥的方法、函数和应用程序,同时提供了对公钥和私钥的DER编解码功能。并实现了私钥的PKCS#12和PKCS#8的编解码功能。OpenSSL在标准中提供了对私钥的加密保护功能,使得密钥可以安全地进行存储和分发。 在此基础上,OpenSSL实现了对证书的X.509标准编解码、PKCS#12格式的编解码以及PKCS#7的编解码功能。并提供了一种文本数据库,支持证书的管理功能,包括证书密钥产生、请求产生、证书签发、吊销和验证等功能。 事实上,OpenSSL提供的CA应用程序就是一个小型的证书管理中心(CA),实现了证书签发的整个流程和证书管理的大部分机制。
5.SSL和TLS协议
SSL(Secure Sockets Layer 安全套接层)是一种基于Web应用的安全通信协议,最早由Netscape(网景)公司提出。SSL介于TCP协议和应用层 协议之间,主要作用就是将HTTP、FTP等应用层的数据进行加密然后依托可靠的TCP协议在互联网上传输到目的地,其中最典型的应用就是https。
代码展示
1 #include
2 #include
3 #include "iostream"
4 using namespace std;
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 delete P; 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 else
63 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." << endl;
82 return 1;
83 }
84 if (!fout)
85 {
86 cout << "Can not open fout file." << endl;
87 return 1;
88 }
89
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() < encrypt_chunk_size)
103 {
104 fout.write(in_data, fin.gcount());
105 }
106 else
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." << endl;
132 return 1;
133 }
134 if (!fout)
135 {
136 cout << "Can not open fout file." << endl;
137 return 1;
138 }
139
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() < encrypt_chunk_size)
154 {
155 fout.write(in_data, fin.gcount());
156 }
157 else
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 int main()
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 }
测试结果
测试文件:model.bin (920MB)
windows命令行进入工作目录,执行 fc model.bin modelnew.bin >> 1.txt 比较model.bin和modelnew.bin异同:
-----------------------------------------------
至此,AES256加解密测试顺利结束。
本文由职坐标整理并发布,希望对同学们有所帮助。了解更多详情请关注职坐标编程语言C/C+频道!
您输入的评论内容中包含违禁敏感词
我知道了
请输入正确的手机号码
请输入正确的验证码
您今天的短信下发次数太多了,明天再试试吧!
我们会在第一时间安排职业规划师联系您!
您也可以联系我们的职业规划师咨询:
版权所有 职坐标-一站式IT培训就业服务领导者 沪ICP备13042190号-4
上海海同信息科技有限公司 Copyright ©2015 www.zhizuobiao.com,All Rights Reserved.
沪公网安备 31011502005948号