解密和加密函数

· · 个人记录

cipher 为密钥,为 64 位的字符串,最好是 SHA256 加密的内容。

void Encryption(vector<string> &Ciphertext,string cipher){
    for(auto it=cipher.begin();it!=cipher.end();it++) (*it)-=32;
    for(auto it=Ciphertext.begin();it!=Ciphertext.end();it++){
        string ciphertext=*it;
        for(auto it2=ciphertext.begin();it2!=ciphertext.end();it2++) (*it2)-=32;
        //x%96+32
        while(ciphertext.size()<64) ciphertext.push_back(0);
        for(int i=0;i<=32;i++){
            for(int j=0;j<32;j++){
                ciphertext[i+j]=(ciphertext[i+j]+cipher[i+j])%96;
                cipher=cipher.back()+cipher.substr(0,63);
            }
        }
        for(auto it2=ciphertext.begin();it2!=ciphertext.end();it2++) (*it2)+=32;
        (*it)=ciphertext.substr(0,(int)(*it).size());
    }
}
void Decryption(vector<string> &Enphertext,string cipher){
    for(auto it=cipher.begin();it!=cipher.end();it++) (*it)-=32;
    for(auto it=Enphertext.begin();it!=Enphertext.end();it++){
        string enphertext=*it;
        for(auto it2=enphertext.begin();it2!=enphertext.end();it2++) (*it2)-=32;
        //x%96+32
        while(enphertext.size()<64) enphertext.push_back(0);
        for(int i=0;i<=32;i++){
            for(int j=0;j<32;j++){
                enphertext[i+j]=(enphertext[i+j]+96-cipher[i+j])%96;
                cipher=cipher.back()+cipher.substr(0,63);
            }
        }
        for(auto it2=enphertext.begin();it2!=enphertext.end();it2++) (*it2)+=32;
        (*it)=enphertext.substr(0,(int)(*it).size());
    }
}