【问题标题】:Simple AES encryption and decryption not returning original text简单的AES加密和解密不返回原始文本
【发布时间】:2014-05-27 09:04:59
【问题描述】:

我使用以下简单的加密和解密函数只是为了在使用更复杂的安全功能(如填充和散列)之前查看它是否有效。由于某种原因,返回的明文与原始消息不相似。代码如下:

public static byte[] encrypt(SecretKey secret, byte[] buffer) throws NoSuchAlgorithmException, NoSuchPaddingException, InvalidKeyException, InvalidParameterSpecException, IllegalBlockSizeException, BadPaddingException, UnsupportedEncodingException
{
    /* Encrypt the message. */
    cipher = Cipher.getInstance("AES/CTR/NoPadding");
    cipher.init(Cipher.ENCRYPT_MODE, secret);
    byte[] ciphertext = cipher.doFinal(buffer);

    return ciphertext;
}

public static byte[] decrypt(SecretKey secret, byte[] buffer) throws NoSuchAlgorithmException, NoSuchPaddingException, InvalidKeyException, InvalidParameterSpecException, IllegalBlockSizeException, BadPaddingException, UnsupportedEncodingException
{
    /* Decrypt the message. - use cipher instance created at encrypt */
    cipher.init(Cipher.DECRYPT_MODE, secret);
    byte[] clear = cipher.doFinal(buffer);

    return clear;
}

和调用代码:

    SecretKey secret1 = null;
    byte[] ciphertext = null;
    byte[] message = "Hello, World!".getBytes();
    byte[] clear = null;

    try {
// aSecret is a shared secret generated with ECDH
        secret1 = Crypto.createAESKey(aSecret);
        ciphertext = Crypto.encrypt(secret1, message);
        clear = Crypto.decrypt(secret1, ciphertext);

        String s = new  String(clear);//clear.toString();

        keyAText.setText(new String(message));
        keyBText.setText(s);

        return;
    } catch (InvalidKeySpecException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    } catch (NoSuchAlgorithmException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    } catch (InvalidKeyException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    } catch (NoSuchPaddingException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    } catch (InvalidParameterSpecException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    } catch (IllegalBlockSizeException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    } catch (BadPaddingException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    } catch (UnsupportedEncodingException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }

【问题讨论】:

  • 在字节数组上调用 toString 并没有按照您的想法进行。看看String 构造函数。
  • Henry - 即使字符串没有正确构建,我也希望两个字节数组生成相同的“字符串”。
  • @Simon 不,因为它不是同一个实例。默认的toString 方法将对象的哈希码显示为字符串的一部分。
  • 您确定要使用 AES 计数器模式吗?如果是,则必须使用相同的IV值进行加密和解密。

标签: android security encryption aes


【解决方案1】:

这几乎可以肯定是因为您在初始化期间没有使用IvParameterSpec 提供 IV。在 Java SE 上,您的代码甚至不会运行,因为 CTR 需要 设置此参数。然而,其他提供者可能会以不同的方式实现事物,例如他们可能会提供随机静脉注射。

当然,如果你在加解密时使用不同的随机IV,你的解密结果很可能与你的明文不匹配。

尝试以下修正方法:

public static byte[] encrypt(SecretKey secret, byte[] buffer) throws GeneralSecurityException
{
    /* Encrypt the message. */
    Cipher cipher = Cipher.getInstance("AES/CTR/NoPadding");

    SecureRandom rng = new SecureRandom();
    byte[] ivData = new byte[cipher.getBlockSize()];
    rng.nextBytes(ivData);

    cipher.init(Cipher.ENCRYPT_MODE, secret, new IvParameterSpec(ivData));
    byte[] ciphertext = cipher.doFinal(buffer);

    return Arrays.concatenate(ivData, ciphertext);
}

public static byte[] decrypt(SecretKey secret, byte[] buffer) throws GeneralSecurityException
{
    /* Decrypt the message. - use cipher instance created at encrypt */
    Cipher cipher = Cipher.getInstance("AES/CTR/NoPadding");

    int n = cipher.getBlockSize();
    byte[] ivData = Arrays.copyOf(buffer, n);

    cipher.init(Cipher.DECRYPT_MODE, secret, new IvParameterSpec(ivData));
    byte[] clear = cipher.doFinal(buffer, n, buffer.length - n);

    return clear;
}

【讨论】:

  • 请注意,我没有在方法中添加任何对参数的检查,并且我将避免提供安全建议(例如改用 GCM 模式)。
猜你喜欢
  • 1970-01-01
  • 2012-12-30
  • 2014-06-28
  • 1970-01-01
  • 1970-01-01
  • 2021-04-24
  • 1970-01-01
  • 2012-03-12
相关资源
最近更新 更多