加密/解密使用AES / ECB / NoPadding [英] encryption/decryption using AES/ECB/NoPadding

查看:3506
本文介绍了加密/解密使用AES / ECB / NoPadding的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

以下是我的加密/解密方法:

Following are my encrypt/decrypt methods:

private String decrypt_data(String encData) 
                throws NoSuchAlgorithmException, NoSuchPaddingException, InvalidKeyException, IllegalBlockSizeException, BadPaddingException
    {
        String key = "bad8deadcafef00d";
        SecretKeySpec skeySpec = new SecretKeySpec(key.getBytes(), "AES");
        Cipher cipher = Cipher.getInstance("AES/ECB/NoPadding");

        cipher.init(Cipher.DECRYPT_MODE, skeySpec);

        System.out.println("Base64 decoded: "+Base64.decode(encData.getBytes()).length);
        byte[] original = cipher.doFinal(Base64.decode(encData.getBytes()));
        return new String(original).trim();     
    }

    private String encrypt_data(String data) 
                throws NoSuchAlgorithmException, NoSuchPaddingException, InvalidKeyException, IllegalBlockSizeException, BadPaddingException
    {
        String key = "bad8deadcafef00d";
        SecretKeySpec skeySpec = new SecretKeySpec(key.getBytes(), "AES");
        Cipher cipher = Cipher.getInstance("AES/ECB/NoPadding");

        cipher.init(Cipher.ENCRYPT_MODE, skeySpec);

        System.out.println("Base64 encoded: "+ Base64.encode(data.getBytes()).length);

        byte[] original = cipher.doFinal(Base64.encode(data.getBytes()));
        return new String(original);
    }

现在当我试图加密时,我得到这个异常: / p>

So now when I am trying to encrypt, I am getting this exception:

javax.crypto.IllegalBlockSizeException: Input length not multiple of 16 bytes
    at com.sun.crypto.provider.CipherCore.finalNoPadding(CipherCore.java:854)
    at com.sun.crypto.provider.CipherCore.doFinal(CipherCore.java:828)
    at com.sun.crypto.provider.CipherCore.doFinal(CipherCore.java:676)
    at com.sun.crypto.provider.AESCipher.engineDoFinal(AESCipher.java:313)
    at javax.crypto.Cipher.doFinal(Cipher.java:2087)



<在加密和解密。它仅用于加密而不用于解密。我认为需要填充,因为数据大小不是16bytes的倍数。 'data'字节长度打印为 152 因此尝试追加 \0 字符到数据然后加密,它也没有工作。

I have tried with cipher instance AES/ECB/PKCS5Padding in both encrypt and decrpt. It works only for encryption and not for decryption. I suppose padding is needed as data size is not multiple of 16bytes. 'data' byte length was printed as 152 so tried appending eight \0 characters to data and then encrypt, it didn't work either.

推荐答案

我在您的代码中看到了另一个错误:

I saw another mistake in your code :

你的encyption方法得到工作,你的解密方法是不会工作。
确实,您正在使用Base64加密AES后加密。然后你使用Base64,然后AES解密整个事情。

Even if your encyption method get working, you're decryption method is not going to work. Indeed you are encrypting using Base64 followed bye AES. And you decrypt the whole thing using Base64 followed by AES.

我几乎肯定这两种类型的加密不是可交换的。
(您必须解密AES,然后Base64)。

I am almost sure that this two type of encryption are not exchangeables. (you must decrypt AES then Base64).

考虑这个答案: javax.crypto.IllegalBlockSizeException:使用填充密码解密时,输入长度必须为16的倍数
,使用填充(NoPadding arg),你必须使用长度为16的输入字符串。

Considering this answer : javax.crypto.IllegalBlockSizeException : Input length must be multiple of 16 when decrypting with padded cipher , without the use of padding (NoPadding arg), you have to use an input String with a length multiple of 16.

如果我们用我的注释修改代码,如果我们让密码管理填充,我们得到以下代码,它工作良好:

If we correct your code with my remark and if we let the cipher manage the padding, we get the following code, which works well :

private static String decrypt_data(String encData)
        throws NoSuchAlgorithmException, NoSuchPaddingException,
        InvalidKeyException, IllegalBlockSizeException, BadPaddingException {
    String key = "bad8deadcafef00d";
    SecretKeySpec skeySpec = new SecretKeySpec(key.getBytes(), "AES");
    Cipher cipher = Cipher.getInstance("AES");

    cipher.init(Cipher.DECRYPT_MODE, skeySpec);

    System.out.println("Base64 decoded: "
            + Base64.decode(encData.getBytes()).length);
    byte[] original = cipher
            .doFinal(Base64.decode(encData.getBytes()));
    return new String(original).trim();
}

private static String encrypt_data(String data)
        throws Exception {
    String key = "bad8deadcafef00d";
    SecretKeySpec skeySpec = new SecretKeySpec(key.getBytes(), "AES");
    Cipher cipher = Cipher.getInstance("AES");

    cipher.init(Cipher.ENCRYPT_MODE, skeySpec);

    System.out.println("Base64 encoded: "
            + Base64.encode(data.getBytes()).length);

    byte[] original = Base64.encode(cipher.doFinal(data.getBytes()));
    return new String(original);
}

这篇关于加密/解密使用AES / ECB / NoPadding的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆