【发布时间】:2015-01-19 13:28:17
【问题描述】:
我在加密 128 位密钥时得到了它。我能做些什么来扭转这个过程。我几乎要坐在这里将近一个小时才能弄清楚这一点,但我做不到。顺便说一句,我是新手。
样本输入为:J§???????ÿK♥?{↕?
输出应该是:hello
在这个程序中:
样本输入为:hello
输出为:J§???????ÿK♥?{↕?...
public class en {
public static void main(String[] args){
...
try{
System.out.print("Enter text: ");
String text = dataIn.readLine();
String key = "dAtAbAsE98765432"; // 128 bit key
// Create key and cipher
Key aesKey = new SecretKeySpec(key.getBytes(), "AES");
Cipher cipher = Cipher.getInstance("AES");
// encrypt the text
cipher.init(Cipher.ENCRYPT_MODE, aesKey);
byte[] encrypted = cipher.doFinal(text.getBytes());
System.err.println("Encrypted: " + new String(encrypted));
// Decrypt the text
cipher.init(Cipher.DECRYPT_MODE, aesKey);
String decrypted = new String(cipher.doFinal(encrypted));
System.err.println("Decrypted: " + decrypted);
}catch(Exception e){
e.printStackTrace();
}
}
}
【问题讨论】:
-
据我理解的代码,输入和输出应该是一样的
-
在我的程序输出中,我尝试获取加密和解密的文本。我的输入是一个字符串。我想要成为可能的是解密符号而不是将字符串作为输出。接受符号作为输入以获取其等效文本。
-
如果加密字符串包含您用来显示它们的字体中未包含的字符,您将看到一个问号
?符号。 -
我不明白。您的代码有效。唯一的事情是您应该完全指定您的方案:
Cipher cipher = Cipher.getInstance("AES/ECB/PKCS5Padding");,具体取决于您的默认提供程序。 -
如果你真的想在屏幕上输出你的加密数据来输入,你需要一些额外的编码,比如base64,它可以让你恢复确切的字节。简单地将字节数组输出为字符串是行不通的,因为您丢失了一些信息。
标签: java encryption aes