2014-05-10 2 views
0

저는 Java에서 새로운 기능입니다. 나는 무차별 대입을 사용하여 Caesar의 암호를 해독 한 다음 사전에서 단어를 찾아 올바른 이동을 찾기위한 알고리즘을 작성해야합니다. 그것은 지금까지 작성한 코드입니다. 누군가가 편지를 이동하고 dictionary.txt 파일과 일치시키는 것을 도와 주면 정말 고맙습니다.Java caesars cipher brute forcing

decodedText[i] = alphabet[(alphabetList.indexOf(message[i])+key) % alphabet.length]; 

당신은 아마 반복을 시작해야합니다 후 다음 작업을 수행 할 수 있습니다 내부

private List<Character> alphabetList = java.util.Arrays.asList(alphabet); 

:

private char[] alphabet = {'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'}; 
private char[] decodedText; 
private String plainText; 

public String producePlaintext(String cipherText) { 
    //put each letter of the ciphertext in an array of characters in the upper case format 
    char[] message = cipherText.toUpperCase().toCharArray(); 
    //loop through all the possible keys 
    for (int key = 0; key < alphabet.length; key++) { 
     //set the value of the decrypted array of characters to be the same as the length of the cipher text 
     decodedText = new char[message.length]; 
     //loop through the characters of the ciphertext 
     for (int i = 0; i < message.length; i++) { 

      //if character is not space 
      if (message[i] != ' ') { 
       //shift the letters 

      }else{ 
       decodedText[i] = ' '; 
      } 
     } 
     plainText = String.valueOf(decodedText); 
    } 
    return plainText; 
} 
+0

구체적으로 어떤 문제가 있습니까? 생성 된 일반 텍스트가 실제 일반 텍스트라는 확인 함수가 필요할 것입니다. 그렇지 않으면 해독 할 수 없습니다. 그러나 가능한 모든 26 개의 일반 텍스트를 인쇄하고 알려진 단어를 찾아 실제 일반 텍스트를 선택하려고 할 수 있습니다. 프로그래밍 방식으로도이 작업을 수행 할 수 있지만 단어 목록이 필요합니다. –

+0

제 문제는 가능한 모든 일반 텍스트를 인쇄하는 것입니다. – user3451112

답변

1

당신은 indexOf 방법을 사용 List에 알파벳 배열을 변환해야 key1이 아니라 0이 아닌 암호문을 얻을 수 있기 때문에 뒤로.

완벽한 솔루션 :

public class Test01 { 

    private Character[] alphabet = {'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'}; 
    private char[] decodedText; 
    private String[] plainText; 
    private java.util.List<Character> alphabetList; 

    public Test01(){ 
     alphabetList = java.util.Arrays.asList(alphabet); 
     plainText = new String[alphabet.length]; 
    } 

    public String[] producePlaintext(String cipherText) { 
     //put each letter of the ciphertext in an array of characters in the upper case format 
     char[] message = cipherText.toUpperCase().toCharArray(); 
     //loop through all the possible keys 
     for (int key = 0; key < alphabet.length; key++) { 
      //set the value of the decrypted array of characters to be the same as the length of the cipher text 
      decodedText = new char[message.length]; 
      //loop through the characters of the ciphertext 
      for (int i = 0; i < message.length; i++) { 

       //if character is not space 
       if (message[i] != ' ') { 
        //shift the letters 
        decodedText[i] = alphabet[(alphabetList.indexOf(message[i])+key) % alphabet.length]; 
       }else{ 
        decodedText[i] = ' '; 
       } 
      } 
      plainText[key] = String.valueOf(decodedText); 
     } 
     return plainText; 
    } 

    public static void main(String[] args) { 
     Test01 t = new Test01(); 
     for(String pt : t.producePlaintext("abc")) { 
      System.out.println(pt); 
     } 
    } 

} 

참고 문자 유형의 차이.

+0

'j' 변수는 무엇입니까? 그것 대신'i'를 사용하면 ZZZ를 일반 텍스트로 계속 사용합니다. – user3451112

+0

최종 해결책은 거기에 있습니다. 마지막 평문뿐만 아니라 모든 평문을 반환해야했습니다. –