2013-12-20 18 views
0

我有這個java代碼,它只加密給定的文本女巫已經寫在代碼中 我如何編輯此代碼以使程序詢問用戶輸入文本,然後對文本進行加密並顯示最終結果?我試圖更換文字("NagaSakti");和("bismillah");(System.in);但它沒有工作!請幫我如何讓用戶在java代碼中輸入文本進行加密

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

    } 
} 

回答

-1

您可以使用掃描儀類。 添加此導入:在你的主要方法,然後

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

如果你想使用其他NagaSakti一個密碼,然後開始改變用繩子線加密...到

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

請注意,您還必須將DesEncrypter構造函數更改爲public來執行此操作。

+0

非常感謝!最後它的工作! – user3124061

0

嘗試是這樣的:

// 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!"); 
    } 
1

使用Console來讀取密碼。你main方法看起來是這樣的:

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可以清楚地瞭解您的代碼在做什麼,未來功能的改進,並簡化您的應用程序。

使用加密還有其他一些問題,我不建議任何人按原樣使用代碼,但我已將重點放在提出的問題上。

相關問題