Java 输出给出了相同的结果

Java 输出给出了相同的结果,java,algorithm,encryption,cypher,Java,Algorithm,Encryption,Cypher,我正在尝试运行此代码,但它给出了相同的输出: 我想做的是要求用户输入要加密的文本。然后输入他想要用来加密128、192或256的密钥。但当我测试它时,无论何时输入任何内容,它都会给出相同的输出: 这就是我试图调用的方法 package test; public class MARS { public static byte[] encrypt(byte[] in,byte[] key){ K = expandKey(key); int lenght=0;

我正在尝试运行此代码,但它给出了相同的输出: 我想做的是要求用户输入要加密的文本。然后输入他想要用来加密128、192或256的密钥。但当我测试它时,无论何时输入任何内容,它都会给出相同的输出: 这就是我试图调用的方法

package test;

public class MARS {
    public static byte[] encrypt(byte[] in,byte[] key){
        K = expandKey(key);
        int lenght=0;
        byte[] padding = new byte[1];
        int i;
        lenght = 16 - in.length % 16;
        padding = new byte[lenght];
        padding[0] = (byte) 0x80;

        for (i = 1; i < lenght; i++)
            padding[i] = 0;

        byte[] tmp = new byte[in.length + lenght];
        byte[] bloc = new byte[16];

        int count = 0;

        for (i = 0; i < in.length + lenght; i++) {
            if (i > 0 && i % 16 == 0) {
                bloc = encryptBloc(bloc);
                System.arraycopy(bloc, 0, tmp, i - 16, bloc.length);
            }
            if (i < in.length)
                bloc[i % 16] = in[i];
            else{
                bloc[i % 16] = padding[count % 16];
                count++;
            }
        }
        if(bloc.length == 16){
            bloc = encryptBloc(bloc);
            System.arraycopy(bloc, 0, tmp, i - 16, bloc.length);
        }

        return tmp;
    }
}

你的代码完全没有意义。您得到的是作为明文和键的字符串文本字节,实际上您忽略了用户输入。您还创建了多个未使用的输入流,并在奇怪的时候提示用户输入

下面是它的外观:

// Create input reader
BufferedReader userInput = new BufferedReader(new InputStreamReader(System.in));

// Prompt, then wait and get user input for the plaintext 
System.out.println("Enter Text that you want to encrypt:");
String plainText = userInput.readLine();

// Prompt, then wait and get user input for the key
System.out.println("Enter the key:");
String key = userInput.readLine();

// Actually encrypt it
byte[] encrypted = MARS.encrypt(plainText.getBytes(), key.getBytes());

// Print encrypted and unencrypted
System.out.println("Plain text: " + plainText);
System.out.println("Encrypted Text: " + new String(encrypted));

即使MARS已经被定义,你总是加密静态数据数组,密钥…抱歉,伙计们,我在编辑代码,我只是添加了我试图从其他类调用的代码。这就是其中的一部分,我试图调用静态数据的方法意味着不能要求用户添加他自己想要添加的文本?换句话说,静态意味着总是相同的输出?无论用户在运行程序时输入什么,都只会更改一个输出行,并在其中重复给他。否则你的程序是完全确定的。啊哈,我明白了。谢谢你真是太棒了谢谢你组织我的代码。实际上,我没有注意组织代码,因为稍后我将把这些代码带到GUI设计中。无论如何,谢谢你,这是有道理的,现在我真的很感激@小姐,当然。很乐意帮忙。
// Create input reader
BufferedReader userInput = new BufferedReader(new InputStreamReader(System.in));

// Prompt, then wait and get user input for the plaintext 
System.out.println("Enter Text that you want to encrypt:");
String plainText = userInput.readLine();

// Prompt, then wait and get user input for the key
System.out.println("Enter the key:");
String key = userInput.readLine();

// Actually encrypt it
byte[] encrypted = MARS.encrypt(plainText.getBytes(), key.getBytes());

// Print encrypted and unencrypted
System.out.println("Plain text: " + plainText);
System.out.println("Encrypted Text: " + new String(encrypted));