开发者

Strange bug in decryption program

I have been writing this encryption algorithm in my free time for a few days, and I thought I finally had it working, but it started malfunctioning when I subject certain characters to it. I had this set up to perform a substitution with a cycling key for the shift in characters. The issue is that is cuts out after just the one character being translated. The decryption code is below:

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 + " successfu开发者_如何学编程lly 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

as the key, and

R EWW'U(AO)TP(MO!\QAU) as the ciphertext. It should come out to

I DON'T WANT TO DO THAT!`

I just want to know if anyone can fix the code so it doesn't give up with those answers.


The problem is in your input handling, not your algorithm. java.util.Scanner, by default, delimits tokens on whitespace characters (including the space which is the second character of your input string). So your call to in.next() is returning a String with a single character ('R'), which is then processed and returns a single character of output.

One quick way to fix it is to grab your input text using Scanner.nextLine() instead of next, which will get all of the characters on the line (including the space):

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

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜