输出结果相同 [英] The output gives the same result

查看:71
本文介绍了输出结果相同的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图运行此代码,但它提供相同的输出:我要做的是要求用户输入要加密的文本。然后输入他想要用来加密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 = < span class =code-digit> 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%< span class =code-digit> 16 ];
count ++;
}
}
如果(bloc.length == 16 ){
bloc = encryptBloc(bloc);
System.arraycopy(bloc, 0 ,tmp,i - 16 ,bloc.length) ;
}

return tmp;
}
}







这是调用方法的类别/>


  package  test; 
import java.io.BufferedReader;
import java.io.InputStreamReader;

public class 致电{
public static void main( String [] args) throws 异常{
byte [] array = 要加密 .getBytes();
byte [] key = 密钥你想用 .getBytes();
byte [] encryptBloc = MARS.encrypt(array,key);
BufferedReader userInput = new BufferedReader( new InputStreamReader(System.in));
String plainText = userInput.readLine();
System.out.println( 输入要加密的文本: + plainText );

BufferedReader ke = new BufferedReader( new InputStreamReader(System.in)) ;
String kee = userInput.readLine();
System.out.println( 输入您要用来加密此消息的密钥: + new String (key));
System.out.println( 纯文本: + plainText);
System.out.println( 加密文本: + new String (encryptBloc));
}
}





我的尝试:



尝试读取用户输入:(

解决方案

 BufferedReader userInput = new BufferedReader(new InputStreamReader) (System.in)); 
System.out.println(输入要加密的文本:);
String plainText = userInput.readLine();
System.out。 println(输入密钥:);
String key = userInput.readLine();
byte [] encrypted = MARS.encrypt(plainText.getBytes(),key.getBytes())
System.out.println(纯文本:+ plainText);
System.out.println(加密文本:+新字符串(加密));


Im trying to run this code but it gives the same output: What I’m trying to do is that ask the user to enter text to be encrypted. Then enter the key he wants to use to encrypt either 128, 192, or 256. But when I test it whenever I enter anything it gives the same output :( this is the method i'm trying to call

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;
    }
}




this is the class of calling method

package test;
import java.io.BufferedReader;
import java.io.InputStreamReader;

public class Call {
    public static void main(String[] args) throws Exception {
        byte[] array = "going to encrypt".getBytes();
        byte[] key = "the key you want to use".getBytes();
        byte[] encryptBloc = MARS.encrypt(array,key);
        BufferedReader userInput = new BufferedReader(new InputStreamReader(System.in));
        String plainText = userInput.readLine();
        System.out.println("Enter Text that you want to Encrypt: " + plainText);

        BufferedReader ke = new BufferedReader(new InputStreamReader(System.in));
        String kee = userInput.readLine();
        System.out.println("Enter The Key you want to use to encrypt this message: " + new String(key));
        System.out.println("plain Text: "+ plainText);
        System.out.println("Encrypted Text: " + new String(encryptBloc));
    }
}



What I have tried:

tried to read the user input :(

解决方案

BufferedReader userInput = new BufferedReader(new InputStreamReader(System.in));
System.out.println("Enter Text that you want to encrypt:");
String plainText = userInput.readLine();
System.out.println("Enter the key:");
String key = userInput.readLine();
byte[] encrypted = MARS.encrypt(plainText.getBytes(), key.getBytes())
System.out.println("Plain text: " + plainText);
System.out.println("Encrypted Text: " + new String(encrypted));


这篇关于输出结果相同的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆