如果仅使用长字符串

时间:2017-04-12 13:53:55

标签: java android encryption cryptography aes

我正在尝试使用Cipher使用AES(“AES / CBC / PKCS7Padding”)加密和解密纯文本。
由KeyGenerator制作secretKey并用它加密/解密。 这起初效果很好。

但是,如果纯文本是一个大字符串(例如183244个字符),则部分字符串被解密而不是全部。

“它的一部分”是指
如果纯文本是“abcdefghijklmn”,则解密的文本就像“abcdefgh” (这是一个例子。实际上只有太长的字符才会发生这种情况)

代码

        // get key from Android kyestore
        SecretKey secretKey = (SecretKey)mKeyStore.getKey(KEY_ALIAS, null);

        Cipher cipher = Cipher.getInstance("AES/CBC/PKCS7Padding");
        cipher.init(Cipher.ENCRYPT_MODE, secretKey);

        IvParameterSpec ivParams = cipher.getParameters().getParameterSpec(IvParameterSpec.class);

        //
        // Encrypt
        //
        ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
        CipherOutputStream cipherOutputStream = new CipherOutputStream(outputStream, cipher);
        cipherOutputStream.write(plainString.getBytes("UTF-8"));
        cipherOutputStream.close();

        String encryptedString = Base64.encodeToString(outputStream.toByteArray(), Base64.DEFAULT);

        //
        // Decrypt
        //
        Cipher decryptCipher = Cipher.getInstance("AES/CBC/PKCS7Padding");
        decryptCipher.init(Cipher.DECRYPT_MODE, secretKey, ivParams);

        ByteArrayInputStream byteArrayInputStream = new ByteArrayInputStream(Base64.decode(encryptedString, Base64.DEFAULT));
        CipherInputStream cipherInputStream = new CipherInputStream(byteArrayInputStream, decryptCipher);
        outputStream = new ByteArrayOutputStream();

        int b;
        while ((b = cipherInputStream.read()) != -1) {
            outputStream.write(b);
        }
        outputStream.close();

        String decryptedString =  outputStream.toString("UTF-8");

        //
        // results. decrypted string is part of source string and short.
        //
        int sourceStringLength = plainString.length(); // is 183244;
        int decryptedStringLength = decryptedString.length()); // is UNEXPECTED 68553;

请你知道解决这个问题的重点吗?

被修改

我检查了加密/解密的字节数。

加密输出字节:“outputStream.toByteArray()。length”是68560
解密输入字节:“Base64.decode(encryptedString,Base64.DEFAULT).length”也是68560

我认为这意味着解密过程不会丢失任何部分,加密过程可能会截断部分输入纯文本。 这是因为AES算法的限制吗?

解决

正如@zaph评论的那样,并非所有字节都写入了steam。

更换

  cipherOutputStream.write(plainString.getBytes("UTF-8"));
  cipherOutputStream.close();

        // NOTE: workaround. too bit bytes doesn't writted correctly.
        byte[] bytes = plainString.getBytes("UTF-8");

        int oneBulkSize = 1024;// temp value for proof of concept. might be bigger one.
        int numOfBulk = (bytes.length / oneBulkSize);

        for (int i = 0; i < numOfBulk; i++) {
            cipherOutputStream.write(bytes, oneBulkSize * i, oneBulkSize);
        }

        if ((bytes.length % oneBulkSize) != 0) {
            cipherOutputStream.write(bytes, oneBulkSize * numOfBulk, bytes.length % oneBulkSize);
        }
        cipherOutputStream.close();

解决了这个问题。

1 个答案:

答案 0 :(得分:1)

AES没有数据长度限制。加密数据的长度应与输入相同,加上最多16个字节进行填充。

在加密循环中添加一些调试代码,并验证是否正在读取整个输入。也许加密流需要一个循环,就像解密流一样。

另一种可能性是183244是UTF-16编码的字节大小,但你读的是UTF-8,大多数UTF-16字符可以是一个UTF-8字节。

  

以下是OP @nsht的实际解决方案代码,此处包含完整性(来自@nsht删除的答案):

更换

  cipherOutputStream.write(plainString.getBytes("UTF-8"));
  cipherOutputStream.close();

        // NOTE: workaround. too bit bytes doesn't writted correctly.
        byte[] bytes = plainString.getBytes("UTF-8");

        int oneBulkSize = 1024;// temp value for proof of concept. might be bigger one.
        int numOfBulk = (bytes.length / oneBulkSize);

        for (int i = 0; i < numOfBulk; i++) {
            cipherOutputStream.write(bytes, oneBulkSize * i, oneBulkSize);
        }

        if ((bytes.length % oneBulkSize) != 0) {
            cipherOutputStream.write(bytes, oneBulkSize * numOfBulk, bytes.length % oneBulkSize);
        }
        cipherOutputStream.close();

解决了这个问题。