我有一个代码,它通过采用扫描器方法加密到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());
}
}
(我想缩进可能会很怪这里)
没有你修改它的代码工作吗?如果是这样,也许尝试回溯你的错误? –
有没有错误? “加密终止而不等待响应”是什么意思?如果你把'System.out.flush()'放在main的最后? – 2017-04-17 20:12:00
你必须使用“String toEncrypt = encrypt.next();”而不是“String toEncrypt = encrypt.toString();”。然后扫描仪将等待,直到按下Enter。 – DiabolicWords