반응형
플레이페어 암화화란
플레이페어 암호화는 영국의 물리학자 휘트스톤과 영국의 수학자 플레이페어가 함께 만든 함호화 방식입니다.
휘트스톤이 죽은다음 발표를 하게되어 이 암호화 방식은 플레이페어란 이름을 붙이게 되었습니다.
순서
1. 암호화키 중복문자 제거
EX) 암호화키(ASSASSINATOR) -> (ASINTOR)
2. 암호화키 5x5 테이블에 삽입
A |
S |
I |
N |
T |
O |
R |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
3. 빈테이블에 알파벳 A~Z까지 삽입
단 I/J or Q/Z 같은 자리에 넣어줘야 ㅎ
A |
S |
I |
N |
T |
O |
R |
B |
C |
D |
E |
F |
G |
H |
J |
K |
L |
M |
P |
Q/Z |
U |
V |
W |
X |
Y |
규칙
1. 문장을 2글자씩 쪼개기(한쌍의 문자가 같거나 마지막에 하나남은 문자에 X추가)
규칙1. 암호화 하려는 두 문자가 서로다른 행과 다른 열에 존재할 경우
규칙2. 두 문자가 같은 열에 있는 경우
규칙3. 두 문자가 같은 행에 있는 경우
c언어 코드
#include<stdio.h> #include<stdlib.h> void MakeCypherTable(char table[][5]); void Plain2Cypher(char table[][5]); char Encrypt_DifferRowNColumn(char table[][5],int i,int j); char Encrypt_SameRow(char table[][5],int i,int j); char Encrypt_SameColumn(char table[][5], int i,int j); void Cypher2Plain(char table[][5]); char Decrypt_DifferRowNColumn(char table[][5],int i,int j); char Decrypt_SameRow(char table[][5],int i,int j); char Decrypt_SameColumn(char table[][5],int i,int j); int flag=0; main() { int select=0; char table[5][5]; while(1) { printf("input num (1.start/0.end):"); scanf("%d",&select); getchar(); if(select==0) { return 0; } else if(select==1) { MakeCypherTable(table); Plain2Cypher(table); Cypher2Plain(table); } } } void MakeCypherTable(char table[][5]) { char alph[]= "ABCDEFGHIJKLMNOPQRSTUVWXY"; char keystring[30]; int count=0,alphnum=0; int i,j,k,keylen,keycheck,alphcheck; printf("input key string:"); gets(keystring); //----key overlap for(i=0;i<(strlen(keystring))-1;i++) { for(j=i+1;j<strlen(keystring);j++) { if(keystring[i]==keystring[j]) { keycheck=j; for(k=keycheck;k<(strlen(keystring)-1);k++) { keystring[k]=keystring[k+1]; } keystring[strlen(keystring)-1]='\0'; j=j-1; } } } printf("keystring:%s\n",keystring); for(i=0;i<strlen(keystring);i++) { for(j=0;j<strlen(alph);j++) { if(keystring[i]==alph[j]) { alphcheck=j; for(k=alphcheck;k<(strlen(alph)-1);k++) { alph[k]=alph[k+1]; } alph[strlen(alph)-1]='\0'; } } } printf("alph:%s\n",alph); keylen=strlen(keystring); for(i=0;i<5;i++) { for(j=0;j<5;j++) { if(keylen>count) { table[i][j]=keystring[count]; } else { table[i][j]=alph[alphnum]; alphnum++; } count++; } } for(i=0;i<5;i++) { for(j=0;j<5;j++) { printf("%c ",table[i][j]); } printf("\n"); } } void Plain2Cypher(char table[][5]) { char plain[30]=""; char encryption[30]=""; int i,j,k; int check; int plainlen=0; int plainleft,plainright,plainreleft,plainreright; printf("input plain string:"); gets(plain); for(i=0;i<(strlen(plain)-1);i=i+2) { if(plain[i]==plain[i+1]) { check=i+1; for(j=(strlen(plain)-1);j>=check;j--) { plain[j+1]=plain[j]; } plain[check]='X'; } } if(strlen(plain)%2==1) { plain[strlen(plain)]='X'; flag=1; } plain[strlen(plain)]='\0'; printf("plain:%s\n",plain); for(i=0;i<strlen(plain);i++) { if(plain[i]=='Z') { plain[i]=='Q'; } } plainlen=strlen(plain); for(k=0;k<plainlen;k=k+2) { for(i=0;i<5;i++) { for(j=0;j<5;j++) { if(table[i][j]==plain[k]) { plainright=j; plainreleft=i; } if(table[i][j]==plain[k+1]) { plainleft=i; plainreright=j; } } } if(plainreright==plainright) { encryption[k]=Encrypt_SameRow(table,plainreleft,plainright); encryption[k+1]=Encrypt_SameRow(table,plainleft,plainreright); } else if(plainreleft==plainleft) { encryption[k]=Encrypt_SameColumn(table,plainreleft,plainright); encryption[k+1]=Encrypt_SameColumn(table,plainleft,plainreright); } else { encryption[k]=Encrypt_DifferRowNColumn(table,plainleft,plainright); encryption[k+1]=Encrypt_DifferRowNColumn(table,plainreleft,plainreright); } } printf("encryption:%s\n",encryption); } char Encrypt_DifferRowNColumn(char table[][5],int i,int j) { return table[i][j]; } char Encrypt_SameRow(char table[][5],int i,int j) { if(i==4) { return table[0][j]; } else { return table[i+1][j]; } } char Encrypt_SameColumn(char table[][5], int i,int j) { if(j==4) { return table[i][0]; } else { return table[i][j+1]; } } void Cypher2Plain(char table[][5]) { char cypher[30]=""; char dencryption[30]=""; int i,j,k; int check; int cypherlen=0; int cypherleft,cypherright,cypherreleft,cypherreright; printf("input cypher string:"); gets(cypher); cypherlen=strlen(cypher); for(k=0;k<cypherlen;k=k+2) { for(i=0;i<5;i++) { for(j=0;j<5;j++) { if(table[i][j]==cypher[k]) { cypherright=j; cypherreleft=i; } if(table[i][j]==cypher[k+1]) { cypherleft=i; cypherreright=j; } } } if(cypherreright==cypherright) { dencryption[k]=Decrypt_SameRow(table,cypherreleft,cypherright); dencryption[k+1]=Decrypt_SameRow(table,cypherleft,cypherreright); } else if(cypherreleft==cypherleft) { dencryption[k]=Decrypt_SameColumn(table,cypherreleft,cypherright); dencryption[k+1]=Decrypt_SameColumn(table,cypherleft,cypherreright); } else { dencryption[k]=Decrypt_DifferRowNColumn(table,cypherleft,cypherright); dencryption[k+1]=Decrypt_DifferRowNColumn(table,cypherreleft,cypherreright); } } printf("dencryption:%s\n",dencryption); if(flag==1) { dencryption[strlen(dencryption)-1]='\0'; } for(i=1;i<strlen(dencryption);i=i+2) { if(dencryption[i]=='X') { if(dencryption[i-1]==dencryption[i+1]) { for(j=i;j<(strlen(dencryption)-1);j++) { dencryption[j]=dencryption[j+1]; } dencryption[strlen(dencryption)-1]='\0'; i=i-1; } } } printf("plain dencryption : %s\n",dencryption); } char Decrypt_DifferRowNColumn(char table[][5],int i,int j) { return table[i][j]; } char Decrypt_SameRow(char table[][5],int i,int j) { if(i==0) { return table[4][j]; } else { return table[i-1][j]; } } char Decrypt_SameColumn(char table[][5],int i,int j) { if(j==0) { return table[i][4]; } else { return table[i][j-1]; } }
반응형