0

この暗号化アルゴリズムを数日間空き時間に書いていて、やっと動くようになったと思ったのですが、特定の文字に適用すると誤動作し始めました。文字のシフトのサイクリングキーで置換を実行するように、この設定を行いました。問題は、1 文字だけが翻訳された後に切り取られることです。復号化コードは次のとおりです。

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
as the key, and
R EWW'U(AO)TP(MO!\QAU) as the ciphertext. It should come out to

私はそれをしたくありません!

誰かがコードを修正して、それらの答えをあきらめないようにできるかどうか知りたいだけです。

4

1 に答える 1

0

問題は、アルゴリズムではなく、入力処理にあります。デフォルトでは、java.util.Scanner は空白文字 (入力文字列の 2 番目の文字であるスペースを含む) でトークンを区切ります。したがって、in.next() への呼び出しは、1 文字 ('R') の文字列を返し、それが処理されて 1 文字の出力を返します。

これを修正する簡単な方法の 1 つは、next の代わりに Scanner.nextLine() を使用して入力テキストを取得することです。これにより、行のすべての文字 (スペースを含む) が取得されます。

System.out.print("Enter your Cipher Text message :");
in.nextLine();
plainTxt = new String(in.nextLine());
于 2011-02-02T06:22:06.933 に答える