首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Caesar密码的暴力破解

Caesar密码的暴力破解
EN

Stack Overflow用户
提问于 2021-04-10 11:23:30
回答 1查看 206关注 0票数 0

这是我用经典的凯撒密码破解的代码。使用暴力破解显示了奇怪的结果,因为当我将密钥设置为3时,它会多次发送相同的解密字符串。在其他密钥中,例如7,它甚至不会显示正确的解密字符串。

我使用暴力破解的方法是根据字母表更改加密消息中的每个字母,因此有一个26次的for循环,以及另一个消息长度的for循环。它使用StringBuilder setCharAt方法更改字符串中的字符。

这是我使用暴力破解的代码:

代码语言:javascript
复制
    void decryptbruteforce(String encryptmessage) {
    
    //Get the standard alphabet
    String standalpha = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
    
    //Convert this message to uppercase
    String encryptmessageupper = encryptmessage.toUpperCase();
    
    StringBuilder sbdecrypt = new StringBuilder(encryptmessageupper);
    
    
    int key;
    int i;
    int index;
    char currentchar;
    char newchar;
    
    //Loop through the 26 keys in the alphabet.
    for (key = 1; key < 27; key++) {
        
        //Loop through the encrypted message
        for (i = 0; i < sbdecrypt.length(); i++) {
            
            //Get the encrypted character
            currentchar = sbdecrypt.charAt(i);
            
            //Get the index in the alphabet
            index = standalpha.indexOf(currentchar);
            
            //If the currentchar is in the alphabet
            if (index != -1) {
                
                //Reduce the character by the key in the alphabet
                index = index - key;
                
                //If the character goes below 0, aka 'A', go back to the end of the alphabet
                if (index < 0) {
                    index = index + 26;
                    
                    //Get the new character in the alphabet
                    newchar = standalpha.charAt(index);
                    
                    //Set the character in the stringbuilder
                    sbdecrypt.setCharAt(i, newchar);
                }
                
                else {
                    
                    //Get the new character in the alphabet
                    newchar = standalpha.charAt(index);
                    
                    //Set the character in the stringbuilder
                    sbdecrypt.setCharAt(i, newchar);
                }
            }
        }
        
        //Print the key and the resulting string
        System.out.println("Key: " + key + " Decrypted String: " + sbdecrypt);
    }
}

这是我的输出:Caesar Cipher key of 3 Caesar Cipher key of 7

这是我的整个代码的链接,如果有帮助的话:https://gist.github.com/cliven-hew/35e9458c24d5f5b1ace97b7146ec429a

EN

回答 1

Stack Overflow用户

发布于 2021-04-10 13:59:59

您的问题是您总是在编辑同一个StringBuilder sbdecrypt对象。因此,当您使用键1进行移位时,您的下一次尝试不是基于移位原始字符串,而是先前移位的字符串。这就是为什么您从N....转到L....,而跳过M....,并且这种模式从头到尾都在继续。

您可以通过在for (key = 1; key < 27; key++)行之后添加sbdecrypt = new StringBuilder(encryptmessageupper);来修复此问题。

代码语言:javascript
复制
  public static void decryptbruteforce(String encryptmessage) {

    String standalpha = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
    String encryptmessageupper = encryptmessage.toUpperCase();
    StringBuilder sbdecrypt = new StringBuilder(encryptmessageupper);
    
    int key;
    int i;
    int index;
    char currentchar;
    char newchar;

    for (key = 1; key < 27; key++) {
      //Ensure you do your shift on the same string each time.
      //This makes a new object each time
      sbdecrypt = new StringBuilder(encryptmessageupper);

      for (i = 0; i < sbdecrypt.length(); i++) {
        currentchar = sbdecrypt.charAt(i);
        index = standalpha.indexOf(currentchar);
        if (index != -1) {
          index = index - key;
          if (index < 0) {
            index = index + 26;
          }
          newchar = standalpha.charAt(index);
          sbdecrypt.setCharAt(i, newchar);
        }
      }
      System.out.println("Key: " + key + " Decrypted String: " + sbdecrypt);
    }
  }
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/67030516

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档