2017-04-17 109 views
-1

我有一个代码,它通过采用扫描器方法加密到Aes-128进行加密。程序运行时,加密终止而不等待响应。我试图在继续使用Java中的println之前,如何让代码等待对象?

try { 

     Thread.sleep(10000); 

    } catch(InterruptedException ex) { 

     Thread.currentThread().interrupt(); 
    } 

,但如果我打的东西真的很长,将加密

java.util.Scanner中的[分隔符= \ p {} javaWhitespace +] [位置= 0] [比赛有效= FALSE ] [正文后缀=] [正文后缀=] [正文后缀=] [正文后缀=] [正文后缀= ] [negative suffix =] [NaN string = \Q \ E] [infinity string = \Q∞\ E]

没有等待扫描器的响应。

下面是代码(这是一个修改代码:http://aesencryption.net/):

package package1; 

    import java.io.UnsupportedEncodingException; 
    import java.security.MessageDigest; 
    import java.security.NoSuchAlgorithmException; 
    import java.util.*; 
    import javax.crypto.Cipher; 
    import javax.crypto.spec.SecretKeySpec; 
    import org.apache.commons.codec.binary.Base64; 

    public class AesOne{ 


    private static SecretKeySpec secretKey; 
    private static byte[] key; 


    private static String decryptedString; 
    private static String encryptedString; 

public static void setKey(String myKey){ 


    MessageDigest sha = null; 
    try { 
     key = myKey.getBytes("UTF-8"); 
     System.out.println(key.length); 
     sha = MessageDigest.getInstance("SHA-1"); 
     key = sha.digest(key); 
     key = Arrays.copyOf(key, 16); 
     System.out.println(key.length); 
     System.out.println(new String(key,"UTF-8")); 
     secretKey = new SecretKeySpec(key, "AES"); 


    } catch (NoSuchAlgorithmException e) { 
     // TODO Auto-generated catch block 
     e.printStackTrace(); 
    } catch (UnsupportedEncodingException e) { 
     // TODO Auto-generated catch block 
     e.printStackTrace(); 
    } 



} 

public static String getDecryptedString() { 
    return decryptedString; 
} 
public static void setDecryptedString(String decryptedString) { 
    AesOne.decryptedString = decryptedString; 
} 
public static String getEncryptedString() { 
    return encryptedString; 
} 
public static void setEncryptedString(String encryptedString) { 
    AesOne.encryptedString = encryptedString; 
} 
public static String encrypt(String strToEncrypt) 
{ 
    try 
    { 
     Cipher cipher = Cipher.getInstance("AES/ECB/PKCS5Padding"); 

     cipher.init(Cipher.ENCRYPT_MODE, secretKey); 


     setEncryptedString(Base64.encodeBase64String(cipher.doFinal(strToEncrypt.getBytes("UTF-8")))); 

    } 
    catch (Exception e) 
    { 

     System.out.println("Error while encrypting: "+e.toString()); 
    } 
    return null; 
} 
public static String decrypt(String strToDecrypt) 
{ 
    try 
    { 
     Cipher cipher = Cipher.getInstance("AES/ECB/PKCS5PADDING"); 

     cipher.init(Cipher.DECRYPT_MODE, secretKey); 
     setDecryptedString(new String(cipher.doFinal(Base64.decodeBase64(strToDecrypt)))); 

    } 
    catch (Exception e) 
    { 

     System.out.println("Error while decrypting: "+e.toString()); 
    } 
    return null; 
} 
public static void main(String args[]) 
{ 



      System.out.println("What would you like to encrypt?"); 

      Scanner encrypt = new Scanner(System.in); 
      String toEncrypt = encrypt.toString(); 

      final String strToEncrypt = toEncrypt; 
      final String strPssword = "encryptor key"; 
      AesOne.setKey(strPssword); 

      AesOne.encrypt(strToEncrypt.trim()); 

      System.out.println("String to Encrypt: " + strToEncrypt); 
      System.out.println("Encrypted: " + AesOne.getEncryptedString()); 

      final String strToDecrypt = AesOne.getEncryptedString(); 
      AesOne.decrypt(strToDecrypt.trim()); 

      System.out.println("String To Decrypt : " + strToDecrypt); 
      System.out.println("Decrypted : " + AesOne.getDecryptedString()); 

} 

} 

(我想缩进可能会很怪这里)

+0

没有你修改它的代码工作吗?如果是这样,也许尝试回溯你的错误? –

+0

有没有错误? “加密终止而不等待响应”是什么意思?如果你把'System.out.flush()'放在main的最后? – 2017-04-17 20:12:00

+0

你必须使用“String toEncrypt = encrypt.next();”而不是“String toEncrypt = encrypt.toString();”。然后扫描仪将等待,直到按下Enter。 – DiabolicWords

回答

0

我尝试了代码,它工作正常。 ......如果你是从单元测试中踢掉它(例如,在JUnit中),那么单元测试线程可能会在响应返回之前结束。对于长时间运行的任务,我有这个问题。 如果是在一个单元测试,你可以尝试添加课余呼叫这样一个给力的单元测试,以等待...

@AfterClass 
    public static void afterClass() { 

     // the threads to finish before we terminate JUNIT 
     Thread sleepyThread = new Thread() { 
      public void run() { 
       // wait for a bit 
       try { 
        this.sleep(5000); 
       } catch (InterruptedException e) { 
        // TODO Auto-generated catch block 
        e.printStackTrace(); 
       } 
      } 
     }; 

     // start the sleepy thread 
     try { 
      sleepyThread.start(); 
      sleepyThread.join(); 
     } catch (InterruptedException e) { 
      // TODO Auto-generated catch block 
      e.printStackTrace(); 
     } 

    System.err.println("Done running tests"); 

    } 
1

你必须使用

String toEncrypt = encrypt.next(); 

代替

String toEncrypt = encrypt.toString(); 

然后扫描仪将等待,直到按下Enter键。

.toString()立即停止等待过程并提供当前Scanner内容的字符串表示。

+0

啊......我假设他传递了对象的toString作为测试加密的方式。 :) – Matt

相关问题