-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathver.c
82 lines (67 loc) · 2.21 KB
/
ver.c
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
#include <stdio.h>
#include <string.h>
#include <ctype.h>
#include <time.h>
// 암호화 함수
void encrypt(const char* plaintext, const char* key, char* ciphertext) {
size_t textLen = strlen(plaintext);
size_t keyLen = strlen(key);
for (size_t i = 0, j = 0; i < textLen; i++) {
char c = plaintext[i];
if (isalpha(c)) { // 알파벳 문자만 암호화
char base = islower(c) ? 'a' : 'A';
ciphertext[i] = (c - base + (key[j % keyLen] - '0')) % 26 + base;
j++; // 키의 다음 문자로 이동
}
else {
ciphertext[i] = c; // 알파벳이 아니면 그대로 저장
}
}
ciphertext[textLen] = '\0'; // 문자열 종료
}
// 복호화 함수
void decrypt(const char* ciphertext, const char* key, char* plaintext) {
size_t textLen = strlen(ciphertext);
size_t keyLen = strlen(key);
for (size_t i = 0, j = 0; i < textLen; i++) {
char c = ciphertext[i];
if (isalpha(c)) { // 알파벳 문자만 복호화
char base = islower(c) ? 'a' : 'A';
plaintext[i] = (c - base - (key[j % keyLen] - '0') + 26) % 26 + base;
j++; // 키의 다음 문자로 이동
}
else {
plaintext[i] = c; // 알파벳이 아니면 그대로 저장
}
}
plaintext[textLen] = '\0'; // 문자열 종료
}
// 현재 시간을 키로 변환
void generateKeyFromTime(char* key) {
time_t currentTime;
time(¤tTime);
struct tm* localTime = localtime(¤tTime);
// HHMM 형식으로 키 생성
snprintf(key, 5, "%02d%02d", localTime->tm_hour, localTime->tm_min);
}
int main() {
char plaintext[256]; // 사용자 입력 평문
char key[5]; // 시간 기반 키 (HHMM 형식)
char ciphertext[256];
char decrypted[256];
// 평문 입력 받기
printf("평문을 입력하세요: ");
if (fgets(plaintext, sizeof(plaintext), stdin)) {
plaintext[strcspn(plaintext, "\n")] = '\0'; // 개행 문자 제거
}
// 키 생성
generateKeyFromTime(key);
printf("생성된 키: %s\n", key);
// 암호화
encrypt(plaintext, key, ciphertext);
printf("암호화된 텍스트: %s\n", ciphertext);
// 복호화
decrypt(ciphertext, key, decrypted);
printf("복호화된 텍스트: %s\n", decrypted);
return 0;
}