0

どこで間違いを犯したのかはわかりませんが、私のプログラムは、input.txt ファイルを取得charsして数値に暗号化し、復号化して に戻すストリーム暗号であると想定されていますchars

私の問題は、次のように入力することです。

java Program4 -e 71 < inp.txt > out.txt

(txtを出力ファイルに暗号化すると正常に動作します)入力ファイルは次のようになります。

guess what? Chicken butt

出力ファイルは次のようになります。

222 204 220 202 202 153 206 209 216 205 134 153 250 209 208 218 210 220 215 153 219 204 205 205

次に、ファイルを復号化すると..

java Program4 -d 71 < out.txt

次のようになります。

g  E ?  ? 8 º ? Ä ì  ß ê ( ? ½ ^ ~ ? ? X  ?

何が間違っているのかわかりませんが、それは私の復号化方法や、暗号化がいくつかの値で同じ数値を与える方法にあると思いますか? 助けていただければ幸いです。

import java.util.Scanner;
import java.util.Random;
public class Program4
{
public static void main(String[] args)
{
    if(args.length < 2)
    {
        usage();
    }
    else if(args[0].equals("-e"))
    {
        encrypt(args);
    }
    else if(args[0].equals("-d"))
    {
        decrypt(args);
    }

}   
        //Intro (Usage Method)
    public static void usage()
    {
        System.out.println("Stream Encryption program by my name");
        System.out.println("usage: java Encrypt [-e, -d] < inputFile > outputFile" );
    }
        //Encrypt Method
        public static void encrypt(String[] args)
    {   Scanner scan = new Scanner(System.in);
        String key1 = args[1];
        long key = Long.parseLong(key1);
        Random rng = new Random(key);
        int randomNum = rng.nextInt(256);
        while (scan.hasNextLine())
        {
            String s = scan.nextLine();
            for (int i = 0; i < s.length(); i++)
            {
                char allChars = s.charAt(i);
                int cipherNums = allChars ^ randomNum;
                System.out.print(cipherNums + " ");
            }

        }
    }   

        //Decrypt Method
        public static void decrypt(String[] args)
    {   String key1 = args[1];
        long key = Long.parseLong(key1);
        Random rng = new Random(key);
        Scanner scan = new Scanner(System.in);

            while (scan.hasNextInt())
        {
            int next = scan.nextInt();
            int randomNum = rng.nextInt(256);
            int decipher = next ^ randomNum;
            System.out.print((char)decipher + " ");

        }

    }

}
4

1 に答える 1