-
Notifications
You must be signed in to change notification settings - Fork 0
/
crypto_example.cpp
145 lines (111 loc) · 3.66 KB
/
crypto_example.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
#include "crypto_example.h"
using std::string;
using std::cin;
int main() {
Crypto crypto;
#ifdef PRINT_KEYS
printKeys(&crypto);
#endif
while(!std::cin.eof()) {
encryptRsa(&crypto);
encryptAes(&crypto);
}
return 0;
}
void encryptRsa(Crypto *crypto) {
// 获取明文
string message = getMessage("Message to RSA encrypt: ");
// 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;
}
// Print the encrypted message as a base64 string
char* b64Message = base64Encode(encryptedMessage, encryptedMessageLength);
printf("Encrypted message: %s\n", b64Message);
// Decrypt the message
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;
}
printf("Decrypted message: %s\n", decryptedMessage);
// Clean up
free(encryptedMessage);
free(decryptedMessage);
free(encryptedKey);
free(iv);
free(b64Message);
encryptedMessage = NULL;
decryptedMessage = NULL;
encryptedKey = NULL;
iv = NULL;
b64Message = NULL;
}
void encryptAes(Crypto *crypto) {
// Get the message to encrypt
string message = getMessage("Message to AES encrypt: ");
// Encrypt the message with 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;
}
// Print the encrypted message as a base64 string
char *b64Message = base64Encode(encryptedMessage, encryptedMessageLength);
printf("Encrypted message: %s\n", b64Message);
// Decrypt the message
char *decryptedMessage = NULL;
int decryptedMessageLength = crypto->aesDecrypt(encryptedMessage, (size_t)encryptedMessageLength, (unsigned char**)&decryptedMessage);
if(decryptedMessageLength == -1) {
fprintf(stderr, "Decryption failed\n");
return;
}
printf("Decrypted message: %s\n", decryptedMessage);
// Clean up
free(encryptedMessage);
free(decryptedMessage);
free(b64Message);
encryptedMessage = NULL;
decryptedMessage = NULL;
b64Message = NULL;
}
string getMessage(const char *prompt) {
string message;
printf(prompt);
fflush(stdout);
getline(std::cin, message);
return message;
}
void printKeys(Crypto *crypto) {
// Write the RSA keys to stdout
crypto->writeKeyToFile(stdout, KEY_SERVER_PRI);
crypto->writeKeyToFile(stdout, KEY_SERVER_PUB);
crypto->writeKeyToFile(stdout, KEY_CLIENT_PUB);
// Write the AES key to stdout in hex
unsigned char *aesKey;
size_t aesKeyLength = crypto->getAesKey(&aesKey);
printBytesAsHex(aesKey, aesKeyLength, "AES Key");
// Write the AES IV to stdout in hex
unsigned char *aesIv;
size_t aesIvLength = crypto->getAesIv(&aesIv);
printBytesAsHex(aesIv, aesIvLength, "AES IV");
}
void printBytesAsHex(unsigned char *bytes, size_t length, const char *message) {
printf("%s: ", message);
for(unsigned int i=0; i<length; i++) {
printf("%02hhx", bytes[i]);
}
puts("");
}