不知道我哪里出错了,但是我的程序应该是流密码,它采用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 + " ");

        }

    }

}

最佳答案

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

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


(当然,此代码不应用于真正的加密-我认为它仅是出于教育目的。)

08-03 14:25
查看更多