last block incomplete with CipherInputStream/CipherOutputStream, even with padding AES/CBC/PKCS5Padding

后端 未结 6 2133
自闭症患者
自闭症患者 2020-12-28 18:06

Actually, I searched lot from internet and in stackoverflow too for this,

Initially I don\'t used padding in my encryption and decryption,

But Finally I got

6条回答
  •  清酒与你
    2020-12-28 19:05

    For those who are struggling with aes encryption / decryption with image file, here is my example, and it works like a charm.

    public static String decrypt(String textToDecrypt)  {
        byte[] dataDecrypted = null;
    
        try {
            Cipher cipher = getCipher();
            SecretKey key = getKey();
            IvParameterSpec iv = getIV();
            if(cipher == null) {
                return null;
            }
            cipher.init(Cipher.DECRYPT_MODE, key, iv);
    
            byte[] dataToDecrypt = Base64.decode(textToDecrypt, Base64.NO_WRAP);
            ByteArrayInputStream bais = new ByteArrayInputStream(dataToDecrypt);
            ByteArrayOutputStream baos = new ByteArrayOutputStream();
            // Wrap the output stream
            CipherOutputStream cos = new CipherOutputStream(baos, cipher);
    
            // Read bytes
            int count = 0;
            byte[] buffer = new byte[DEFAULT_BYTE_READ_WRITE_BLOCK_BUFFER_SIZE];
            while ((count = bais.read(buffer)) != -1) {
                cos.write(buffer, 0, count);
            }
            cos.close();    // manually do close for the last bit
    
            dataDecrypted = baos.toByteArray();
    
            // Close streams.
            baos.close();
            bais.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
    
        return (dataDecrypted == null ? "" : Base64.encodeToString(dataDecrypted, Base64.NO_WRAP));
    }
    

    public static String encrypt(String textToEncrypt) {
        byte[] dataEncrypted = null;
    
        try {
            Cipher cipher = getCipher();
            SecretKey key = getKey();
            IvParameterSpec iv = getIV();
            if (cipher == null) {
                return null;
            }
            cipher.init(Cipher.ENCRYPT_MODE, key, iv);
    
            byte[] dataToEncrypt = Base64.decode(textToEncrypt, Base64.NO_WRAP);
            ByteArrayInputStream bais = new ByteArrayInputStream(dataToEncrypt);
            ByteArrayOutputStream baos = new ByteArrayOutputStream();
            // Wrap the output stream
            CipherOutputStream cos = new CipherOutputStream(baos, cipher);
    
            // Read bytes
            int count = 0;
            byte[] buffer = new byte[DEFAULT_BYTE_READ_WRITE_BLOCK_BUFFER_SIZE];
            while ((count = bais.read(buffer)) != -1) {
                cos.write(buffer, 0, count);
            }
            cos.close();    // manually do close for the last bit
    
            dataEncrypted = baos.toByteArray();
    
            // Close streams.
            baos.close();
            bais.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
    
        return (dataEncrypted == null ? "" : Base64.encodeToString(dataEncrypted, Base64.NO_WRAP));
    }
    

提交回复
热议问题