Java 流密码(输入和输出 txt 文件)输出随机字符不完整的解密字符串

Java Stream Cipher (With input and output txt files) outputting random characters not complete decrypted string

不知道我哪里出错了,但我的程序应该是一个流密码,它采用 chars 的 input.txt 文件并将其加密成数字,然后将其解密回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 + " ");

        }

    }

}

在这两种情况下,您使用的随机数生成器有所不同。在您的加密代码中,您生成 one 个随机数,并将其用于 all 个字符:

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 + " ");
    }
}

在你的解密代码中,你生成一个新的随机数每个字符

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

解决此问题的最佳方法(例如,为了避免每个 'e' 始终加密为相同的数字)是在加密时为每个字符使用一个新的随机数:

Random rng = new Random(key);
while (scan.hasNextLine())
{
    String s = scan.nextLine();
    for (int i = 0; i < s.length(); i++)
    {
        char allChars = s.charAt(i);
        int randomNum = rng.nextInt(256);
        int cipherNums = allChars ^ randomNum;
        System.out.print(cipherNums + " ");
    }
}

(当然,此代码不应该用于 真正的 加密 - 我假设它仅用于教育目的。)