首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何要求用户输入java代码中的加密文本

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

Stack Overflow用户
提问于 2013-12-20 22:11:20
回答 3查看 924关注 0票数 0

我有这个java代码,它只加密一个给定的文本,在代码中已经写好了,我怎样才能编辑这段代码,让程序让用户输入文本,然后对文本进行加密,并显示最终结果呢?我试着用("NagaSakti")代替文本("bismillah");,用(System.in);代替("bismillah");,但是它没有工作!!请帮帮我

代码语言:javascript
复制
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);

    }
}
EN

回答 3

Stack Overflow用户

回答已采纳

发布于 2013-12-20 22:50:57

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

代码语言:javascript
复制
import java.util.Scanner;

然后在您的主要方法中这样做:

代码语言:javascript
复制
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);
}

如果您想使用NagaSakti以外的密码,请更改以加密字符串开头的行.至

代码语言:javascript
复制
System.out.println("Enter your pass phrase");  
String passPhrase = keyboard.next();
String encrypted = new DesEncrypter(passPhrase).encrypt(textToEncrypt);

注意,您还必须将DesEncrypter构造函数更改为public。

票数 -1
EN

Stack Overflow用户

发布于 2013-12-20 23:04:52

使用Console读取密码。main方法可能如下所示:

代码语言:javascript
复制
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);
}

使用Console类的专用API有一些优点。

首先,不要将密码回显到屏幕上。这有助于保护它不受冲浪匪徒的影响。

此外,密码作为字符数组返回,以便在密码使用完成时,应用程序可以用零或随机字符填充数组。这减少了由于分页而将其写入磁盘或包含在堆转储中的可能性,等等。

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

所使用的加密还有许多其他问题,我不建议任何人按原样使用该代码,但我关注的是所提出的问题。

票数 1
EN

Stack Overflow用户

发布于 2013-12-20 22:50:49

试着做这样的事情:

代码语言:javascript
复制
  //  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!");
  }
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/20712738

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档