[C] 치환 암호 (Substitution Cipher)
2017. 3. 15. 23:08
- #include <stdio.h>
- #include <string.h>
- void encrypt(char* P, char* C,char* table)
- {
- int len;
- int i,j;
- for(i=0; i<len; i++){
- if(P[i]>='A' && P[i]<='Z')
- C[i]=table[P[i]-'A']-('a'-'A');
- else if(P[i]>='a' && P[i]<='z')
- C[i]=table[P[i]-'a'];
- else
- C[i]=P[i];
- }
- }
- void decrypt(char* C, char* D, char* table)
- {
- int len;
- int i,j;
- for(i=0; i<len; i++){
- if(C[i]>='A' && C[i]<='Z')
- {
- for(j=0; j<26; j++)
- if((table[j]-('a'-'A'))==C[i])
- D[i]='A'+j;
- }
- else if(C[i]>='a' && C[i]<='z')
- {
- for(j=0; j<26; j++)
- if(table[j]==C[i])
- D[i]='a'+j;
- }
- else
- D[i]=C[i];
- }
- }
- int main()
- {
- char plain[50]="HelloWorld!MyNameisArgos";
- char crypt[50];
- char d_crypt[50];
- char replace_table[27]="lqkrmnedvwxaopbghifstucjyz";
- encrypt(plain,crypt,replace_table);
- decrypt(crypt,d_crypt,replace_table);
- return 0;
- }
'코드 저장소' 카테고리의 다른 글
[C] 폴리비우스 암호(Polibius cipher) (0) | 2017.03.15 |
---|