500 likes | 916 Views
STEP_ 플레이페어 암호학. PLAYFAIR CIPHER. [ 플레이페어 암호 ]. 경일대학교 컴퓨터공학과 20092213 박동혁. STEP_ 플레이페어 암호학. What is it?. NICE. STEP_ 플레이페어 암호학. What is it?. NI CE. LA. DF. STEP_ 플레이페어 암호학. What is it?. NI CE. LADF. STEP_ 플레이페어 암호학. C Source. #include< stdio.h > #include< stdlib.h >
E N D
STEP_플레이페어암호학 PLAYFAIR CIPHER [ 플레이페어 암호 ] 경일대학교 컴퓨터공학과 20092213 박동혁
STEP_플레이페어암호학 What is it? NICE
STEP_플레이페어암호학 What is it? NI CE LA DF
STEP_플레이페어암호학 What is it? NI CE LADF
STEP_플레이페어암호학 C Source #include<stdio.h> #include<stdlib.h> #include<conio.h> #include<time.h> #include<string.h> #include<windows.h> intEncDec(char*,char*,int); char* mkMatrix(char*); char* mkText(char*,int); int print2clip(char*); int main(){ intch=0; char key[25], text[100]; printf("1. Enc, 2.Dec :: "); scanf("%d", &ch); fflush(stdin); printf("Enter Key :: "); gets_s(key); fflush(stdin); printf("Enter Text :: "); gets_s(text); strupr(text); strcpy(text,mkText(text,ch)); strcpy(key,mkMatrix(key)); EncDec(key, text, ch); } char* mkText(char text[], intch){ char text_tmp[100]={""}; int count=0, cLen=0; /* 공백제거 */ for(inti=0;i<100;i++){ if(text[i]>='A'&&text[i]<='Z'){ text_tmp[count++]=text[i]; } } /* 연속되는 글자 구분 */ count=0; for(unsigned inti=0;i<strlen(text_tmp)+1;i++){ if(text_tmp[i]==text_tmp[i+1]){ text[count++]=text_tmp[i]; text[count++]='X'; }else{ text[count++]=text_tmp[i]; text[count++]=text_tmp[i+1]; i++; } } if(ch==1){ /* 홀수 시 문자 추가 */ srand(time_t(NULL)); cLen=strlen(text); if(cLen%2) text[cLen++]=rand()%26+'A'; text[cLen]='\0'; } return text; } char* mkMatrix(char key[]){ char putChar='A', key_tmp[25]={""}, keyWj[25]={""}; int count=0, ck=0; intcLen=0; strupr(key); cLen=strlen(key); /* KEY 내 동일문자 삭제 */ for(inti=0;i<cLen;i++){ ck = 0; for(int j=0;j<cLen;j++){ if(key[i]==key_tmp[j]) ck=1; } if(ck == 0) key_tmp[count++]=key[i]; } strcpy(key,key_tmp); strcpy(keyWj,key_tmp); cLen=strlen(key); for(inti=0;i<cLen;i++){ if(keyWj[i]=='I') keyWj[cLen]='J'; else if(keyWj[i]=='J') keyWj[cLen]='I'; } /* 행렬만들기 */ for(inti=cLen; i<25; i++){ for(int j=0;j<cLen+1;j++){ if(putChar==keyWj[j]){ putChar++; j=-1; } if(putChar!=keyWj[j]){ key[i]=putChar; } } if(key[i]>='A'&&key[i]<='Z') putChar++; } return key; } intEncDec(char key[], char text[], intch){ char res[100]; char (*mt)[5] = (char(*)[5]) key; printf("\n"); /* 5X5 행렬 출력 */ for(inti=0; i<5; i++){ for(int j=0;j<5;j++){ printf("%2c",mt[i][j]); } printf("\n"); } /* 플레이페어암호학 규칙 */ int s[2][2]={0,}; for(unsigned int a=0;a<strlen(text);a+=2){ for(inti=0;i<5;i++){ for(int j=0;j<5;j++){ if(mt[i][j]==text[a]){ s[0][0]=i; s[0][1]=j; } if(mt[i][j]==text[a+1]){ s[1][0]=i; s[1][1]=j; } if(text[a]=='J'&&mt[i][j]=='I'){ s[0][0]=i; s[0][1]=j; break; } if(text[a+1]=='J'&&mt[i][j]=='I'){ s[1][0]=i; s[1][1]=j; break; } } } int v=0; if(ch==1) v=1; else if(ch==2) v=4; if(s[0][1]==s[1][1]){ res[a]=mt[(s[0][0]+v)%5][s[0][1]]; res[a+1]=mt[(s[1][0]+v)%5][s[1][1]]; }else if(s[0][0]==s[1][0]){ res[a]=mt[s[0][0]][(s[0][1]+v)%5]; res[a+1]=mt[s[1][0]][(s[1][1]+v)%5]; }else{ res[a]=mt[s[0][0]][s[1][1]]; res[a+1]=mt[s[1][0]][s[0][1]]; } } printf("\nTXT=%s\n",text); /* 암호문 출력 */ res[strlen(text)]='\0'; for(unsigned inti=0;i<strlen(text);i++){ printf("%c",text[i]); if(i%2) printf(" "); } printf("\n"); for(unsigned inti=0;i<strlen(res);i++){ printf("%c",res[i]); if(i%2) printf(" "); } printf("\n"); print2clip(res); printf("RES=%s\n",res); return 0; } int print2clip(char *source){ int ok = OpenClipboard(NULL); if (!ok) return 0; HGLOBAL clipbuffer; char * buffer; EmptyClipboard(); clipbuffer = GlobalAlloc(GMEM_DDESHARE, strlen(source)+1); buffer = (char*)GlobalLock(clipbuffer); strcpy(buffer, source); GlobalUnlock(clipbuffer); SetClipboardData(CF_TEXT,clipbuffer); CloseClipboard(); return 1; }
STEP_플레이페어암호학 C Source int v=0; if(ch==1) v=1; if(ch==2) v=4; if(s[0][1]==s[1][1]){ res[a]=mt[(s[0][0]+v)%5][s[0][1]]; res[a+1]=mt[(s[1][0]+v)%5][s[1][1]]; }elseif(s[0][0]==s[1][0]){ res[a]=mt[s[0][0]][(s[0][1]+v)%5]; res[a+1]=mt[s[1][0]][(s[1][1]+v)%5]; }else{ res[a]=mt[s[0][0]][s[1][1]]; res[a+1]=mt[s[1][0]][s[0][1]]; } } printf("\nTXT=%s\n",text); /* 암호문 출력 */ res[strlen(text)]='\0'; for(unsigned int i=0;i<strlen(text);i++){ printf("%c",text[i]); if(i%2) printf(" "); } printf("\n"); for(unsigned int i=0;i<strlen(res);i++){ printf("%c",res[i]); if(i%2) printf(" "); } printf("\n"); printf("RES=%s\n",res); print2clip(res); return 0; } #include<stdio.h>#include<stdlib.h>#include<conio.h>#include<time.h>#include<string.h>#include<windows.h> int EncDec(char*,char*,int);char* mkMatrix(char*);char* mkText(char*,int);int print2clip(char*); int main(){ int ch=0; char key[25], text[100]; printf("1. Enc, 2.Dec :: "); scanf("%d", &ch); fflush(stdin); printf("Enter Key :: "); gets_s(key); fflush(stdin); printf("Enter Text :: "); gets_s(text); strupr(text); strcpy(text,mkText(text,ch)); strcpy(key,mkMatrix(key)); EncDec(key, text, ch);} int EncDec(char key[], char text[], int ch){ char res[100]; char (*mt)[5] = (char(*)[5]) key; printf("\n"); /* 5X5 행렬 출력 */ for(int i=0; i<5; i++){ for(int j=0;j<5;j++){ printf("%2c",mt[i][j]); } printf("\n"); } /* 플레이페어 암호학 규칙 */ int s[2][2]={0,}; for(unsigned int a=0;a<strlen(text);a+=2){ for(int i=0;i<5;i++){ for(int j=0;j<5;j++){ if(mt[i][j]==text[a]){ s[0][0]=i; s[0][1]=j; } if(mt[i][j]==text[a+1]){ s[1][0]=i; s[1][1]=j; } if(text[a]=='J'){// J를 I로 처리 if(mt[i][j]=='I'){ s[0][0]=i; s[0][1]=j; break; } } if(text[a+1]=='J'){// J를 I로 처리 if(mt[i][j]=='I'){ s[1][0]=i; s[1][1]=j; break; } } } } Swapper Main Fuction Print
STEP_플레이페어암호학 C Source char* mkText(char text[], int ch){ char text_tmp[100]={""}; int count=0, cLen=0; /* 공백제거 */ for(int i=0;i<100;i++){ if(text[i]>='A'&&text[i]<='Z'){ text_tmp[count++]=text[i]; } } /* 연속되는 글자 구분 */ count=0; for(unsigned int i=0;i<strlen(text_tmp)+1;i++){ if(text_tmp[i]==text_tmp[i+1]){ text[count++]=text_tmp[i]; text[count++]='X'; }else{ text[count++]=text_tmp[i]; text[count++]=text_tmp[i+1]; i++; } } if(ch==1){ /* 홀수 시 문자 추가 */ srand(time_t(NULL)); cLen=strlen(text); if(cLen%2) text[cLen++]=rand()%26+'A'; text[cLen]='\0'; } return text; } char* mkMatrix(char key[]){ char putChar='A', key_tmp[25]={""}, keyWj[25]={""}; int count=0, ck=0; int cLen=0; strupr(key); cLen=strlen(key); /* KEY 내 동일문자 삭제 */ for(int i=0;i<cLen;i++){ ck = 0; for(int j=0;j<cLen;j++){ if(key[i]==key_tmp[j]) ck=1; } if(ck == 0) key_tmp[count++]=key[i]; } strcpy(key,key_tmp); strcpy(keyWj,key_tmp); cLen=strlen(key); for(int i=0;i<cLen;i++){ if(keyWj[i]=='I') keyWj[cLen]='J'; else if(keyWj[i]=='J') keyWj[cLen]='I'; } /* 행렬만들기 */ for(int i=cLen; i<25; i++){ for(int j=0;j<cLen+1;j++){ if(putChar==keyWj[j]){ putChar++; j=-1; } if(putChar!=keyWj[j]){ key[i]=putChar; } } if(key[i]>='A'&&key[i]<='Z') putChar++; } return key; } Text Maker Matrix Maker
STEP_플레이페어암호학 C Source #include<stdio.h> #include<stdlib.h> #include<conio.h> #include<time.h> #include<string.h> #include<windows.h> intEncDec(char*,char*,int); char* mkMatrix(char*); char* mkText(char*,int); int print2clip(char*); int main(){ intch=0; char key[25], text[100]; printf("1. Enc, 2.Dec :: "); scanf("%d", &ch); fflush(stdin); printf("Enter Key :: "); gets_s(key); fflush(stdin); printf("Enter Text :: "); gets_s(text); strupr(text); strcpy(text,mkText(text,ch)); strcpy(key,mkMatrix(key)); EncDec(key, text, ch); } intEncDec(char key[], char text[], intch){ char res[100]; char (*mt)[5] = (char(*)[5]) key; printf("\n"); /* 5X5 행렬 출력 */ for(inti=0; i<5; i++){ for(int j=0;j<5;j++){ printf("%2c",mt[i][j]); } printf("\n"); } /* 플레이페어암호학 규칙 */ int s[2][2]={0,}; for(unsigned int a=0;a<strlen(text);a+=2){ for(inti=0;i<5;i++){ for(int j=0;j<5;j++){ if(mt[i][j]==text[a]){ s[0][0]=i; s[0][1]=j; } if(mt[i][j]==text[a+1]){ s[1][0]=i; s[1][1]=j; } if(text[a]=='J'){ // J를 I로 처리 if(mt[i][j]=='I'){ s[0][0]=i; s[0][1]=j; break; } } if(text[a+1]=='J'){ // J를 I로 처리 if(mt[i][j]=='I'){ s[1][0]=i; s[1][1]=j; break; } } } } int v=0; if(ch==1) v=1; if(ch==2) v=4; if(s[0][1]==s[1][1]){ res[a]=mt[(s[0][0]+v)%5][s[0][1]]; res[a+1]=mt[(s[1][0]+v)%5][s[1][1]]; }elseif(s[0][0]==s[1][0]){ res[a]=mt[s[0][0]][(s[0][1]+v)%5]; res[a+1]=mt[s[1][0]][(s[1][1]+v)%5]; }else{ res[a]=mt[s[0][0]][s[1][1]]; res[a+1]=mt[s[1][0]][s[0][1]]; } } printf("\nTXT=%s\n",text); /* 암호문 출력 */ res[strlen(text)]='\0'; for(unsigned inti=0;i<strlen(text);i++){ printf("%c",text[i]); if(i%2) printf(" "); } printf("\n"); for(unsigned inti=0;i<strlen(res);i++){ printf("%c",res[i]); if(i%2) printf(" "); } printf("\n"); printf("RES=%s\n",res); print2clip(res); return 0; } char* mkMatrix(char key[]){ char putChar='A', key_tmp[25]={""}, keyWj[25]={""}; int count=0, ck=0; intcLen=0; strupr(key); cLen=strlen(key); /* KEY 내 동일문자 삭제 */ for(inti=0;i<cLen;i++){ ck = 0; for(int j=0;j<cLen;j++){ if(key[i]==key_tmp[j]) ck=1; } if(ck == 0) key_tmp[count++]=key[i]; } strcpy(key,key_tmp); strcpy(keyWj,key_tmp); cLen=strlen(key); for(inti=0;i<cLen;i++){ if(keyWj[i]=='I') keyWj[cLen]='J'; else if(keyWj[i]=='J') keyWj[cLen]='I'; } /* 행렬만들기 */ for(inti=cLen; i<25; i++){ for(int j=0;j<cLen+1;j++){ if(putChar==keyWj[j]){ putChar++; j=-1; } if(putChar!=keyWj[j]){ key[i]=putChar; } } if(key[i]>='A'&&key[i]<='Z') putChar++; } return key; } char* mkText(char text[], intch){ char text_tmp[100]={""}; int count=0, cLen=0; /* 공백제거 */ for(inti=0;i<100;i++){ if(text[i]>='A'&&text[i]<='Z'){ text_tmp[count++]=text[i]; } } /* 연속되는 글자 구분 */ count=0; for(unsigned inti=0;i<strlen(text_tmp)+1;i++){ if(text_tmp[i]==text_tmp[i+1]){ text[count++]=text_tmp[i]; text[count++]='X'; }else{ text[count++]=text_tmp[i]; text[count++]=text_tmp[i+1]; i++; } } if(ch==1){ /* 홀수 시 문자 추가 */ srand(time_t(NULL)); cLen=strlen(text); if(cLen%2) text[cLen++]=rand()%26+'A'; text[cLen]='\0'; } return text; } Matrix Maker Main Fuction Print Swapper Text Maker
STEP_플레이페어암호학 Main Fuction int main(){ int ch=0; char key[25], text[100]; printf("1. Enc, 2.Dec :: "); scanf("%d", &ch); fflush(stdin); printf("Enter Key :: "); gets_s(key); fflush(stdin); printf("Enter Text :: "); gets_s(text); strupr(text); strcpy(text,mkText(text,ch)); strcpy(key,mkMatrix(key)); EncDec(key, text, ch);} 1 – 암호화 2 – 복호화 암호키 -변수 key에 초기화 평 문 -변수 text에 초기화 평문 대문자화와 Text Maker함수 호출 암호키를 추가한 행렬 출력 암호키, 평문을 이용하여 암복호화
STEP_플레이페어암호학 Text Maker char* mkText(char text[], int ch){ char text_tmp[100]={""}; int count=0, cLen=0; /* 공백제거 */ for(int i=0;i<100;i++){ if(text[i]>='A'&&text[i]<='Z'){ text_tmp[count++]=text[i]; } } /* 연속되는 글자 구분 */ count=0; for(unsigned int i=0;i<strlen(text_tmp)+1;i++){ if(text_tmp[i]==text_tmp[i+1]){ text[count++]=text_tmp[i]; text[count++]='X'; }else{ text[count++]=text_tmp[i]; text[count++]=text_tmp[i+1]; i++; } } if(ch==1){ /* 홀수 시 문자 추가 */ srand(time_t(NULL)); cLen=strlen(text); if(cLen%2) text[cLen++]=rand()%26+'A'; text[cLen]='\0'; } return text; } 변수 text의 A와 Z사이 문자만 text_tmp에 저장 (공백제거) 두문자가 연속 같은 문자면 두문자 사이에 X를 추가 평문 문자가 홀수 시 랜덤문자 추가
STEP_플레이페어암호학 Matrix Maker char* mkMatrix(char key[]){ char putChar='A', key_tmp[25]={""}, keyWj[25]={""}; int count=0, ck=0; int cLen=0; strupr(key); cLen=strlen(key); /* KEY 내 동일문자 삭제 */ for(int i=0;i<cLen;i++){ ck = 0; for(int j=0;j<cLen;j++){ if(key[i]==key_tmp[j]) ck=1; } if(ck == 0) key_tmp[count++]=key[i]; } strcpy(key,key_tmp); strcpy(keyWj,key_tmp); cLen=strlen(key); for(int i=0;i<cLen;i++){ if(keyWj[i]=='I') keyWj[cLen]='J'; else if(keyWj[i]=='J') keyWj[cLen]='I'; } /* 행렬만들기 */ for(int i=cLen; i<25; i++){ for(int j=0;j<cLen+1;j++){ if(putChar==keyWj[j]){ putChar++; j=-1; } if(putChar!=keyWj[j]){ key[i]=putChar; } } if(key[i]>='A'&&key[i]<='Z') putChar++; } return key; } 암호키 내 동일문자 삭제 및 I, J 문자 동일화 암호키를 추가한 행렬 제작
STEP_플레이페어암호학 Swapper int EncDec(char key[], char text[], int ch){ char res[100]; char (*mt)[5] = (char(*)[5]) key; printf("\n"); /* 5X5 행렬 출력 */ for(int i=0; i<5; i++){ for(int j=0;j<5;j++){ printf("%2c",mt[i][j]); } printf("\n"); } /* 플레이페어 암호학 규칙 */ int s[2][2]={0,}; for(unsigned int a=0;a<strlen(text);a+=2){ for(int i=0;i<5;i++){ for(int j=0;j<5;j++){ if(mt[i][j]==text[a]){ s[0][0]=i; s[0][1]=j; } if(mt[i][j]==text[a+1]){ s[1][0]=i; s[1][1]=j; } if(text[a]=='J'){ // J를 I로 처리 if(mt[i][j]=='I'){ s[0][0]=i; s[0][1]=j; break; } } if(text[a+1]=='J'){ // J를 I로 처리 if(mt[i][j]=='I'){ s[1][0]=i; s[1][1]=j; break; } } } } 행렬출력 평문 각 각의 문자와 행렬 문자 비교 후 일치하는 문자의 행렬 x, y 값 지정
STEP_플레이페어암호학 Swapper int v=0; if(ch==1) v=1; if(ch==2) v=4; if(s[0][1]==s[1][1]){ res[a]=mt[(s[0][0]+v)%5][s[0][1]]; res[a+1]=mt[(s[1][0]+v)%5][s[1][1]]; }elseif(s[0][0]==s[1][0]){ res[a]=mt[s[0][0]][(s[0][1]+v)%5]; res[a+1]=mt[s[1][0]][(s[1][1]+v)%5]; }else{ res[a]=mt[s[0][0]][s[1][1]]; res[a+1]=mt[s[1][0]][s[0][1]]; } } printf("\nTXT=%s\n",text); /* 암호문 출력 */ res[strlen(text)]='\0'; for(unsigned int i=0;i<strlen(text);i++){ printf("%c",text[i]); if(i%2) printf(" "); } printf("\n"); for(unsigned int i=0;i<strlen(res);i++){ printf("%c",res[i]); if(i%2) printf(" "); } printf("\n"); printf("RES=%s\n",res); print2clip(res); return 0; } 행이 같으면,(행+1)mod5 열이 같으면,(열+1)mod5 그 외, 두 문자의 행의 위치 교환 < 행이 같은 경우 >
STEP_플레이페어암호학 Swapper int v=0; if(ch==1) v=1; if(ch==2) v=4; if(s[0][1]==s[1][1]){ res[a]=mt[(s[0][0]+v)%5][s[0][1]]; res[a+1]=mt[(s[1][0]+v)%5][s[1][1]]; }elseif(s[0][0]==s[1][0]){ res[a]=mt[s[0][0]][(s[0][1]+v)%5]; res[a+1]=mt[s[1][0]][(s[1][1]+v)%5]; }else{ res[a]=mt[s[0][0]][s[1][1]]; res[a+1]=mt[s[1][0]][s[0][1]]; } } printf("\nTXT=%s\n",text); /* 암호문 출력 */ res[strlen(text)]='\0'; for(unsigned int i=0;i<strlen(text);i++){ printf("%c",text[i]); if(i%2) printf(" "); } printf("\n"); for(unsigned int i=0;i<strlen(res);i++){ printf("%c",res[i]); if(i%2) printf(" "); } printf("\n"); printf("RES=%s\n",res); print2clip(res); return 0; } 행이 같으면,(행+1)mod5 열이 같으면,(열+1)mod5 그 외, 두 문자의 행의 위치 교환 < 행이 같은 경우 >
STEP_플레이페어암호학 Swapper int v=0; if(ch==1) v=1; if(ch==2) v=4; if(s[0][1]==s[1][1]){ res[a]=mt[(s[0][0]+v)%5][s[0][1]]; res[a+1]=mt[(s[1][0]+v)%5][s[1][1]]; }elseif(s[0][0]==s[1][0]){ res[a]=mt[s[0][0]][(s[0][1]+v)%5]; res[a+1]=mt[s[1][0]][(s[1][1]+v)%5]; }else{ res[a]=mt[s[0][0]][s[1][1]]; res[a+1]=mt[s[1][0]][s[0][1]]; } } printf("\nTXT=%s\n",text); /* 암호문 출력 */ res[strlen(text)]='\0'; for(unsigned int i=0;i<strlen(text);i++){ printf("%c",text[i]); if(i%2) printf(" "); } printf("\n"); for(unsigned int i=0;i<strlen(res);i++){ printf("%c",res[i]); if(i%2) printf(" "); } printf("\n"); printf("RES=%s\n",res); print2clip(res); return 0; } 행이 같으면,(행+1)mod5 열이 같으면,(열+1)mod5 그 외, 두 문자의 행의 위치 교환 < 열이 같은 경우 >
STEP_플레이페어암호학 Swapper int v=0; if(ch==1) v=1; if(ch==2) v=4; if(s[0][1]==s[1][1]){ res[a]=mt[(s[0][0]+v)%5][s[0][1]]; res[a+1]=mt[(s[1][0]+v)%5][s[1][1]]; }elseif(s[0][0]==s[1][0]){ res[a]=mt[s[0][0]][(s[0][1]+v)%5]; res[a+1]=mt[s[1][0]][(s[1][1]+v)%5]; }else{ res[a]=mt[s[0][0]][s[1][1]]; res[a+1]=mt[s[1][0]][s[0][1]]; } } printf("\nTXT=%s\n",text); /* 암호문 출력 */ res[strlen(text)]='\0'; for(unsigned int i=0;i<strlen(text);i++){ printf("%c",text[i]); if(i%2) printf(" "); } printf("\n"); for(unsigned int i=0;i<strlen(res);i++){ printf("%c",res[i]); if(i%2) printf(" "); } printf("\n"); printf("RES=%s\n",res); print2clip(res); return 0; } 행이 같으면,(행+1)mod5 열이 같으면,(열+1)mod5 그 외, 두 문자의 행의 위치 교환 < 그 외 경우 >
STEP_플레이페어암호학 Swapper int v=0; if(ch==1) v=1; if(ch==2) v=4; if(s[0][1]==s[1][1]){ res[a]=mt[(s[0][0]+v)%5][s[0][1]]; res[a+1]=mt[(s[1][0]+v)%5][s[1][1]]; }elseif(s[0][0]==s[1][0]){ res[a]=mt[s[0][0]][(s[0][1]+v)%5]; res[a+1]=mt[s[1][0]][(s[1][1]+v)%5]; }else{ res[a]=mt[s[0][0]][s[1][1]]; res[a+1]=mt[s[1][0]][s[0][1]]; } } printf("\nTXT=%s\n",text); /* 암호문 출력 */ res[strlen(text)]='\0'; for(unsigned int i=0;i<strlen(text);i++){ printf("%c",text[i]); if(i%2) printf(" "); } printf("\n"); for(unsigned int i=0;i<strlen(res);i++){ printf("%c",res[i]); if(i%2) printf(" "); } printf("\n"); printf("RES=%s\n",res); print2clip(res); return 0; } 행이 같으면,(행+1)mod5 열이 같으면,(열+1)mod5 그 외, 두 문자의 행의 위치 교환 평문을 두 문자씩 끊어서 출력 암호화된 문서를 두 문자씩 끊어서 출력 암호문 출력
ch=0 key=“” text=“” 암호키 :: jerry 평문 :: His son STEP_플레이페어암호학 Main Fuction int main(){ int ch=0; char key[25], text[100]; printf("1. Enc, 2.Dec :: "); scanf("%d", &ch); fflush(stdin); printf("Enter Key :: "); gets_s(key); fflush(stdin); printf("Enter Text :: "); gets_s(text); strupr(text); strcpy(text,mkText(text,ch)); strcpy(key,mkMatrix(key)); EncDec(key, text, ch);} ch=0, key=“”, text=“”
ch=0 key=“” text=“” 암호키 :: jerry 평문 :: His son STEP_플레이페어암호학 Main Fuction int main(){ int ch=0; char key[25], text[100]; printf("1. Enc, 2.Dec :: "); scanf("%d", &ch); fflush(stdin); printf("Enter Key :: "); gets_s(key); fflush(stdin); printf("Enter Text :: "); gets_s(text); strupr(text); strcpy(text,mkText(text,ch)); strcpy(key,mkMatrix(key)); EncDec(key, text, ch);} ch=0, key=“”, text=“” ch=1, key=“”, text=“”
ch=1 key=“” text=“” 암호키 :: jerry 평문 :: His son STEP_플레이페어암호학 Main Fuction int main(){ int ch=0; char key[25], text[100]; printf("1. Enc, 2.Dec :: "); scanf("%d", &ch); fflush(stdin); printf("Enter Key :: "); gets_s(key); fflush(stdin); printf("Enter Text :: "); gets_s(text); strupr(text); strcpy(text,mkText(text,ch)); strcpy(key,mkMatrix(key)); EncDec(key, text, ch);} ch=0, key=“”, text=“” ch=1, key=“”, text=“” ch=1, key=“jerry”, text=“”
ch=1 key=“jerry” text=“” 암호키 :: jerry 평문 :: His son STEP_플레이페어암호학 Main Fuction int main(){ int ch=0; char key[25], text[100]; printf("1. Enc, 2.Dec :: "); scanf("%d", &ch); fflush(stdin); printf("Enter Key :: "); gets_s(key); fflush(stdin); printf("Enter Text :: "); gets_s(text); strupr(text); strcpy(text,mkText(text,ch)); strcpy(key,mkMatrix(key)); EncDec(key, text, ch);} ch=0, key=“”, text=“” ch=1, key=“”, text=“” ch=1, key=“jerry”, text=“” ch=1, key=“jerry”, text=“His son”
ch=1 key=“jerry” text=“His son” 암호키 :: jerry 평문 :: His son STEP_플레이페어암호학 Main Fuction int main(){ int ch=0; char key[25], text[100]; printf("1. Enc, 2.Dec :: "); scanf("%d", &ch); fflush(stdin); printf("Enter Key :: "); gets_s(key); fflush(stdin); printf("Enter Text :: "); gets_s(text); strupr(text); strcpy(text,mkText(text,ch)); strcpy(key,mkMatrix(key)); EncDec(key, text, ch);} ch=0, key=“”, text=“” ch=1, key=“”, text=“” ch=1, key=“jerry”, text=“” ch=1, key=“jerry”, text=“His son” ch=1, key=“jerry”, text=“HIS SON”
ch=1 key=“jerry” text=“HIS SON” 암호키 :: jerry 평문 :: His son STEP_플레이페어암호학 Main Fuction int main(){ int ch=0; char key[25], text[100]; printf("1. Enc, 2.Dec :: "); scanf("%d", &ch); fflush(stdin); printf("Enter Key :: "); gets_s(key); fflush(stdin); printf("Enter Text :: "); gets_s(text); strupr(text); strcpy(text,mkText(text,ch)); strcpy(key,mkMatrix(key)); EncDec(key, text, ch);} ch=0, key=“”, text=“” ch=1, key=“”, text=“” ch=1, key=“jerry”, text=“” ch=1, key=“jerry”, text=“His son” ch=1, key=“jerry”, text=“HIS SON”
ch=1 key=“jerry” text=“HIS SON” text_tmp=“” count=0 cLen=0 STEP_플레이페어암호학 Text Maker text=“HIS SON”, ch=1 char* mkText(char text[], int ch){ char text_tmp[100]={""}; int count=0, cLen=0; /* 공백제거 */ for(int i=0;i<100;i++){ if(text[i]>='A'&&text[i]<='Z'){ text_tmp[count++]=text[i]; } } /* 연속되는 글자 구분 */ count=0; for(unsigned int i=0;i<=strlen(text_tmp);i++){ if(text_tmp[i]==text_tmp[i+1]){ text[count++]=text_tmp[i]; text[count++]='X'; }else{ text[count++]=text_tmp[i]; text[count++]=text_tmp[i+1]; i++; } } if(ch==1){ /* 홀수 시 문자 추가 */ srand(time_t(NULL)); cLen=strlen(text); if(cLen%2) text[cLen++]=rand()%26+'A'; text[cLen]='\0'; } return text; } text=“HIS SON”, text_tmp=“”, count=0, cLen=0, ch=1
ch=1 key=“jerry” text=“HIS SON” text_tmp=“” count=0 cLen=0 STEP_플레이페어암호학 Text Maker text=“HIS SON”, ch=1 char* mkText(char text[], int ch){ char text_tmp[100]={""}; int count=0, cLen=0; /* 공백제거 */ for(int i=0;i<100;i++){ if(text[i]>='A'&&text[i]<='Z'){ text_tmp[count++]=text[i]; } } /* 연속되는 글자 구분 */ count=0; for(unsigned int i=0;i<=strlen(text_tmp);i++){ if(text_tmp[i]==text_tmp[i+1]){ text[count++]=text_tmp[i]; text[count++]='X'; }else{ text[count++]=text_tmp[i]; text[count++]=text_tmp[i+1]; i++; } } if(ch==1){ /* 홀수 시 문자 추가 */ srand(time_t(NULL)); cLen=strlen(text); if(cLen%2) text[cLen++]=rand()%26+'A'; text[cLen]='\0'; } return text; } text=“HIS SON”, text_tmp=“”, count=0, cLen=0, ch=1 text=“HIS SON”, text_tmp=“HISSON”, count=6, cLen=0, ch=1
ch=1 key=“jerry” text=“HIS SON” text_tmp=“HISSON” count=6 cLen=0 STEP_플레이페어암호학 Text Maker text=“HIS SON”, ch=1 char* mkText(char text[], int ch){ char text_tmp[100]={""}; int count=0, cLen=0; /* 공백제거 */ for(int i=0;i<100;i++){ if(text[i]>='A'&&text[i]<='Z'){ text_tmp[count++]=text[i]; } } /* 연속되는 글자 구분 */ count=0; for(unsigned int i=0;i<=strlen(text_tmp);i++){ if(text_tmp[i]==text_tmp[i+1]){ text[count++]=text_tmp[i]; text[count++]='X'; }else{ text[count++]=text_tmp[i]; text[count++]=text_tmp[i+1]; i++; } } if(ch==1){ /* 홀수 시 문자 추가 */ srand(time_t(NULL)); cLen=strlen(text); if(cLen%2) text[cLen++]=rand()%26+'A'; text[cLen]='\0'; } return text; } text=“HIS SON”, text_tmp=“”, count=0, cLen=0, ch=1 text=“HIS SON”, text_tmp=“HISSON”, count=6, cLen=0, ch=1 text=“HIS SON”, text_tmp=“HISSON”, count=0, cLen=0, ch=1
ch=1 key=“jerry” text=“HIS SON” text_tmp=“HISSON” count=0 cLen=0 STEP_플레이페어암호학 Text Maker text=“HIS SON”, ch=1 char* mkText(char text[], int ch){ char text_tmp[100]={""}; int count=0, cLen=0; /* 공백제거 */ for(int i=0;i<100;i++){ if(text[i]>='A'&&text[i]<='Z'){ text_tmp[count++]=text[i]; } } /* 연속되는 글자 구분 */ count=0; for(unsigned int i=0;i<=strlen(text_tmp);i++){ if(text_tmp[i]==text_tmp[i+1]){ text[count++]=text_tmp[i]; text[count++]='X'; }else{ text[count++]=text_tmp[i]; text[count++]=text_tmp[i+1]; i++; } } if(ch==1){ /* 홀수 시 문자 추가 */ srand(time_t(NULL)); cLen=strlen(text); if(cLen%2) text[cLen++]=rand()%26+'A'; text[cLen]='\0'; } return text; } text=“HIS SON”, text_tmp=“”, count=0, cLen=0, ch=1 text=“HIS SON”, text_tmp=“HISSON”, count=6, cLen=0, ch=1 text=“HIS SON”, text_tmp=“HISSON”, count=0, cLen=0, ch=1 text=“HISXSON”, text_tmp=“HISSON”, count=8, cLen=0, ch=1
ch=1 key=“jerry” text=“HISXSON” text_tmp=“HISSON” count=0 cLen=0 STEP_플레이페어암호학 Text Maker text=“HIS SON”, ch=1 char* mkText(char text[], int ch){ char text_tmp[100]={""}; int count=0, cLen=0; /* 공백제거 */ for(int i=0;i<100;i++){ if(text[i]>='A'&&text[i]<='Z'){ text_tmp[count++]=text[i]; } } /* 연속되는 글자 구분 */ count=0; for(unsigned int i=0;i<=strlen(text_tmp);i++){ if(text_tmp[i]==text_tmp[i+1]){ text[count++]=text_tmp[i]; text[count++]='X'; }else{ text[count++]=text_tmp[i]; text[count++]=text_tmp[i+1]; i++; } } if(ch==1){ /* 홀수 시 문자 추가 */ srand(time_t(NULL)); cLen=strlen(text); if(cLen%2) text[cLen++]=rand()%26+'A'; text[cLen]='\0'; } return text; } text=“HIS SON”, text_tmp=“”, count=0, cLen=0, ch=1 text=“HIS SON”, text_tmp=“HISSON”, count=6, cLen=0, ch=1 text=“HIS SON”, text_tmp=“HISSON”, count=0, cLen=0, ch=1 text=“HISXSON”, text_tmp=“HISSON”, count=8, cLen=0, ch=1 text=“HISXSONM”, text_tmp=“HISSON”, count=8, cLen=8, ch=1
ch=1 key=“jerry” text=“HISXSONM” 암호키 :: jerry 평문 :: His son STEP_플레이페어암호학 Main Fuction int main(){ int ch=0; char key[25], text[100]; printf("1. Enc, 2.Dec :: "); scanf("%d", &ch); fflush(stdin); printf("Enter Key :: "); gets_s(key); fflush(stdin); printf("Enter Text :: "); gets_s(text); strupr(text); strcpy(text,mkText(text,ch)); strcpy(key,mkMatrix(key)); EncDec(key, text, ch);} ch=0, key=“”, text=“” ch=1, key=“”, text=“” ch=1, key=“jerry”, text=“” ch=1, key=“jerry”, text=“His son” ch=1, key=“jerry”, text=“HIS SON” ch=1, key=“jerry”, text=“HISXSONM”
key=“jerry” putChar=‘A’ key_tmp=“” keyWj=“” count=0 ck=0 cLen=0 STEP_플레이페어암호학 Text Maker char* mkMatrix(char key[]){ char putChar='A', key_tmp[25]={""}, keyWj[25]={""}; int count=0, ck=0; int cLen=0; strupr(key); cLen=strlen(key); /* KEY 내 동일문자 삭제 */ for(int i=0;i<cLen;i++){ ck = 0; for(int j=0;j<cLen;j++){ if(key[i]==key_tmp[j]) ck=1; } if(ck == 0) key_tmp[count++]=key[i]; } strcpy(key,key_tmp); strcpy(keyWj,key_tmp); cLen=strlen(key); for(int i=0;i<cLen;i++){ if(keyWj[i]=='I') keyWj[cLen]='J'; else if(keyWj[i]=='J') keyWj[cLen]='I'; } /* 행렬만들기 */ for(int i=cLen; i<25; i++){ for(int j=0;j<cLen+1;j++){ if(putChar==keyWj[j]){ putChar++; j=-1; } if(putChar!=keyWj[j]){ key[i]=putChar; } } if(key[i]>='A'&&key[i]<='Z') putChar++; } return key; } key =“jerry” key =“JERRY”, putChar=‘A’ , key_tmp=“”, keyWj=“”, count=0, ck=0, cLen=5
key=“JERRY” putChar=‘A’ key_tmp=“” keyWj=“” count=0 ck=0 cLen=5 STEP_플레이페어암호학 Text Maker char* mkMatrix(char key[]){ char putChar='A', key_tmp[25]={""}, keyWj[25]={""}; int count=0, ck=0; int cLen=0; strupr(key); cLen=strlen(key); /* KEY 내 동일문자 삭제 */ for(int i=0;i<cLen;i++){ ck = 0; for(int j=0;j<cLen;j++){ if(key[i]==key_tmp[j]) ck=1; } if(ck == 0) key_tmp[count++]=key[i]; } strcpy(key,key_tmp); strcpy(keyWj,key_tmp); cLen=strlen(key); for(int i=0;i<cLen;i++){ if(keyWj[i]=='I') keyWj[cLen]='J'; else if(keyWj[i]=='J') keyWj[cLen]='I'; } /* 행렬만들기 */ for(int i=cLen; i<25; i++){ for(int j=0;j<cLen+1;j++){ if(putChar==keyWj[j]){ putChar++; j=-1; } if(putChar!=keyWj[j]){ key[i]=putChar; } } if(key[i]>='A'&&key[i]<='Z') putChar++; } return key; } key =“jerry” key =“JERRY”, putChar=‘A’ , key_tmp=“”, keyWj=“”, count=0, ck=0, cLen=5 key =“JERRY”, putChar=‘A’ , key_tmp=“JERY”, keyWj=“”, count=4, ck=0, cLen=5
key=“JERRY” putChar=‘A’ key_tmp=“JERY” keyWj=“” count=4 ck=0 cLen=5 STEP_플레이페어암호학 Text Maker char* mkMatrix(char key[]){ char putChar='A', key_tmp[25]={""}, keyWj[25]={""}; int count=0, ck=0; int cLen=0; strupr(key); cLen=strlen(key); /* KEY 내 동일문자 삭제 */ for(int i=0;i<cLen;i++){ ck = 0; for(int j=0;j<cLen;j++){ if(key[i]==key_tmp[j]) ck=1; } if(ck == 0) key_tmp[count++]=key[i]; } strcpy(key,key_tmp); strcpy(keyWj,key_tmp); cLen=strlen(key); for(int i=0;i<cLen;i++){ if(keyWj[i]=='I') keyWj[cLen]='J'; else if(keyWj[i]=='J') keyWj[cLen]='I'; } /* 행렬만들기 */ for(int i=cLen; i<25; i++){ for(int j=0;j<cLen+1;j++){ if(putChar==keyWj[j]){ putChar++; j=-1; } if(putChar!=keyWj[j]){ key[i]=putChar; } } if(key[i]>='A'&&key[i]<='Z') putChar++; } return key; } key =“jerry” key =“JERRY”, putChar=‘A’ , key_tmp=“”, keyWj=“”, count=0, ck=0, cLen=5 key =“JERRY”, putChar=‘A’ , key_tmp=“JERY”, keyWj=“”, count=4, ck=0, cLen=5 key =“JERY”, putChar=‘A’ , key_tmp=“JERY”, keyWj=“JERY”, count=4, ck=0, cLen=4
key=“JERY” putChar=‘A’ key_tmp=“JERY” keyWj=“JERY” count=4 ck=0 cLen=4 STEP_플레이페어암호학 Text Maker char* mkMatrix(char key[]){ char putChar='A', key_tmp[25]={""}, keyWj[25]={""}; int count=0, ck=0; int cLen=0; strupr(key); cLen=strlen(key); /* KEY 내 동일문자 삭제 */ for(int i=0;i<cLen;i++){ ck = 0; for(int j=0;j<cLen;j++){ if(key[i]==key_tmp[j]) ck=1; } if(ck == 0) key_tmp[count++]=key[i]; } strcpy(key,key_tmp); strcpy(keyWj,key_tmp); cLen=strlen(key); for(int i=0;i<cLen;i++){ if(keyWj[i]=='I') keyWj[cLen]='J'; else if(keyWj[i]=='J') keyWj[cLen]='I'; } /* 행렬만들기 */ for(int i=cLen; i<25; i++){ for(int j=0;j<cLen+1;j++){ if(putChar==keyWj[j]){ putChar++; j=-1; } if(putChar!=keyWj[j]){ key[i]=putChar; } } if(key[i]>='A'&&key[i]<='Z') putChar++; } return key; } key =“jerry” key =“JERRY”, putChar=‘A’ , key_tmp=“”, keyWj=“”, count=0, ck=0, cLen=5 key =“JERRY”, putChar=‘A’ , key_tmp=“JERY”, keyWj=“”, count=4, ck=0, cLen=5 key =“JERY”, putChar=‘A’ , key_tmp=“JERY”, keyWj=“JERY”, count=4, ck=0, cLen=4 key =“JERY”, putChar=‘A’ , key_tmp=“JERY”, keyWj=“JERYI”, count=4, ck=0, cLen=4
key=“JERY” putChar=‘A’ key_tmp=“JERY” keyWj=“JERYI” count=4 ck=0 cLen=4 STEP_플레이페어암호학 Text Maker char* mkMatrix(char key[]){ char putChar='A', key_tmp[25]={""}, keyWj[25]={""}; int count=0, ck=0; int cLen=0; strupr(key); cLen=strlen(key); /* KEY 내 동일문자 삭제 */ for(int i=0;i<cLen;i++){ ck = 0; for(int j=0;j<cLen;j++){ if(key[i]==key_tmp[j]) ck=1; } if(ck == 0) key_tmp[count++]=key[i]; } strcpy(key,key_tmp); strcpy(keyWj,key_tmp); cLen=strlen(key); for(int i=0;i<cLen;i++){ if(keyWj[i]=='I') keyWj[cLen]='J'; else if(keyWj[i]=='J') keyWj[cLen]='I'; } /* 행렬만들기 */ for(int i=cLen; i<25; i++){ for(int j=0;j<cLen+1;j++){ if(putChar==keyWj[j]){ putChar++; j=-1; } if(putChar!=keyWj[j]){ key[i]=putChar; } } if(key[i]>='A'&&key[i]<='Z') putChar++; } return key; } key =“jerry” key =“JERRY”, putChar=‘A’ , key_tmp=“”, keyWj=“”, count=0, ck=0, cLen=5 key =“JERRY”, putChar=‘A’ , key_tmp=“JERY”, keyWj=“”, count=4, ck=0, cLen=5 key =“JERY”, putChar=‘A’ , key_tmp=“JERY”, keyWj=“JERY”, count=4, ck=0, cLen=4 key =“JERY”, putChar=‘A’ , key_tmp=“JERY”, keyWj=“JERYI”, count=4, ck=0, cLen=4 key =“JERYABCDFG…”, putChar=‘[‘ , keyWj=“JERYI”
ch=1 key=“JERYABCDFGH…” text=“HISXSONM” 암호키 :: jerry 평문 :: His son STEP_플레이페어암호학 Main Fuction int main(){ int ch=0; char key[25], text[100]; printf("1. Enc, 2.Dec :: "); scanf("%d", &ch); fflush(stdin); printf("Enter Key :: "); gets_s(key); fflush(stdin); printf("Enter Text :: "); gets_s(text); strupr(text); strcpy(text,mkText(text,ch)); strcpy(key,mkMatrix(key)); EncDec(key, text, ch);} ch=0, key=“”, text=“” ch=1, key=“”, text=“” ch=1, key=“jerry”, text=“” ch=1, key=“jerry”, text=“His son” ch=1, key=“jerry”, text=“HIS SON” ch=1, key=“jerry”, text=“HISXSONM” ch=1, key=“JERYABCDFGH…”, text=“HISXSONM”
key=“JERYABCD..” text=“HISXSONM” ch=1 res=“” mt[5][5]=&key STEP_플레이페어암호학 Swapper int EncDec(char key[], char text[], int ch){ char res[100]; char (*mt)[5] = (char(*)[5]) key; printf("\n"); /* 5X5 행렬 출력 */ for(int i=0; i<5; i++){ for(int j=0;j<5;j++){ printf("%2c",mt[i][j]); } printf("\n"); } key=“JERYABCDFG…”, text=“HISXSONM”, ch=1 key=“JERYAB..”, text=“HISXSONM”, res=“”, mt[5][5]=&key, ch=1
key=“JERYABCD..” text=“HISXSONM” ch=1 res=“” mt[5][5]=&key STEP_플레이페어암호학 Swapper int EncDec(char key[], char text[], int ch){ char res[100]; char (*mt)[5] = (char(*)[5]) key; printf("\n"); /* 5X5 행렬 출력 */ for(int i=0; i<5; i++){ for(int j=0;j<5;j++){ printf("%2c",mt[i][j]); } printf("\n"); } key=“JERYABCDFG…”, text=“HISXSONM”, ch=1 key=“JERYAB..”, text=“HISXSONM”, res=“”, mt[5][5]=&key, ch=1 key=“JERYAB..”, text=“HISXSONM”, res=“”, mt[5][5]=&key, ch=1
key=“JERYABCD..” text=“HISXSONM” ch=1 res=“” mt[5][5]=&key s[][]={0,0,0,0} v=0 STEP_플레이페어암호학 Swapper /* 플레이페어 암호학 규칙 */ int s[2][2]={0,}; for(unsigned int a=0;a<strlen(text);a+=2){ for(int i=0;i<5;i++){ for(int j=0;j<5;j++){ if(mt[i][j]==text[a]){ s[0][0]=i; s[0][1]=j; } if(mt[i][j]==text[a+1]){ s[1][0]=i; s[1][1]=j; } if(text[a]=='J'&&mt[i][j]=='I'){ s[0][0]=i; s[0][1]=j; break; } if(text[a+1]=='J'&&mt[i][j]=='I'){ s[1][0]=i; s[1][1]=j; break; } } } int v=0; if(ch==1) v=1; else if(ch==2) v=4; if(s[0][1]==s[1][1]){ res[a]=mt[(s[0][0]+v)%5][s[0][1]]; res[a+1]=mt[(s[1][0]+v)%5][s[1][1]]; }else if(s[0][0]==s[1][0]){ res[a]=mt[s[0][0]][(s[0][1]+v)%5]; res[a+1]=mt[s[1][0]][(s[1][1]+v)%5]; }else{ res[a]=mt[s[0][0]][s[1][1]]; res[a+1]=mt[s[1][0]][s[0][1]]; } } s[][]={0,0,0,0}, text=“HISXSONM”, res=“”, mt[5][5]=&key, ch=1
key=“JERYABCD..” text=“HISXSONM” ch=1 res=“” mt[5][5]=&key s[][]={0,0,0,0} v=0 STEP_플레이페어암호학 Swapper /* 플레이페어 암호학 규칙 */ int s[2][2]={0,}; for(unsigned int a=0;a<strlen(text);a+=2){ for(int i=0;i<5;i++){ for(int j=0;j<5;j++){ if(mt[i][j]==text[a]){ s[0][0]=i; s[0][1]=j; } if(mt[i][j]==text[a+1]){ s[1][0]=i; s[1][1]=j; } if(text[a]=='J'&&mt[i][j]=='I'){ s[0][0]=i; s[0][1]=j; break; } if(text[a+1]=='J'&&mt[i][j]=='I'){ s[1][0]=i; s[1][1]=j; break; } } } int v=0; if(ch==1) v=1; else if(ch==2) v=4; if(s[0][1]==s[1][1]){ res[a]=mt[(s[0][0]+v)%5][s[0][1]]; res[a+1]=mt[(s[1][0]+v)%5][s[1][1]]; }else if(s[0][0]==s[1][0]){ res[a]=mt[s[0][0]][(s[0][1]+v)%5]; res[a+1]=mt[s[1][0]][(s[1][1]+v)%5]; }else{ res[a]=mt[s[0][0]][s[1][1]]; res[a+1]=mt[s[1][0]][s[0][1]]; } } s[][]={0,0,0,0}, text=“HISXSONM”, res=“”, mt[5][5]=&key, ch=1 s[][]={2,0,0,0}, text=“HISXSONM”, res=“OB”, mt[5][5]=&key, ch=1
key=“JERYABCD..” text=“HISXSONM” ch=1 res=“OB” mt[5][5]=&key s[][]={2,0,0,0} v=1 STEP_플레이페어암호학 Swapper /* 플레이페어 암호학 규칙 */ int s[2][2]={0,}; for(unsigned int a=0;a<strlen(text);a+=2){ for(int i=0;i<5;i++){ for(int j=0;j<5;j++){ if(mt[i][j]==text[a]){ s[0][0]=i; s[0][1]=j; } if(mt[i][j]==text[a+1]){ s[1][0]=i; s[1][1]=j; } if(text[a]=='J'&&mt[i][j]=='I'){ s[0][0]=i; s[0][1]=j; break; } if(text[a+1]=='J'&&mt[i][j]=='I'){ s[1][0]=i; s[1][1]=j; break; } } } int v=0; if(ch==1) v=1; else if(ch==2) v=4; if(s[0][1]==s[1][1]){ res[a]=mt[(s[0][0]+v)%5][s[0][1]]; res[a+1]=mt[(s[1][0]+v)%5][s[1][1]]; }else if(s[0][0]==s[1][0]){ res[a]=mt[s[0][0]][(s[0][1]+v)%5]; res[a+1]=mt[s[1][0]][(s[1][1]+v)%5]; }else{ res[a]=mt[s[0][0]][s[1][1]]; res[a+1]=mt[s[1][0]][s[0][1]]; } } s[][]={0,0,0,0}, text=“HISXSONM”, res=“”, mt[5][5]=&key, ch=1 s[][]={2,0,0,0}, text=“HISXSONM”, res=“OB”, mt[5][5]=&key, ch=1 s[][]={3,3,4,3}, text=“HISXSONM”, res=“OBXY”, mt[5][5]=&key, ch=1
key=“JERYABCD..” text=“HISXSONM” ch=1 res=“OBXY” mt[5][5]=&key s[][]={3,3,4,3} v=1 STEP_플레이페어암호학 Swapper /* 플레이페어 암호학 규칙 */ int s[2][2]={0,}; for(unsigned int a=0;a<strlen(text);a+=2){ for(int i=0;i<5;i++){ for(int j=0;j<5;j++){ if(mt[i][j]==text[a]){ s[0][0]=i; s[0][1]=j; } if(mt[i][j]==text[a+1]){ s[1][0]=i; s[1][1]=j; } if(text[a]=='J'&&mt[i][j]=='I'){ s[0][0]=i; s[0][1]=j; break; } if(text[a+1]=='J'&&mt[i][j]=='I'){ s[1][0]=i; s[1][1]=j; break; } } } int v=0; if(ch==1) v=1; else if(ch==2) v=4; if(s[0][1]==s[1][1]){ res[a]=mt[(s[0][0]+v)%5][s[0][1]]; res[a+1]=mt[(s[1][0]+v)%5][s[1][1]]; }else if(s[0][0]==s[1][0]){ res[a]=mt[s[0][0]][(s[0][1]+v)%5]; res[a+1]=mt[s[1][0]][(s[1][1]+v)%5]; }else{ res[a]=mt[s[0][0]][s[1][1]]; res[a+1]=mt[s[1][0]][s[0][1]]; } } s[][]={0,0,0,0}, text=“HISXSONM”, res=“”, mt[5][5]=&key, ch=1 s[][]={2,0,0,0}, text=“HISXSONM”, res=“OB”, mt[5][5]=&key, ch=1 s[][]={3,3,4,3}, text=“HISXSONM”, res=“OBXY”, mt[5][5]=&key, ch=1 s[][]={3,3,3,0}, text=“HISXSONM”, res=“OBXYTP”, mt[5][5]=&key, ch=1
key=“JERYABCD..” text=“HISXSONM” ch=1 res=“OBXYTP” mt[5][5]=&key s[][]={3,3,3,0} v=1 STEP_플레이페어암호학 Swapper /* 플레이페어 암호학 규칙 */ int s[2][2]={0,}; for(unsigned int a=0;a<strlen(text);a+=2){ for(int i=0;i<5;i++){ for(int j=0;j<5;j++){ if(mt[i][j]==text[a]){ s[0][0]=i; s[0][1]=j; } if(mt[i][j]==text[a+1]){ s[1][0]=i; s[1][1]=j; } if(text[a]=='J'&&mt[i][j]=='I'){ s[0][0]=i; s[0][1]=j; break; } if(text[a+1]=='J'&&mt[i][j]=='I'){ s[1][0]=i; s[1][1]=j; break; } } } int v=0; if(ch==1) v=1; else if(ch==2) v=4; if(s[0][1]==s[1][1]){ res[a]=mt[(s[0][0]+v)%5][s[0][1]]; res[a+1]=mt[(s[1][0]+v)%5][s[1][1]]; }else if(s[0][0]==s[1][0]){ res[a]=mt[s[0][0]][(s[0][1]+v)%5]; res[a+1]=mt[s[1][0]][(s[1][1]+v)%5]; }else{ res[a]=mt[s[0][0]][s[1][1]]; res[a+1]=mt[s[1][0]][s[0][1]]; } } s[][]={0,0,0,0}, text=“HISXSONM”, res=“”, mt[5][5]=&key, ch=1 s[][]={2,0,0,0}, text=“HISXSONM”, res=“OB”, mt[5][5]=&key, ch=1 s[][]={3,3,4,3}, text=“HISXSONM”, res=“OBXY”, mt[5][5]=&key, ch=1 s[][]={3,3,3,0}, text=“HISXSONM”, res=“OBXYTP”, mt[5][5]=&key, ch=1 s[][]={2,4,2,3}, text=“HISXSONM”, res=“OBXYTPHN”, mt[5][5]=&key, ch=1
key=“JERYABCD..” text=“HISXSONM” ch=1 res=“OBXYTPHN” mt[5][5]=&key s[][]={2,4,2,3} v=1 STEP_플레이페어암호학 Print /* 암호문 출력 */ res[strlen(text)]='\0'; for(unsigned int i=0;i<strlen(text);i++){ printf("%c",text[i]); if(i%2) printf(" "); } printf("\n"); for(unsigned int i=0;i<strlen(res);i++){ printf("%c",res[i]); if(i%2) printf(" "); } printf("\n"); printf("RES=%s\n",res); return 0; } s[][]={2,4,2,3}, text=“HISXSONM”, res=“OBXYTPHN”, mt[5][5]=&key, ch=1
STEP_플레이페어암호학 Swapper_복호화 /* 플레이페어 암호학 규칙 */ int s[2][2]={0,}; for(unsigned int a=0;a<strlen(text);a+=2){ for(int i=0;i<5;i++){ for(int j=0;j<5;j++){ if(mt[i][j]==text[a]){ s[0][0]=i; s[0][1]=j; } if(mt[i][j]==text[a+1]){ s[1][0]=i; s[1][1]=j; } if(text[a]=='J'&&mt[i][j]=='I'){ s[0][0]=i; s[0][1]=j; break; } if(text[a+1]=='J'&&mt[i][j]=='I'){ s[1][0]=i; s[1][1]=j; break; } } } int v=0; if(ch==1) v=1; else if(ch==2) v=4; if(s[0][1]==s[1][1]){ res[a]=mt[(s[0][0]+v)%5][s[0][1]]; res[a+1]=mt[(s[1][0]+v)%5][s[1][1]]; }else if(s[0][0]==s[1][0]){ res[a]=mt[s[0][0]][(s[0][1]+v)%5]; res[a+1]=mt[s[1][0]][(s[1][1]+v)%5]; }else{ res[a]=mt[s[0][0]][s[1][1]]; res[a+1]=mt[s[1][0]][s[0][1]]; } } s[][]={2,0,2,4}, text=“OBXYTPHN”, res=“HISXSONM”, mt[5][5]=&key, ch=1
STEP_플레이페어암호학 Print
STEP_플레이페어암호학 #include<stdio.h> #include<stdlib.h> #include<conio.h> #include<time.h> #include<string.h> #include<windows.h> intEncDec(char*,char*,int); char* mkMatrix(char*); char* mkText(char*,int); int main(){ intch=0; char key[25], text[100]; printf("1. Enc, 2.Dec :: "); scanf("%d", &ch); fflush(stdin); printf("Enter Key :: "); gets_s(key); fflush(stdin); printf("Enter Text :: "); gets_s(text); strupr(text); strcpy(text,mkText(text,ch)); strcpy(key,mkMatrix(key)); EncDec(key, text, ch); } intEncDec(char key[], char text[], intch){ char res[100]; char (*mt)[5] = (char(*)[5]) key; printf("\n"); /* 5X5 행렬 출력 */ for(inti=0; i<5; i++){ for(int j=0;j<5;j++){ printf("%2c",mt[i][j]); } printf("\n"); } /* 플레이페어암호학 규칙 */ int s[2][2]={0,}; for(unsigned int a=0;a<strlen(text);a+=2){ for(inti=0;i<5;i++){ for(int j=0;j<5;j++){ if(mt[i][j]==text[a]){ s[0][0]=i; s[0][1]=j; } if(mt[i][j]==text[a+1]){ s[1][0]=i; s[1][1]=j; } if(text[a]=='J'&&mt[i][j]=='I'){ // J를 I로 처리 s[0][0]=i; s[0][1]=j; break; } if(text[a+1]=='J'&&mt[i][j]=='I'){ // J를 I로 처리 s[1][0]=i; s[1][1]=j; break; } } } int v=0; if(ch==1) v=1; else if(ch==2) v=4; if(s[0][1]==s[1][1]){ res[a]=mt[(s[0][0]+v)%5][s[0][1]]; res[a+1]=mt[(s[1][0]+v)%5][s[1][1]]; }else if(s[0][0]==s[1][0]){ res[a]=mt[s[0][0]][(s[0][1]+v)%5]; res[a+1]=mt[s[1][0]][(s[1][1]+v)%5]; }else{ res[a]=mt[s[0][0]][s[1][1]]; res[a+1]=mt[s[1][0]][s[0][1]]; } } printf("\nTXT=%s\n",text); /* 암호문 출력 */ res[strlen(text)]='\0'; for(unsigned inti=0;i<strlen(text);i++){ printf("%c",text[i]); if(i%2) printf(" "); } printf("\n"); for(unsigned inti=0;i<strlen(res);i++){ printf("%c",res[i]); if(i%2) printf(" "); } printf("\n"); printf("RES=%s\n",res); return 0; } char* mkMatrix(char key[]){ char putChar='A', key_tmp[25]={""}, keyWj[25]={""}; int count=0, ck=0; intcLen=0; strupr(key); cLen=strlen(key); /* KEY 내 동일문자 삭제 */ for(inti=0;i<cLen;i++){ ck = 0; for(int j=0;j<cLen;j++){ if(key[i]==key_tmp[j]) ck=1; } if(ck == 0) key_tmp[count++]=key[i]; } strcpy(key,key_tmp); strcpy(keyWj,key_tmp); cLen=strlen(key); for(inti=0;i<cLen;i++){ if(keyWj[i]=='I') keyWj[cLen]='J'; else if(keyWj[i]=='J') keyWj[cLen]='I'; else keyWj[cLen]=‘J'; } /* 행렬만들기 */ for(inti=cLen; i<25; i++){ for(int j=0;j<cLen+1;j++){ if(putChar==keyWj[j]){ putChar++; j=-1; } if(putChar!=keyWj[j]){ key[i]=putChar; } } if(key[i]>='A'&&key[i]<='Z') putChar++; } return key; } char* mkText(char text[], intch){ char text_tmp[100]={""}; int count=0, cLen=0; /* 공백제거 */ for(inti=0;i<100;i++){ if(text[i]>='A'&&text[i]<='Z'){ text_tmp[count++]=text[i]; } } /* 연속되는 글자 구분 */ count=0; for(unsigned inti=0;i<strlen(text_tmp)+1;i++){ if(text_tmp[i]==text_tmp[i+1]){ text[count++]=text_tmp[i]; text[count++]='X'; }else{ text[count++]=text_tmp[i]; text[count++]=text_tmp[i+1]; i++; } } if(ch==1){ /* 홀수 시 문자 추가 */ srand(time_t(NULL)); cLen=strlen(text); if(cLen%2) text[cLen++]=rand()%26+'A'; text[cLen]='\0'; } return text; } Q&A - Thank u -