2011-02-02 5 views
0

며칠 동안 자유 시간에이 암호화 알고리즘을 작성했으며 마침내 작동한다고 생각했지만 특정 문자를 접할 때 오작동하기 시작했습니다. 캐릭터의 이동을 위해 사이클링 키로 대체를 수행하도록 설정했습니다. 문제는 그것이 번역되고있는 한명의 캐릭터가 끝난 후에 상처를 낸다는 것입니다. 암호 해독 코드는 아래와 같습니다! 암호 해독 프로그램의 이상한 버그

import java.util.Scanner; 
import java.io.*; 
/* File CycleDeCipher.java*/ 

public class CycleDeCipher 
{ 
    public static void main(String[] args) 
    { 
      new CycleDeCipher(); 
    } 
    public CycleDeCipher() 
    { 
      String plainTxt; 
      Scanner in = new Scanner(System.in); 
      System.out.println("This program decrypts My Cyclical Substitution Algorithm. v0.2"); 
      System.out.println("Enter a multi digit number : "); 
      Long mainKey = new Long(in.nextLong());; 
      System.out.print("Enter your Cipher Text message :"); 
      in.nextLine(); 
      plainTxt = new String(in.next()); 
      in.nextLine(); 
      int[] keys = longParser(mainKey); 
      String cipherTxt=""; 
      int j = 0; 
      while(j < plainTxt.length()) 
      { 
        cipherTxt+=decryptCharacter(plainTxt.charAt(j),keys[j%4]); 
        j++; 
        System.out.println("char number " + j + " successfully translated!"); 
      } 
      System.out.println("Your text is translated to :"+cipherTxt.toUpperCase()); 
    } 
    private String decryptCharacter(Character ch, int key) 
    { 
     System.out.println("Decrypting character "+ch.toString() + " with key "+key); 
     if(Character.isLetter(ch)){ 
      ch = (char) ((int) Character.toLowerCase(ch) - key%10); 
     } 
     else { 
      ch = (char) ((int) ch-key%10); 
     } 
     return(ch.toString()); 
    } 
    public int[] longParser(Long key) 
    { 
     System.out.println("Parsing long to crypto keys..."); 
     int i = 0; 
     int[] result; 
     String sInput = new String(key.toString()); 
     char[] keys = new char[sInput.length()]; 
     for(i = 0; i < sInput.length(); i++) 
     { 
      keys[i] = sInput.charAt(i); 
     } 
     i = 0; 
     result = new int[sInput.length()]; 
     for(i=0; i<keys.length; i++) 
     { 
      result[i] = (int) keys[i]; 
     } 
     return result; 
    } 
} 

The input I gave it that broke the program was
123089648734
키로 및 R EWW'U(
AO) TP (MO \ QAU) 암호문으로 . 내가 그렇게하고 싶지 않아

에 나올한다!`

난 그냥 누군가가 코드를 해결할 수 있다면 그 답변을 포기하지 않도록 알고 싶어

.

+1

코드 형식이 내가 가지고있는 방식대로 여기에 오는지 알지 못한다. " 질문을위한 텍스트 영역 바로 아래에 입력 할 때 미리보기 화면이 있습니다. 질문을 서식 지정하는 데 사용할 수 있습니다. – Nishant

+0

나는 그것에 대해 연구하고 있었다. 나는 지금 두세 번 편집하고 그것을 고쳤다. 죄송합니다. –

답변

0

문제는 알고리즘이 아닌 입력 처리에 있습니다. 기본적으로 java.util.Scanner는 공백 문자 (입력 문자열의 두 번째 문자 인 공백 포함)의 토큰을 구분합니다. 따라서 in.next()를 호출하면 단일 문자 ('R')가있는 String이 반환되고 처리 된 다음 출력의 단일 문자가 반환됩니다.

(공간 포함) 라인에 모든 문자를 얻을 것이다, 대신에 다음의)는 Scanner.nextLine를 (사용하여 입력 텍스트를 잡아하는 것입니다 해결하는 한 가지 빠른 방법 : I "

System.out.print("Enter your Cipher Text message :"); 
in.nextLine(); 
plainTxt = new String(in.nextLine()); 
+0

oops. 나는 nextLine()을 이미 사용하고 있다고 생각했다. 고마워요! –