-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtask.cpp
More file actions
245 lines (186 loc) · 7.85 KB
/
Copy pathtask.cpp
File metadata and controls
245 lines (186 loc) · 7.85 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
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
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
#include "seal/seal.h"
#include <iostream>
using namespace std;
using namespace seal;
string evaluatePasswordStrength(Evaluator& evaluator, SecretKey& secret_key, const vector<Ciphertext>& encrypted_password, SEALContext& context, Encryptor& encryptor);
void checkCharInRange(Evaluator& evaluator, Decryptor& decryptor, Encryptor& encryptor, const Ciphertext& encrypted_char, int lower, int upper, bool& result);
int checkCharTypes( Evaluator& evaluator, Decryptor& decryptor, const vector<Ciphertext>& encrypted_password, Encryptor& encryptor);
double checkLength( const vector<Ciphertext>& encrypted_password, Encryptor& encryptor);
const int LENGTH_WEIGHT = 25; // 길이 점수 (총 25점)
const int LOWERCASE_WEIGHT = 15; // 소문자 포함 (15점)
const int UPPERCASE_WEIGHT = 15; // 대문자 포함 (15점)
const int DIGIT_WEIGHT = 15; // 숫자 포함 (15점)
const int SPECIAL_CHAR_WEIGHT = 20; // 특수문자 포함 (20점)
string evaluatePasswordStrength(
Evaluator& evaluator, SecretKey& secret_key,
const vector<Ciphertext>& encrypted_password,
SEALContext& context,
Encryptor& encryptor) {
cout << "[진행] 비밀번호 강도 계산을 시작합니다..." << endl;
Decryptor decryptor(context, secret_key);
// 각 항목별 점수 계산
double length_score = checkLength(encrypted_password, encryptor);
int char_types_score = checkCharTypes(evaluator, decryptor, encrypted_password, encryptor);
cout << "[진행] 길이 점수(" << length_score << ")와 문자유형 점수(" << char_types_score << ")를 합산합니다." << endl;
// 총점 계산
int score = length_score + char_types_score;
cout << "[진행] 총점: " << score << endl;
string score_str = to_string(score);
string result;
// 강도 등급 판정 및 결과 반환
if (score >= 80) {
result = "매우 강함 (점수: " + score_str + ")";
}
else if (score >= 60) {
result = "강함 (점수: " + score_str + ")";
}
else if (score >= 40) {
result = "보통 (점수: " + score_str + ")";
}
else {
result = "약함 (점수: " + score_str + ")";
}
cout << "[진행] 비밀번호 강도 계산 완료 -> " << result << endl;
return result;
}
// 암호화된 문자가 특정 ASCII 범위에 있는지 확인하는 보조 함수
void checkCharInRange(
Evaluator& evaluator, Decryptor& decryptor,
Encryptor& encryptor,
const Ciphertext& encrypted_char, int lower, int upper, bool& result) {
// 하한 체크
Plaintext lower_plain(to_string(static_cast<int>(lower)));
Ciphertext lower_bound;
encryptor.encrypt(lower_plain, lower_bound);
Ciphertext lower_check;
evaluator.sub(encrypted_char, lower_bound, lower_check);
// 상한 체크
Plaintext upper_plain(to_string(static_cast<int>(upper)));
Ciphertext upper_bound;
encryptor.encrypt(upper_plain, upper_bound);
Ciphertext upper_check;
evaluator.sub(upper_bound, encrypted_char, upper_check);
Plaintext lower_result;
Plaintext upper_result;
// 음수를 0으로 양수를 1로 해서
// 곱하면 양수 양수만 1이 나온다.
// 음수를 0으로 양수를 1로 만드는 다항함수가 필요하다.
// 다항식 보간법
// 모듈러 정수(p)에서 연산에서 다항식 찾기
// 다항 함수를 찾자
// 집가서 x 1 2 3 y를 3 0 2 로 바꾸는거 해보기 이런느낌
// 3 7 11 등 모듈러를 소수로 하면 1로 만드는 수가 있다.
// 127보다 큰 모듈러 소수로 하면 1로 전부 만들 수 있다.
// 모듈러 정수에서 역원 찾기하면 알고리즘
{
// 사용자한테 보내서 값을 체크 후 결과를 받아오는 코드.
decryptor.decrypt(lower_check, lower_result);
decryptor.decrypt(upper_check, upper_result);
}
int64_t lower_val = 0;
int64_t upper_val = 0;
if (lower_result.data() && lower_result.coeff_count() > 0) {
lower_val = static_cast<int64_t>(lower_result.data()[0]);
}
if (upper_result.data() && upper_result.coeff_count() > 0) {
upper_val = static_cast<int64_t>(upper_result.data()[0]);
}
if (lower_val >= 0 && upper_val >= 0) {
result = true;
}
else {
result = false;
}
}
// 암호화된 비밀번호의 문자 유형 점수 계산 (소문자, 대문자, 숫자, 특수문자)
int checkCharTypes(
Evaluator& evaluator, Decryptor& decryptor,
const vector<Ciphertext>& encrypted_password,
Encryptor& encryptor) {
cout << "[진행] 문자 유형 체크를 시작합니다..." << endl;
bool has_lowercase = false;
bool has_uppercase = false;
bool has_digit = false;
bool has_special = false;
// 각 암호화된 문자에 대해 문자 유형 검사
for (auto& encrypted_char : encrypted_password) {
// 소문자 검사 (ASCII 97-122: a-z)
checkCharInRange(evaluator,decryptor, encryptor, encrypted_char, 97, 122, has_lowercase);
// 대문자 검사 (ASCII 65-90: A-Z)
checkCharInRange(evaluator, decryptor, encryptor, encrypted_char, 65, 90, has_uppercase);
// 숫자 검사 (ASCII 48-57: 0-9)
checkCharInRange(evaluator, decryptor, encryptor, encrypted_char, 48, 57, has_digit);
// 특수문자 검사 (여러 ASCII 범위)
checkCharInRange(evaluator, decryptor, encryptor, encrypted_char, 33, 47, has_special);
}
// 점수 합산
int total_score = 0;
// 소문자 포함 시 점수 추가
if (has_lowercase) { total_score += LOWERCASE_WEIGHT; }
// 대문자 포함 시 점수 추가
if (has_uppercase) { total_score += UPPERCASE_WEIGHT; }
// 숫자 포함 점수
if (has_digit) { total_score += DIGIT_WEIGHT; }
// 특수문자 포함 점수
if (has_special) { total_score += SPECIAL_CHAR_WEIGHT; }
cout << "[진행] 문자 유형 체크 완료" << endl;
cout << " 소문자 포함: " << (has_lowercase ? "예" : "아니오") << endl;
cout << " 대문자 포함: " << (has_uppercase ? "예" : "아니오") << endl;
cout << " 숫자 포함: " << (has_digit ? "예" : "아니오") << endl;
cout << " 특수문자 포함: " << (has_special ? "예" : "아니오") << endl;
cout << "[진행] 문자 유형 점수: " << total_score << endl;
return total_score;
}
// 암호화된 비밀번호의 길이 점수 계산
double checkLength(
const vector<Ciphertext>& encrypted_password,
Encryptor& encryptor) {
// 길이 계산
double length = encrypted_password.size();
// 점수 계산 로직
double length_score;
if (length < 8) {
length_score = LENGTH_WEIGHT * (length / 8);
}
else if (length < 12) {
length_score = LENGTH_WEIGHT * (length / 12);
}
else {
length_score = LENGTH_WEIGHT;
}
return length_score;
}
int main() {
EncryptionParameters parms(scheme_type::bfv);
size_t poly_modulus_degree = 8192;
parms.set_poly_modulus_degree(poly_modulus_degree);
parms.set_coeff_modulus(CoeffModulus::BFVDefault(poly_modulus_degree));
parms.set_plain_modulus(PlainModulus::Batching(poly_modulus_degree, 30));
SEALContext context(parms);
KeyGenerator keygen(context);
SecretKey secret_key = keygen.secret_key();
PublicKey public_key;
RelinKeys relin_keys;
keygen.create_public_key(public_key);
keygen.create_relin_keys(relin_keys);
Encryptor encryptor(context, public_key);
Decryptor decryptor(context, secret_key);
Evaluator evaluator(context);
cout << " -------------------------- " << endl;
cout << " 비밀번호 검증 프로그램 " << endl;
cout << " -------------------------- " << endl;
// 테스트할 비밀번호
string test_password = "P@ssw0rd13";
vector<Ciphertext> encrypted_password;
for (char c : test_password) {
// 문자의 ASCII 값을 문자열로 변환 후 Plaintext 객체로 생성
Plaintext plain(to_string(static_cast<int>(c)));
Ciphertext encrypted;
encryptor.encrypt(plain, encrypted);
// 암호화된 문자를 벡터에 추가
encrypted_password.push_back(encrypted);
}
// 암호화된 비밀번호 강도 평가 (서버 측)
string strength = evaluatePasswordStrength(evaluator, secret_key,encrypted_password, context, encryptor);
cout << "비밀번호 강도: " << strength << endl;
}