Repository navigation
Expand file tree
/
Copy pathmain.cpp
More file actions
118 lines (91 loc) · 4.82 KB
/
Copy pathmain.cpp
File metadata and controls
118 lines (91 loc) · 4.82 KB
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
#include <iostream>
#include <string>
#include <chrono>
#include "RSA.h"
using namespace std;
using namespace std::chrono;
int main() {
cout << "=== RSA Algorithm Implementation ===" << endl << endl;
// Test key generation with different bit lengths
vector<int> testBitLengths = {128, 256, 512, 768, 1024, 2048, 4096};
cout << "=== Parallel RSA Key Generation Performance Test ===" << endl;
cout << "Using multi-threading for parallel prime generation (p and q generated simultaneously)" << endl << endl;
for (int bitLength : testBitLengths) {
cout << "Testing RSA-" << bitLength << " key generation..." << endl;
// Actual test run
// auto start = high_resolution_clock::now();
try {
auto start = high_resolution_clock::now();
RSA::KeyPair keys = RSA::generateKeyPair(bitLength);
auto end = high_resolution_clock::now();
auto duration = duration_cast<milliseconds>(end - start);
cout << "✓ Key generation completed in " << duration.count() << " ms" << endl;
if (duration.count() < 1000) {
cout << " (Performance target: < 1000 ms for RSA-768) ✓" << endl;
}
BigInteger n_copy = keys.n;
BigInteger e_copy = keys.e;
BigInteger d_copy = keys.d;
cout << "Public Key (n): " << string(n_copy) << endl;
cout << "Public Exponent (e): " << string(e_copy) << endl;
string d_str = string(d_copy);
cout << "Private Exponent (d): " << d_str.substr(0, min(50, (int)d_str.length())) << "..." << endl;
cout << endl;
// Test encryption and decryption
string testMessage = "Hello, RSA! This is a test message for RSA encryption.";
cout << "Original message: " << testMessage << endl;
string encrypted = RSA::encrypt(testMessage, keys.n, keys.e);
cout << "Encrypted: " << encrypted.substr(0, 100) << "..." << endl;
string decrypted = RSA::decrypt(encrypted, keys.n, keys.d);
cout << "Decrypted: " << decrypted << endl;
if (decrypted == testMessage) {
cout << "✓ Encryption/Decryption test PASSED" << endl;
} else {
cout << "✗ Encryption/Decryption test FAILED" << endl;
}
cout << endl;
// Test digital signature
string messageToSign = "This is a message to sign.";
cout << "Message to sign: " << messageToSign << endl;
string signature = RSA::sign(messageToSign, keys.n, keys.d);
cout << "Signature: " << signature.substr(0, 100) << "..." << endl;
bool verified = RSA::verify(messageToSign, signature, keys.n, keys.e);
if (verified) {
cout << "✓ Signature verification PASSED" << endl;
} else {
cout << "✗ Signature verification FAILED" << endl;
}
// Test with wrong message
bool wrongVerified = RSA::verify("Wrong message", signature, keys.n, keys.e);
if (!wrongVerified) {
cout << "✓ Signature verification (wrong message) correctly REJECTED" << endl;
} else {
cout << "✗ Signature verification (wrong message) incorrectly ACCEPTED" << endl;
}
} catch (const exception& e) {
cout << "Error: " << e.what() << endl;
}
cout << "\n" << string(60, '-') << "\n\n";
}
// Test with user input
cout << "Enter a message to encrypt (or press Enter to skip): ";
string userMessage;
getline(cin, userMessage);
if (!userMessage.empty()) {
cout << "\nGenerating keys for your message..." << endl;
auto start = high_resolution_clock::now();
RSA::KeyPair userKeys = RSA::generateKeyPair(768);
auto end = high_resolution_clock::now();
auto duration = duration_cast<milliseconds>(end - start);
cout << "Keys generated in " << duration.count() << " ms" << endl;
string encrypted = RSA::encrypt(userMessage, userKeys.n, userKeys.e);
cout << "Encrypted message: " << encrypted << endl;
string decrypted = RSA::decrypt(encrypted, userKeys.n, userKeys.d);
cout << "Decrypted message: " << decrypted << endl;
string signature = RSA::sign(userMessage, userKeys.n, userKeys.d);
cout << "Digital signature: " << signature << endl;
bool verified = RSA::verify(userMessage, signature, userKeys.n, userKeys.e);
cout << "Signature verified: " << (verified ? "Yes" : "No") << endl;
}
return 0;
}