-
Notifications
You must be signed in to change notification settings - Fork 0
/
Digital_Envelope.cpp
168 lines (137 loc) · 5.26 KB
/
Digital_Envelope.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
#include <stdio.h>
#include <iostream>
#include <string>
#include <openssl/md5.h>
#include "base64.h"
#include "Crypto.h"
using namespace std;
string getMessage(const char *prompt)
{
string message;
printf(prompt);
fflush(stdout);
getline(std::cin, message);
return message;
}
void encryptAES(Crypto &crypto, string message)
{
// 使用AES对明文进行加密
unsigned char *encryptedMessage = NULL;
int encryptedMessageLength = crypto.aesEncrypt((const unsigned char *)message.c_str(), message.size() + 1, &encryptedMessage);
if (encryptedMessageLength == -1)
{
fprintf(stderr, "Encryption failed\n");
return;
}
// 输出加密结果
char *b64Message = base64Encode(encryptedMessage, encryptedMessageLength);
printf("Encrypted message: %s\n", b64Message);
}
void encryptokeyByRSA(Crypto &crypto)
{
unsigned char *AESkey = crypto.getAesKey();
string aes_key_str((char *)AESkey); //转为string类型
cout << "aes_key_str:" << aes_key_str << endl;
unsigned char *encryptedMessage = NULL;
unsigned char *encryptedKey;
unsigned char *iv;
size_t encryptedKeyLength;
size_t ivLength;
int encryptedMessageLength = crypto.rsaEncrypt((const unsigned char *)aes_key_str.c_str(), aes_key_str.size() + 1,
&encryptedMessage, &encryptedKey, &encryptedKeyLength, &iv, &ivLength); //进行rsa加密
if (encryptedMessageLength == -1)
{
fprintf(stderr, "Encryption failed\n");
return;
}
cout << "encryptedMessageLength: " << encryptedMessageLength << endl;
//密钥加密结果
char *b64Message = base64Encode(encryptedMessage, encryptedMessageLength);
printf("Encrypted message: %s\n", b64Message);
char *decryptedMessage = NULL;
int decryptedMessageLength = crypto.rsaDecrypt(encryptedMessage, (size_t)encryptedMessageLength,
encryptedKey, encryptedKeyLength, iv, ivLength, (unsigned char **)&decryptedMessage);
if (encryptedMessageLength == -1)
{
fprintf(stderr, "Decryption failed\n");
return;
}
cout << "encryptedMessageLength:" << encryptedMessageLength << endl;
// printf("Decrypted message: %s\n", decryptedMessage);
cout << "Decrypted message: " << decryptedMessage << endl;
char *b64Message_de = base64Encode((const unsigned char *)decryptedMessage, decryptedMessageLength);
printf("Decrypted message: %s\n", b64Message_de);
char *b64Message_de2 = base64Encode((const unsigned char *)decryptedMessage, decryptedMessageLength);
printf("Decrypted message: %s\n", b64Message_de2);
}
void print_AESkey(Crypto &crypto)
{
//输出对称加密密钥
unsigned char *AESkey = crypto.getAesKey();
int AESkey_Length = crypto.getAesKey(&AESkey);
char *b64Message = base64Encode(AESkey, AESkey_Length);
printf("AESkey message: %s\n", b64Message);
}
void encryptRsa(Crypto *crypto)
{
// 获取明文
//string message = getMessage("Message to RSA encrypt: ");
unsigned char *AESkey = crypto->getAesKey();
string message((char *)AESkey);
//cout<<"message"<<message<<endl;
// Encrypt the message with RSA
// +1 on the string length argument because we want to encrypt the NUL terminator too
unsigned char *encryptedMessage = NULL;
unsigned char *encryptedKey;
unsigned char *iv;
size_t encryptedKeyLength;
size_t ivLength;
int encryptedMessageLength = crypto->rsaEncrypt((const unsigned char *)message.c_str(), message.size() + 1,
&encryptedMessage, &encryptedKey, &encryptedKeyLength, &iv, &ivLength);
if (encryptedMessageLength == -1)
{
fprintf(stderr, "Encryption failed\n");
return;
}
//cout << "encryptedMessageLength: " << encryptedMessageLength << endl;
// Print the encrypted message as a base64 string
char *b64Message = base64Encode(encryptedMessage, encryptedMessageLength);
printf("Encrypted message: %s\n", b64Message);
// Decrypt the message
unsigned char *decryptedMessage = NULL;
int decryptedMessageLength = crypto->rsaDecrypt(encryptedMessage, (size_t)encryptedMessageLength,
encryptedKey, encryptedKeyLength, iv, ivLength, (unsigned char **)&decryptedMessage);
if (decryptedMessageLength == -1)
{
fprintf(stderr, "Decryption failed\n");
return;
}
//cout << "decryptedMessageLength:" << decryptedMessageLength << endl;
int AESkey_Length = crypto->getAesKey(&AESkey);
char *b64Message2 = base64Encode(decryptedMessage, AESkey_Length);
printf("AESkey message: %s\n", b64Message2);
// Clean up
free(encryptedMessage);
free(decryptedMessage);
free(encryptedKey);
free(iv);
free(b64Message);
encryptedMessage = NULL;
decryptedMessage = NULL;
encryptedKey = NULL;
iv = NULL;
b64Message = NULL;
}
int main()
{
Crypto crypto;
//输入明文
string message = getMessage("Message to encrypt: ");
//对明文进行AES加密并打印密文
encryptAES(crypto, message);
//输出AES的加密密钥
print_AESkey(crypto);
//使用RSA对对称加密密钥加密
encryptRsa(&crypto);
return 0;
}