ZBLOG

转为qt def encrypt(plainText, key): alphabet = [chr(65 + x) for x in range(26)] cipherTable = [] count1 = 0 for x in range(26): row = alphabet[count1:] + alphabet[:count1] cipherTable.append(row) count1 += 1 ...

下面是将你提供的Python代码转换为Qt(C++)代码的示例。这个实现基于你的Python代码进行Autokey加密和解密。Qt中的字符串处理与Python略有不同,但基本逻辑保持一致。

请注意,这段代码依赖于Qt框架,因此在使用时需要确保已安装Qt开发环境。

#include <iostream>
#include <vector>
#include <string>
#include <algorithm>

std::string encrypt(const std::string& plainText, const std::string& key) {
    std::vector<char> alphabet(26);
    for (int i = 0; i < 26; ++i) {
        alphabet[i] = 'A' + i;
    }

    std::vector<std::vector<char>> cipherTable(26);
    for (int count1 = 0; count1 < 26; ++count1) {
        cipherTable[count1] = std::vector<char>(alphabet.begin() + count1, alphabet.end());
        cipherTable[count1].insert(cipherTable[count1].end(), alphabet.begin(), alphabet.begin() + count1);
    }

    std::vector<char> plainTextList;
    for (char c : plainText) {
        if (isalpha(c)) {
            plainTextList.push_back(toupper(c));
        }
    }

    std::vector<char> keyList;
    for (char c : key) {
        keyList.push_back(toupper(c));
    }

    int count2 = 0;
    std::vector<char> cipherTextList, cipherTextPartList;

    for (char x : plainTextList) {
        cipherTextPartList.push_back(cipherTable[keyList[count2] - 'A'][x - 'A']);
        count2++;

        if (count2 == keyList.size()) {
            cipherTextList.insert(cipherTextList.end(), cipherTextPartList.begin(), cipherTextPartList.end());
            keyList = cipherTextPartList;
            cipherTextPartList.clear();
            count2 = 0;
        }
    }

    if (!cipherTextPartList.empty()) {
        cipherTextList.insert(cipherTextList.end(), cipherTextPartList.begin(), cipherTextPartList.end());
    }

    return std::string(cipherTextList.begin(), cipherTextList.end());
}

std::string decrypt(const std::string& cipherText, const std::string& key) {
    std::vector<char> alphabet(26);
    for (int i = 0; i < 26; ++i) {
        alphabet[i] = 'A' + i;
    }

    std::vector<std::vector<char>> cipherTable(26);
    for (int count1 = 0; count1 < 26; ++count1) {
        cipherTable[count1] = std::vector<char>(alphabet.begin() + count1, alphabet.end());
        cipherTable[count1].insert(cipherTable[count1].end(), alphabet.begin(), alphabet.begin() + count1);
    }

   // Prepare the reversed lists
   std::vector<char> keyReversed(key.rbegin(), key.rend());
   std::vector<char> ciphertextReversed(cipherText.rbegin(), ciphertext.rend());

   // Convert to uppercase
   for(auto &c : ciphertextReversed)
       c = toupper(c);

   // Decrypt process
   std::vector<char> plainTextList;
   int lenKey = key.size();

   for(size_t x=0; x<ciphertextReversed.size(); ++x){
       char currentCharIndexKey;

       // If we are in the first segment of the decrypted text
       if(x<lenKey){
           currentCharIndexKey=ciphertextReversed[x+lenKey];
       } else{
           currentCharIndexKey=keyReversed[x-lenKey];
       }
       
       auto foundIndex=std::find(cipherTable[currentCharIndexKey - 'A'].begin(),
                                  cipherTable[currentCharIndexKey - 'A'].end(),
                                  ciphertextReversed[x]);

       if(foundIndex !=cipherTable[currentCharIndexKey-'A'].end()){
           plainTextList.push_back('A' + foundIndex -cipherTable[currentCharIndexKey-'A'].begin());
       }
       
   }


   // Append last part with reversed keys  
   for(size_t z=lenKey-1 ; z<(ciphertextReversed.size()-lenKey); ++z){
      auto foundIndex=std::find(cipherTable[key[z]-'A'].begin(),
                                  cipherTable[key[z]-'A'].end(),
                                  ciphertextReversed[z]);

      if(foundIndex !=cipherTable[key[z]-'A'].end()){
          plainTezt+=" " ;   
          plainTezt+='A'+foundIndex-cipherTabel[key[z]-'A'].begin();
      }
 
  }

// Final assembly of string and returning 
return {plainTezt.rbegin(),plainTezt.rend()};
}

int main() {
     QString plaintext("hello");
     QString key("eii");

     QString ciphertext(QString:fromStdString(encrypt(plaintext.toStdString(),key.toStdString())));
     QString decrypted(QString:fromStdString(decrypt(ciphertext.toStdString(),key.toStdString())));

     qDebug() << "Ciphertext:" << ciphertext;
     qDebug() << "Decrypted Text:" << decrypted;

     return 0;
}

注意事项:

  • 请确保在运行这段C++代码之前,已经包含了相应的<iostream><algorithm>等头文件,并且配置好Qt环境。
  • 在生产中使用时,请考虑加入更多的错误检查和边界条件处理,以保证健壮性。
  • 根据实际需求,可以进一步优化性能和可读性。


    内容由零声教学AI助手提供,问题来源于学员提问

本站部分文章来源于网络,版权归原作者所有,如有侵权请联系站长删除。
转载请注明出处:http://sdn.0voice.com/?id=6768

分享:
扫描分享到社交APP
上一篇
下一篇
发表列表
游客 游客
此处应有掌声~
评论列表

还没有评论,快来说点什么吧~

联系我们

在线咨询: 点击这里给我发消息

微信号:3007537140

上班时间: 10:30-22:30

关注我们
x

注册

已经有帐号?