-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPasswordInput.cpp
More file actions
110 lines (86 loc) · 3.29 KB
/
PasswordInput.cpp
File metadata and controls
110 lines (86 loc) · 3.29 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
#include "PasswordInput.h"
#include <iostream>
#include <conio.h>
std::string get_password(const std::string& prompt) {
std::string password;
char ch;
std::cout << prompt;
while ((ch = _getch()) != '\r') { // Enter key
if (ch == '\b') { // Backspace
if (!password.empty()) {
password.pop_back();
std::cout << "\b \b";
}
}
else {
password += ch;
std::cout << '*';
}
}
std::cout << std::endl;
return password;
}
std::vector<unsigned char> encrypt_password(const std::string& password, const unsigned char* key) {
std::vector<unsigned char> encrypted(password.size() + AES_BLOCK_SIZE); // çàïàñ íà padding
int out_len1 = 0, out_len2 = 0;
EVP_CIPHER_CTX* ctx = EVP_CIPHER_CTX_new();
if (!ctx) return {};
if (!EVP_EncryptInit_ex(ctx, EVP_aes_128_ecb(), nullptr, key, nullptr)) {
EVP_CIPHER_CTX_free(ctx);
return {};
}
EVP_CIPHER_CTX_set_padding(ctx, 1); // Âêëþ÷àåì padding
if (!EVP_EncryptUpdate(ctx,
encrypted.data(), &out_len1,
reinterpret_cast<const unsigned char*>(password.data()), password.size())) {
EVP_CIPHER_CTX_free(ctx);
return {};
}
if (!EVP_EncryptFinal_ex(ctx, encrypted.data() + out_len1, &out_len2)) {
EVP_CIPHER_CTX_free(ctx);
return {};
}
encrypted.resize(out_len1 + out_len2); // îáðåçàåì äî íóæíîãî ðàçìåðà
EVP_CIPHER_CTX_free(ctx);
return encrypted;
}
void save_encrypted_password(const std::string& password, const unsigned char* key) {
std::vector<unsigned char> encrypted_password = encrypt_password(password, key);
std::ofstream file("encrypted_password.bin", std::ios::binary);
file.write(reinterpret_cast<const char*>(encrypted_password.data()), encrypted_password.size());
file.close();
}
// Ôóíêöèÿ äëÿ ðàñøèôðîâêè ïàðîëÿ
std::string decrypt_password(const std::vector<unsigned char>& encrypted_password, const unsigned char* key) {
std::vector<unsigned char> decrypted(encrypted_password.size());
int out_len1 = 0, out_len2 = 0;
EVP_CIPHER_CTX* ctx = EVP_CIPHER_CTX_new();
if (!ctx) return {};
if (!EVP_DecryptInit_ex(ctx, EVP_aes_128_ecb(), nullptr, key, nullptr)) {
EVP_CIPHER_CTX_free(ctx);
return {};
}
EVP_CIPHER_CTX_set_padding(ctx, 1); // Âêëþ÷àåì padding
if (!EVP_DecryptUpdate(ctx,
decrypted.data(), &out_len1,
encrypted_password.data(), encrypted_password.size())) {
EVP_CIPHER_CTX_free(ctx);
return {};
}
if (!EVP_DecryptFinal_ex(ctx, decrypted.data() + out_len1, &out_len2)) {
EVP_CIPHER_CTX_free(ctx);
return {};
}
decrypted.resize(out_len1 + out_len2);
EVP_CIPHER_CTX_free(ctx);
return std::string(decrypted.begin(), decrypted.end());
}
// Ôóíêöèÿ äëÿ ÷òåíèÿ çàøèôðîâàííîãî ïàðîëÿ èç ôàéëà
std::string read_encrypted_password(const std::string& filename, const unsigned char* key) {
std::ifstream file(filename, std::ios::binary);
if (!file) {
return ""; // Ôàéë íå íàéäåí, âîçâðàùàåì ïóñòóþ ñòðîêó
}
std::vector<unsigned char> encrypted_password((std::istreambuf_iterator<char>(file)), std::istreambuf_iterator<char>());
return decrypt_password(encrypted_password, key);
}