Java 使用 AES-128 加密和解密字符串
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/28025742/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Encrypt and decrypt a string with AES-128
提问by Yodism
I got it in encrypting 128-bit key. What can I do to reverse the process. I'm almost sitting here almost an hour to figure this but i cant. I'm new to this btw.
我在加密 128 位密钥时得到了它。我能做些什么来扭转这个过程。我几乎要坐在这里将近一个小时才能弄清楚这一点,但我不能。顺便说一句,我是新手。
The sample input is:J§????????K??{??
样本输入是:J§????????K??{??
The output should be: hello
输出应该是: hello
In this program:
在这个程序中:
The sample input is:hello
样本输入是:hello
The output is: J§????????K??{??...
输出是: 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();
}
}
}
采纳答案by Artjom B.
Ciphertext is composed of bytes and is supposed to look random. You will get unprintable characters which you also cannot type in. You have to use an encoding like Base64 to print your ciphertext after encryption and type in before decryption.
密文由字节组成,应该看起来是随机的。您将获得无法输入的无法打印的字符。您必须使用 Base64 等编码在加密后打印密文并在解密前输入。
During the encryption (Java 8):
在加密期间(Java 8):
cipher.init(Cipher.ENCRYPT_MODE, aesKey);
byte[] encrypted = cipher.doFinal(text.getBytes());
System.err.println("Encrypted: " + new String(Base64.getEncoder().encodeToString(encrypted)));
During the decryption (Java 8):
在解密过程中(Java 8):
System.out.print("Enter ciphertext: ");
byte[] encrypted = Base64.getDecoder().decode(dataIn.readLine());
...
cipher.init(Cipher.DECRYPT_MODE, aesKey);
String decrypted = new String(cipher.doFinal(encrypted));
System.err.println("Decrypted: " + decrypted);
Apart from that you really should fully specify your scheme, because it might behave differently on another Java implementation.
除此之外,您确实应该完全指定您的方案,因为它在另一个 Java 实现上的行为可能有所不同。
Cipher cipher = Cipher.getInstance("AES/ECB/PKCS5Padding");
回答by Jerry C.
For some that is still having trouble to decrypt what you have encrypted, you have to decode it using Base 64. You can use apache commons codec dependency. Here is a working code copied from http://javapointers.com/tutorial/how-to-encrypt-and-decrypt-using-aes-in-java/
对于一些仍然无法解密您加密的内容的人,您必须使用 Base 64 对其进行解码。您可以使用 apache commons 编解码器依赖项。这是从http://javapointers.com/tutorial/how-to-encrypt-and-decrypt-using-aes-in-java/复制的工作代码
public class EncryptDecrypt {
private static final String SECRET_KEY_1 = "ssdkF$HUy2A#D%kd";
private static final String SECRET_KEY_2 = "weJiSEvR5yAC5ftB";
private IvParameterSpec ivParameterSpec;
private SecretKeySpec secretKeySpec;
private Cipher cipher;
public EncryptDecrypt() throws UnsupportedEncodingException, NoSuchPaddingException, NoSuchAlgorithmException {
ivParameterSpec = new IvParameterSpec(SECRET_KEY_1.getBytes("UTF-8"));
secretKeySpec = new SecretKeySpec(SECRET_KEY_2.getBytes("UTF-8"), "AES");
cipher = Cipher.getInstance("AES/CBC/PKCS5PADDING");
}
public String encrypt(String toBeEncrypt) throws NoSuchPaddingException, NoSuchAlgorithmException,
InvalidAlgorithmParameterException, InvalidKeyException, BadPaddingException, IllegalBlockSizeException {
cipher.init(Cipher.ENCRYPT_MODE, secretKeySpec, ivParameterSpec);
byte[] encrypted = cipher.doFinal(toBeEncrypt.getBytes());
return Base64.encodeBase64String(encrypted);
}
public String decrypt(String encrypted) throws InvalidAlgorithmParameterException, InvalidKeyException,
BadPaddingException, IllegalBlockSizeException {
cipher.init(Cipher.DECRYPT_MODE, secretKeySpec, ivParameterSpec);
byte[] decryptedBytes = cipher.doFinal(Base64.decodeBase64(encrypted));
return new String(decryptedBytes);
}