-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest.cpp
More file actions
305 lines (248 loc) · 10.9 KB
/
Copy pathtest.cpp
File metadata and controls
305 lines (248 loc) · 10.9 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
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
#include "seal/seal.h"
#include <iostream>
using namespace std;
using namespace seal;
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점)
// 암호화된 문자가 특정 ASCII 범위에 있는지 확인하는 함수
void checkCharInRange(const Ciphertext& encrypted_char, int lower, int upper, Ciphertext& result,
const SEALContext& context, const PublicKey& public_key,
Evaluator& evaluator)
{
Encryptor encryptor(context, public_key);
// 범위 체크를 위한 임시 변수
Ciphertext temp_result;
encryptor.encrypt_zero(temp_result);
// 하한 체크: encrypted_char >= lower
Plaintext lower_plain(to_string(lower));
Ciphertext lower_bound;
encryptor.encrypt(lower_plain, lower_bound);
evaluator.sub(encrypted_char, lower_bound, lower_bound); // encrypted_char - lower
// lower_bound가 0 이상인지를 확인 (encrypted_char >= lower)
Ciphertext lower_check;
encryptor.encrypt_zero(lower_check);
evaluator.negate(lower_bound, lower_check); // 부호 반전하여 양수로 만들어줌
evaluator.add(lower_check, lower_check, lower_check); // 음수인지 확인: 0 이상이면 양수, 아니면 음수
// 상한 체크: encrypted_char <= upper
Plaintext upper_plain(to_string(upper));
Ciphertext upper_bound;
encryptor.encrypt(upper_plain, upper_bound);
evaluator.sub(upper_bound, encrypted_char, upper_bound); // upper - encrypted_char
// upper_bound가 0 이상인지를 확인 (encrypted_char <= upper)
Ciphertext upper_check;
encryptor.encrypt_zero(upper_check);
evaluator.negate(upper_bound, upper_check); // 부호 반전하여 양수로 만들어줌
evaluator.add(upper_check, upper_check, upper_check); // 음수인지 확인: 0 이상이면 양수, 아니면 음수
//두 조건이 모두 만족하는지 (논리 AND 연산)
Ciphertext both_conditions;
encryptor.encrypt_zero(both_conditions);
evaluator.multiply(lower_check, upper_check, both_conditions); // 둘 다 양수일 때만 1
//결과 업데이트 (둘 다 양수일 때만 temp_result가 1)
evaluator.add(result, both_conditions, result);
}
// 암호화된 비밀번호의 길이 점수 계산
Ciphertext checkLength(const vector<Ciphertext>& encrypted_password,
const SEALContext& context, const PublicKey& public_key, const SecretKey& secret_key)
{
Encryptor encryptor(context, public_key);
// 길이 계산
int length = static_cast<int>(encrypted_password.size());
// 점수 계산
int score;
if (length < 8) {
score = length * (LENGTH_WEIGHT / 8);
}
else if (length < 12) {
score = static_cast<int>(LENGTH_WEIGHT * 0.7);
}
else {
score = LENGTH_WEIGHT;
}
// 점수 암호화하여 반환
Plaintext length_score(to_string(score));
Ciphertext encrypted_score;
encryptor.encrypt(length_score, encrypted_score);
// 디버그용: 중간 점수 복호화 후 출력
Decryptor decryptor(context, secret_key);
Plaintext decrypted_score;
decryptor.decrypt(encrypted_score, decrypted_score);
cout << "길이 점수 (복호화): " << decrypted_score.to_string() << endl;
return encrypted_score;
}
// 암호화된 비밀번호의 문자 유형 점수 계산
Ciphertext checkCharTypes(const vector<Ciphertext>& encrypted_password,
const SEALContext& context, const PublicKey& public_key,
Evaluator& evaluator, const RelinKeys& relin_keys, const SecretKey& secret_key)
{
Encryptor encryptor(context, public_key);
// 문자 유형 플래그 초기화
Ciphertext has_lowercase;
encryptor.encrypt_zero(has_lowercase);
Ciphertext has_uppercase;
encryptor.encrypt_zero(has_uppercase);
Ciphertext has_digit;
encryptor.encrypt_zero(has_digit);
Ciphertext has_special;
encryptor.encrypt_zero(has_special);
// 각 문자 유형 검사
for (const auto& encrypted_char : encrypted_password) {
// 소문자 검사 (ASCII 97-122: a-z)
checkCharInRange(encrypted_char, 97, 122, has_lowercase, context, public_key, evaluator);
// 대문자 검사 (ASCII 65-90: A-Z)
checkCharInRange(encrypted_char, 65, 90, has_uppercase, context, public_key, evaluator);
// 숫자 검사 (ASCII 48-57: 0-9)
checkCharInRange(encrypted_char, 48, 57, has_digit, context, public_key, evaluator);
// 특수문자 검사 (ASCII 33-47)
checkCharInRange(encrypted_char, 33, 47, has_special, context, public_key, evaluator);
}
// 점수 합산
Ciphertext total_score;
encryptor.encrypt_zero(total_score);
// 소문자 점수
Plaintext lowercase_score(to_string(LOWERCASE_WEIGHT));
Ciphertext encrypted_lowercase_score;
encryptor.encrypt(lowercase_score, encrypted_lowercase_score);
evaluator.multiply(encrypted_lowercase_score, has_lowercase, encrypted_lowercase_score);
evaluator.relinearize_inplace(encrypted_lowercase_score, relin_keys);
evaluator.add_inplace(total_score, encrypted_lowercase_score);
// 디버그용: 중간 결과 복호화 후 출력
Decryptor decryptor(context, secret_key);
Plaintext decrypted_score1;
decryptor.decrypt(total_score, decrypted_score1);
cout << "소문자 점수 (복호화): " << decrypted_score1.to_string() << endl;
// 대문자 점수
Plaintext uppercase_score(to_string(UPPERCASE_WEIGHT));
Ciphertext encrypted_uppercase_score;
encryptor.encrypt(uppercase_score, encrypted_uppercase_score);
evaluator.multiply(encrypted_uppercase_score, has_uppercase, encrypted_uppercase_score);
evaluator.relinearize_inplace(encrypted_uppercase_score, relin_keys);
evaluator.add_inplace(total_score, encrypted_uppercase_score);
// 디버그용: 중간 결과 복호화 후 출력
Plaintext decrypted_score2;
decryptor.decrypt(total_score, decrypted_score2);
cout << "대문자 점수 (복호화): " << decrypted_score2.to_string() << endl;
// 숫자 점수
Plaintext digit_score(to_string(DIGIT_WEIGHT));
Ciphertext encrypted_digit_score;
encryptor.encrypt(digit_score, encrypted_digit_score);
evaluator.multiply(encrypted_digit_score, has_digit, encrypted_digit_score);
evaluator.relinearize_inplace(encrypted_digit_score, relin_keys);
evaluator.add_inplace(total_score, encrypted_digit_score);
// 디버그용: 중간 결과 복호화 후 출력
Plaintext decrypted_score3;
decryptor.decrypt(total_score, decrypted_score3);
cout << "숫자 점수 (복호화): " << decrypted_score3.to_string() << endl;
// 특수문자 점수
Plaintext special_score(to_string(SPECIAL_CHAR_WEIGHT));
Ciphertext encrypted_special_score;
encryptor.encrypt(special_score, encrypted_special_score);
evaluator.multiply(encrypted_special_score, has_special, encrypted_special_score);
evaluator.relinearize_inplace(encrypted_special_score, relin_keys);
evaluator.add_inplace(total_score, encrypted_special_score);
// 디버그용: 중간 결과 복호화 후 출력
Plaintext decrypted_score4;
decryptor.decrypt(total_score, decrypted_score4);
cout << "특수문자 점수 (복호화): " << decrypted_score4.to_string() << endl;
// 디버그용: 중간 결과 복호화 후 출력
Plaintext decrypted_score;
decryptor.decrypt(total_score, decrypted_score);
cout << "문자 유형 점수 (복호화): " << decrypted_score.to_string() << endl;
return total_score;
}
// 암호화된 비밀번호의 연속 패턴 검사
Ciphertext checkSequencePatterns(const vector<Ciphertext>& encrypted_password,
const SEALContext& context, const PublicKey& public_key, const SecretKey& secret_key)
{
// 패턴 감지는 복잡하므로 기본 패널티 점수 반환
Encryptor encryptor(context, public_key);
Plaintext penalty(to_string(0));
Ciphertext encrypted_penalty;
encryptor.encrypt(penalty, encrypted_penalty);
// 디버그용: 중간 패널티 값 복호화 후 출력
Decryptor decryptor(context, secret_key);
Plaintext decrypted_penalty;
decryptor.decrypt(encrypted_penalty, decrypted_penalty);
cout << "연속 패턴 점수 (복호화): " << decrypted_penalty.to_string() << endl;
return encrypted_penalty;
}
// 암호화된 비밀번호의 전체 강도 점수 계산 및 등급 결정
string evaluatePasswordStrength(const vector<Ciphertext>& encrypted_password,
const SEALContext& context, const PublicKey& public_key,
const SecretKey& secret_key, Evaluator& evaluator,
const RelinKeys& relin_keys)
{
// 각 항목별 점수 계산
Ciphertext length_score = checkLength(encrypted_password, context, public_key, secret_key);
Ciphertext char_types_score = checkCharTypes(encrypted_password, context, public_key, evaluator, relin_keys, secret_key);
Ciphertext sequence_penalty = checkSequencePatterns(encrypted_password, context, public_key, secret_key);
// 총점 계산
Ciphertext total_score;
{
Encryptor encryptor(context, public_key);
encryptor.encrypt_zero(total_score);
}
evaluator.add(length_score, char_types_score, total_score);
evaluator.sub_inplace(total_score, sequence_penalty);
// 디버그용: 최종 점수 복호화 후 출력
Decryptor decryptor(context, secret_key);
Plaintext decrypted_score;
decryptor.decrypt(total_score, decrypted_score);
cout << "최종 점수 (복호화): " << decrypted_score.to_string() << endl;
// 점수 해석
string score_str = decrypted_score.to_string();
int score = stoi(score_str);
// 등급 판정
if (score >= 80) {
return "매우 강함 (점수: " + score_str + ")";
}
else if (score >= 60) {
return "강함 (점수: " + score_str + ")";
}
else if (score >= 40) {
return "보통 (점수: " + score_str + ")";
}
else {
return "약함 (점수: " + score_str + ")";
}
}
// 메인 함수
int main()
{
// SEAL 암호화 관련 변수들을 메인 함수 내부로 이동
// SEAL 파라미터 설정
EncryptionParameters parms(scheme_type::bfv);
size_t poly_modulus_degree = 4096;
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, 20));
// SEALContext 직접 생성
SEALContext context(parms);
// 키 생성
KeyGenerator keygen(context);
SecretKey secret_key = keygen.secret_key();
PublicKey public_key;
keygen.create_public_key(public_key);
RelinKeys relin_keys;
keygen.create_relin_keys(relin_keys);
// Evaluator 초기화
Evaluator evaluator(context);
// 비밀번호 암호화 예시
Encryptor encryptor(context, public_key);
vector<Ciphertext> encrypted_password;
// 예시 비밀번호 "P@ssw0rd" 암호화
string password = "P@ssw0rd";
for (char c : password) {
Plaintext plain(to_string(static_cast<int>(c)));
Ciphertext encrypted;
encryptor.encrypt(plain, encrypted);
encrypted_password.push_back(encrypted);
}
// 비밀번호 강도 평가
string strength = evaluatePasswordStrength(encrypted_password, context, public_key,
secret_key, evaluator, relin_keys);
cout << "비밀번호 강도: " << strength << endl;
return 0;
}