使用 AES-128 加密和解密字符串

Encrypt and decrypt a string with AES-128

我在加密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();
    }
  }
}

密文由字节组成,应该看起来是随机的。你会得到不可打印的字符,你也不能输入。你必须使用像Base64这样的编码在加密后打印你的密文并在解密前输入。

加密期间(Java 8):

cipher.init(Cipher.ENCRYPT_MODE, aesKey);
byte[] encrypted = cipher.doFinal(text.getBytes());
System.err.println("Encrypted: " + new String(Base64.getEncoder().encodeToString(encrypted)));

解密过程中(Java8):

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);

Encoding reference


除此之外,您确实应该完全指定您的方案,因为它在另一个 Java 实现上可能表现不同。

Cipher cipher = Cipher.getInstance("AES/ECB/PKCS5Padding");

对于一些仍然无法解密您已加密的内容的人,您必须使用 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);
}