Java 使用AES-128加密和解密字符串
我是通过加密128位密钥得到的。我可以做些什么来扭转这个过程。我几乎坐在这里将近一个小时来思考这个问题,但我不能。顺便说一句,我是新来的 样本输入为:Java 使用AES-128加密和解密字符串,java,encryption,aes,Java,Encryption,Aes,我是通过加密128位密钥得到的。我可以做些什么来扭转这个过程。我几乎坐在这里将近一个小时来思考这个问题,但我不能。顺便说一句,我是新来的 样本输入为:J§????K♥?{↕? 输出应该是:hello 在本节目中: 示例输入为:hello 输出为:J§????K♥?{↕?... public class en { public static void main(String[] args){ ... try{ System.out.print("Enter
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();
}
}
}
密文由字节组成,看起来应该是随机的。您将获得无法打印的字符,您也无法键入这些字符。加密后,您必须使用Base64之类的编码来打印密文,并在解密前键入
在加密过程中(Java 8):
在解密过程中(Java 8):
除此之外,您确实应该完全指定您的方案,因为它在另一个Java实现上的行为可能不同
Cipher cipher = Cipher.getInstance("AES/ECB/PKCS5Padding");
对于一些仍然无法解密加密内容的用户,您必须使用Base 64对其进行解码。您可以使用apache commons编解码器依赖项。以下是从中复制的工作代码
就我所能理解的代码而言,输入和输出应该是我试图获取加密和解密文本的程序输出中的相同内容。我的输入是字符串。我想要实现的是解密符号,而不是将字符串作为输出。接受符号作为输入,以获取其等效文本。如果加密的字符串g包含的字符不包括在您用来显示它们的字体中,您将看到一个问号?
符号。我不明白。您的代码可以工作。唯一的问题是您应该完全指定您的方案:Cipher Cipher=Cipher.getInstance(“AES/ECB/PKCS5Padding”)
取决于您的默认提供者。如果您真的想在屏幕上输出加密数据以进行输入,您需要一些额外的编码,例如base64,它允许您恢复精确的字节。简单地将字节数组作为字符串输出将不起作用,因为您丢失了一些信息。如果我想输入一组要删除的符号,该怎么办地下室?你说的“一套符号”是什么意思?你想用这个集合做什么?完全不清楚。请详细描述,你想实现什么。我想将它解密为它的等价词。例如?使用AES,如果你使用正确的密钥,你就可以得到你加密的内容。我不明白你在找什么。如何得到你在AES中加密的内容?
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);
Cipher cipher = Cipher.getInstance("AES/ECB/PKCS5Padding");
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);
}