Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/182.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android 没有填充的加密不起作用_Android_Cryptography_Aes_Cbc Mode - Fatal编程技术网

Android 没有填充的加密不起作用

Android 没有填充的加密不起作用,android,cryptography,aes,cbc-mode,Android,Cryptography,Aes,Cbc Mode,Im制作应用程序,用于在AES/CBC模式下加密和解密文本。在AES/CBC/PKCS5Padding(和PKCS7Padding)中,一切正常,但如果我将算法设置为AES/CBC/NoPadding,则会得到“error”字符串作为输出。有什么问题 包含加密和解密函数的类: public class CriptographyUtils { private static final String INIT_VECTOR = "fedcba9876543210"; private

Im制作应用程序,用于在AES/CBC模式下加密和解密文本。在AES/CBC/PKCS5Padding(和PKCS7Padding)中,一切正常,但如果我将算法设置为AES/CBC/NoPadding,则会得到“error”字符串作为输出。有什么问题

包含加密和解密函数的类:

public class CriptographyUtils
{
    private static final String INIT_VECTOR = "fedcba9876543210";
    private static final String ALGORITHM = "AES/CBC/NoPadding";

    public static String aesEncrypt(String key, String text)  // encrypts text (get bytes -> encrypt -> encode -> to String)
    {
        String result;

        try
        {
            IvParameterSpec iv = new IvParameterSpec(INIT_VECTOR.getBytes());
            SecretKeySpec myKey = new SecretKeySpec(fixKey(key).getBytes("UTF-8"), "AES");

            Cipher cipher = Cipher.getInstance(ALGORITHM);
            cipher.init(Cipher.ENCRYPT_MODE, myKey, iv);

            byte[] encryptedBytes = cipher.doFinal(text.getBytes("UTF-8"));

            result = Base64.encodeToString(encryptedBytes, Base64.DEFAULT);
        }
        catch (Exception ex)
        {
            ex.printStackTrace();
            result = "error";
        }

        return result;
    }

    public static String aesDecrypt(String key, String text)  // decrypts text (get bytes -> decode -> decrypt -> to String)
    {
        String result;

        try
        {
            IvParameterSpec iv = new IvParameterSpec(INIT_VECTOR.getBytes("UTF-8"));
            SecretKeySpec myKey = new SecretKeySpec(fixKey(key).getBytes("UTF-8"), "AES"); // create new KEY in utf-8

            Cipher cipher = Cipher.getInstance(ALGORITHM); // create new cipher
            cipher.init(Cipher.DECRYPT_MODE, myKey, iv); // set cipher into decrypt mode using my KEY

            byte[] decryptedBytes = cipher.doFinal(Base64.decode(text, Base64.DEFAULT)); // get bytes -> decode -> decrypt

            result = new String(decryptedBytes);    // convert decrypted text to String
        }
        catch (Exception ex)
        {
            ex.printStackTrace();
            result = "error";
        }

        return result;
    }

    private static String fixKey(String key)
    {
        if (key.length() < 16)  // less than 128 bits
        {
            int numPad = 16 - key.length();

            for (int i = 0; i < numPad; i++)
                key += "0"; //0 pad to len 16 bytes
        }
        else if (key.length() > 16)
            key = key.substring(0, 16); //truncate to 16 bytes

        return key;
    }
}
解密:

关键是:


AES是一种块加密算法,因此其输入必须是块大小的倍数,AES为16字节。因此,如果不能保证数据是块大小的倍数,则需要添加填充

使用填充:PKCS#7是AES常用的填充,PKCS#5基本相同

PKCS#5标识符仅用于AES,因为编码人员懒得添加对PKCS#7标识符的支持。见:

PKCS#5填充与PKCS#7填充相同,只是它仅为使用64位(8字节)块大小的块密码定义。实际上,这两者可以互换使用

CriptographyUtils.aesEncrypt(key, textToEncrypt)
CriptographyUtils.aesDecrypt(key, textToDecrypt));
private static final String key = "1234123412341234";