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

Yod*_*ism 3 java encryption aes

我在加密128位密钥时得到了它.我该怎么做才能扭转这个过程.我几乎要坐在这里差不多一个小时才想到这个,但我不能.我刚接触这个btw.

样本输入是: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();
    }
  }
}
Run Code Online (Sandbox Code Playgroud)

Art*_* B. 7

密文由字节组成,应该看起来是随机的.您将获得无法打印的不可打印的字符.您必须使用类似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)));
Run Code Online (Sandbox Code Playgroud)

在解密期间(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);
Run Code Online (Sandbox Code Playgroud)

编码参考


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

Cipher cipher = Cipher.getInstance("AES/ECB/PKCS5Padding");
Run Code Online (Sandbox Code Playgroud)


小智 5

对于仍然无法解密加密内容的某些人,您必须使用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);
}
Run Code Online (Sandbox Code Playgroud)