【问题标题】:Encrypt and decrypt a string with AES-128使用 AES-128 加密和解密字符串
【发布时间】: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


【解决方案1】:

密文由字节组成,应该看起来是随机的。您将获得无法输入的不可打印字符。您必须使用 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)));

解密期间(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);

Encoding reference


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

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

【讨论】:

  • 如果我想输入一组符号来解密怎么办?
  • “符号集”是什么意思?你想用套装做什么?这完全不清楚。请详细描述您想要达到的目标。
  • 我想把它解密成它的等价词。
  • 例如?使用 AES,如果您使用正确的密钥,您可以取回加密的内容。我不明白你在那之后是什么。
  • 如何取回您在 AES 中加密的内容?
【解决方案2】:

对于某些仍然无法解密已加密内容的人,您必须使用 Base 64 对其进行解码。您可以使用 apache commons codec 依赖项。这是从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);
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-02-18
    • 1970-01-01
    • 1970-01-01
    • 2020-12-03
    • 1970-01-01
    相关资源
    最近更新 更多