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

查看:299
本文介绍了使用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)

AES / ECB / PKCS5Padding 在加密和decrpt。它只适用于加密而不是解密。我想填充是需要的,因为数据大小不是16字节的倍数。 '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天全站免登陆