Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/357.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何要求用户在java代码中输入加密文本_Java_Security_Encryption_Cryptography - Fatal编程技术网

如何要求用户在java代码中输入加密文本

如何要求用户在java代码中输入加密文本,java,security,encryption,cryptography,Java,Security,Encryption,Cryptography,我有这个java代码,它只加密一个给定的文本,这个文本已经写在代码中了 如何编辑此代码,使程序要求用户输入文本,然后对文本进行加密并显示最终结果?我试图替换文本(“长崎”);和(“bismillah”)带有(System.in)但它不起作用!!请帮帮我 import java.io.UnsupportedEncodingException; import java.security.spec.AlgorithmParameterSpec; import java.security.spec.Ke

我有这个java代码,它只加密一个给定的文本,这个文本已经写在代码中了 如何编辑此代码,使程序要求用户输入文本,然后对文本进行加密并显示最终结果?我试图替换文本
(“长崎”)
;和
(“bismillah”)带有
(System.in)但它不起作用!!请帮帮我

import java.io.UnsupportedEncodingException;
import java.security.spec.AlgorithmParameterSpec;
import java.security.spec.KeySpec;
import javax.crypto.Cipher;
import javax.crypto.IllegalBlockSizeException;
import javax.crypto.SecretKey;
import javax.crypto.SecretKeyFactory;
import javax.crypto.spec.PBEKeySpec;
import javax.crypto.spec.PBEParameterSpec;



public class DesEncrypter {
  Cipher ecipher;


    // 8-byte Salt
    byte[] salt = {
        (byte)0xA9, (byte)0x9B, (byte)0xC8, (byte)0x32,
        (byte)0x56, (byte)0x35, (byte)0xE3, (byte)0x03
    };

    // Iteration count
    int iterationCount = 19;
    public static final DesEncrypter NAGASAKTI = new DesEncrypter("NagaSakti");

    private DesEncrypter(String passPhrase) {
        try {
            // Create the key
            KeySpec keySpec = new PBEKeySpec(passPhrase.toCharArray(), salt, iterationCount);
            SecretKey key = SecretKeyFactory.getInstance(
                "PBEWithMD5AndDES").generateSecret(keySpec);
            ecipher = Cipher.getInstance(key.getAlgorithm());


            // Prepare the parameter to the ciphers
            AlgorithmParameterSpec paramSpec = new PBEParameterSpec(salt, iterationCount);

            // Create the ciphers
            ecipher.init(Cipher.ENCRYPT_MODE, key, paramSpec);

        } catch (java.security.InvalidAlgorithmParameterException e) {
        } catch (java.security.spec.InvalidKeySpecException e) {
        } catch (javax.crypto.NoSuchPaddingException e) {
        } catch (java.security.NoSuchAlgorithmException e) {
        } catch (java.security.InvalidKeyException e) {
        }
    }

    public String encrypt(String str) {
        try {
            // Encode the string into bytes using utf-8
            byte[] utf8 = str.getBytes("UTF8");

            // Encrypt
            byte[] enc = ecipher.doFinal(utf8);

            // Encode bytes to base64 to get a string
            return new sun.misc.BASE64Encoder().encode(enc);
        } catch (javax.crypto.BadPaddingException e) {
        } catch (IllegalBlockSizeException e) {
        } catch (UnsupportedEncodingException e) {
        }
        return null;
    }



    public static void main(String args[]){
       String encrypted = DesEncrypter.NAGASAKTI.encrypt("bismillah"); 
        System.out.println("Enter your text");  


      System.out.println("encrypted text=  "+ encrypted);

    }
}

试着这样做:

  //  open up standard input
  BufferedReader br = new BufferedReader(new InputStreamReader(System.in));

  String textFromUser = null;

  //  read the text from the command-line; need to use try/catch with the
  //  readLine() method
  try {
     textFromUser = br.readLine();
  } catch (IOException ioe) {
     System.out.println("IO error trying to read your text!");
  }
public static void main(String args[])
{
  Console console = System.console();
  if (console == null)
    throw new IllegalStateException("console required");
  char[] password = console.readPassword("Enter your text: ");
  DesEncrypter encrypter = new DesEncrypter(new String(password));
  String encrypted = encrypter.encrypt("bismillah");
  System.out.println("encrypted text =  " + encrypted);
}

您可以使用Scanner类。 添加此导入:

import java.util.Scanner;
然后在主要方法中执行以下操作:

public static void main(String args[]){
    Scanner keyboard = new Scanner(System.in);
    System.out.println("Enter your text");  
    String textToEncrypt = keyboard.next();

    String encrypted = DesEncrypter.NAGASAKTI.encrypt(textToEncrypt);

    System.out.println("encrypted text=  "+ encrypted);
}
如果您想使用长崎以外的密码短语,请更改以字符串加密开头的行。。。到

System.out.println("Enter your pass phrase");  
String passPhrase = keyboard.next();
String encrypted = new DesEncrypter(passPhrase).encrypt(textToEncrypt);

注意:要执行此操作,您还必须将DesEncrypter构造函数更改为public。

使用
控制台读取密码。您的
main
方法可能如下所示:

  //  open up standard input
  BufferedReader br = new BufferedReader(new InputStreamReader(System.in));

  String textFromUser = null;

  //  read the text from the command-line; need to use try/catch with the
  //  readLine() method
  try {
     textFromUser = br.readLine();
  } catch (IOException ioe) {
     System.out.println("IO error trying to read your text!");
  }
public static void main(String args[])
{
  Console console = System.console();
  if (console == null)
    throw new IllegalStateException("console required");
  char[] password = console.readPassword("Enter your text: ");
  DesEncrypter encrypter = new DesEncrypter(new String(password));
  String encrypted = encrypter.encrypt("bismillah");
  System.out.println("encrypted text =  " + encrypted);
}
使用
控制台
类”专用API有一些优点

首先,您不会将密码回显到屏幕上。这有助于保护它免受肩上冲浪强盗的袭击

此外,密码以字符数组的形式返回,这样当密码使用完成时,应用程序可以用零或随机字符填充数组。这样可以最大限度地减少由于分页或包含在堆转储等原因而将其写入磁盘的可能性

最后,使用正确的高级API可以清楚地了解您的代码在做什么,利用将来对该特性的任何改进,并简化您的应用程序

使用的加密还有许多其他问题,我不建议任何人按原样使用代码,但我已将重点放在提出的问题上