论如何加密一段文字

· · 个人记录

近日,惊现许多奇葩加密算法。

我也来提出一种:

基本结构:

第一位为标识符
0-XXXXX 表示一个数字
1-XXXXX-X 表示一个字母
2-X-XXXXX|XXXXX-X 表示一个汉字
3-XXXXX 表示一个标点符号(英文)
4 表示空格
5 表示Tab(4个空格)
6 表示换行
End 表示结束

填充内容:

  1. 字母 1-XXXXX-X
    第1位为1表示该字符为英文字母
    中间的5位表示该字母在字母表中是第几个字母的二进制(a为0)(例c的二进制码为00010)
    最后1位为1代表该字母为小写,2代表大写
  2. 汉字 2-X-XXXXX|XXXXX-X
    第1位为2代表该字符为汉字
    第2位代表该汉字的拼音长度
    后面每个5位的二进制码代表该拼音字母的二进制码(规则同上),每个之间用 | 隔开
    最后1位代表该汉字的声调,0代表轻声
  3. 符号 3-XXXXX
    第1位为3代表该字符为符号
    后5位二进制码代表该符号在下表中的位置
    char point[]={',','.','?','!',':','\"','\'','%','(',')','+','-','*','/'};

加密与解码代码(施工中):

#include<bits/stdc++.h>
using namespace std;
char letters[]={'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 point[]={',','.','?','!',':','\"','\'','%','(',')','+','-','*','/'};
int getbin(string str,int l,int r){
    int ans=0,tot=1;
    for(int i=r;i>=l;i--){
        ans+=tot*(str[i]=='1'?1:0);
        tot*=2;
    }
    return ans;
}
string tobin(int num){
    string ans;
    int tot=pow(2,4);
    while(tot){
        ans+=num%tot+'0';
        tot/=2;
    }
    return ans;
}
void to_code(string word){
    if(word.size()>1){
        cout<<"2-"<<word.size()-1<<"-"<<tobin(word[0]-'A');
        for(int i=1;i<word.size()-1;i++){
            cout<<"|"<<tobin(word[i]-'a');
        }
        cout<<"-"<<word[word.size()-1];
    }else{
        if(word[0]>='0'&&word[0]<='9'){
            cout<<"0-"<<tobin(word[0]-'0');
        }
        else if(word[0]>='a'&&word[0]<='z'){
            cout<<"1-"<<tobin(word[0]-'a')<<"-1";
        }
        else if(word[0]>='A'&&word[0]<='Z'){
            cout<<"1-"<<tobin(word[0]-'A')<<"-2";
        }
        else if(word[0]==' '){
            cout<<"4";
        }
        else if(word[0]=='\t'){
            cout<<"5";
        }
        else if(word[0]=='\n'){
            cout<<"6";
        }
        else{
            cout<<"3-";
            for(int i=0;i<14;i++){
                if(word[0]==point[i]){
                    cout<<tobin(i);
                    break;
                }
            }
        }
    }
    cout<<" ";
}
void word_to_code(){
    string word;
    cout<<"cin your word:";
    cin>>word;
    while(word!="End"){
        to_code(word);
        cin>>word;
    }
}
void to_word(string code){
    if(code[0]=='0'){
        cout<<getbin(code,2,6);
    }
    if(code[0]=='1'){
        if(code[8]=='1')cout<<char(letters[getbin(code,2,6)]);
        if(code[8]=='2')cout<<char(letters[getbin(code,2,6)]-'a'+'A');
    }
    if(code[0]=='2'){
        int num=code[2]-'0';
        cout<<char(letters[getbin(code,4,8)]-'a'+'A');
        for(int at=10;at<=4+(num-1)*6;at+=6){
            cout<<char(letters[getbin(code,at,at+4)]);
        }
        cout<<code[4+num*6];
    }
    if(code[0]=='3'){
        cout<<point[getbin(code,2,6)];
    }
    if(code[0]=='4'){
        cout<<"   ";
    }
    if(code[0]=='5'){
        cout<<"      ";
    }
    if(code[0]=='6'){
        cout<<endl;
    }else{
        cout<<" ";
    }
}
void code_to_word(){
    string code;
    cout<<"cin your code:";
    cin>>code;
    while(code!="End"){
        to_word(code);
        cin>>code;
    }
}
int main(){
    cout<<"word-to-code or code-to-word ?"<<endl;
    string type;
    cin>>type;
    if(type=="word-to-code")word_to_code();
    else if(type=="code-to-word")code_to_word();
    else cout<<"Error"<<endl;
    return 0;
}