1. 程式人生 > >按照對映關係進行加密,解密

按照對映關係進行加密,解密

應該還有其他彩蛋

1程式碼

/*  字串加密
加密 由S1向S2對映,這個關係由P1儲存,P1(S1[i])-> S2[i],
解密 反向 P2(S2[i])-> S1[i],
利用ASCII碼值,表明對映關係,先進行加密,加密後字串解密
應該還有其他好玩的彩蛋
*/
#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <cstring>
using namespace std;
char s1[30]={'a','b','c','d','e','f','g','h','i','j','k','l','m','n','o','p','q','r','s','t','u','v','w','x','y','z'};
char s2[30]={'n','g','z','q','t','c','o','b','m','u','h','e','l','k','p','d','a','w','x','f','y','i','v','r','s','j'};
char p1[26],p2[26];
int main(){
    char s[1000];
    int s_len,i;
    for(i=0;i<26;i++){
        p1[s1[i]-'a']=s2[i];
        p2[s2[i]-'a']=s1[i];
    }
    cout<<"請輸入您想加密的字串:"<<endl;
    cin>>s;
    s_len=strlen(s);
    for(i=0;i<s_len;i++){
        s[i]=p1[s[i]-'a'];
    }cout<<"加密後字串:"<<endl<<s<<endl;
    for(i=0;i<s_len;i++){
        s[i]=p2[s[i]-'a'];
    }cout<<"原字串:"<<endl<<s;
    return 0;
}