user1408286
user1408286

Reputation: 27

can't remove 0 while decryption

I wrote 2 functions in Java to encrypt and decrypt the message. I chose AES/CBC/PKCS7Padding as parameter. while encryption, my function can't add the right padding(as PKCS7Padding, it shouldn't add 0 to pad my message), so while decryption i can't remove the 0 at the end of the message. with code, my problem will be more clair

    public static  byte[] encryptStringToData(String message, String key){
        // transform the message from string to bytes
        byte[] array_to_encrypt, bkey;
        try {
            array_to_encrypt = message.getBytes("UTF8");
            bkey = key.getBytes("UTF8");
        } catch (UnsupportedEncodingException e1) {
            array_to_encrypt = null;
            bkey = null;
            return null;
        }

        bkey = Arrays.copyOf(bkey, 32);
        BlockCipher engine = new AESEngine();
        engine.init(true, new KeyParameter(bkey, 0, 32));
        PKCS7Padding pad = new PKCS7Padding() ;

        BufferedBlockCipher c = new PaddedBufferedBlockCipher(new CBCBlockCipher(engine),pad); 

        c.init(true, new ParametersWithIV(new KeyParameter(bkey), new byte[16]));

        byte[] encrypted_array = new byte[c.getOutputSize(array_to_encrypt.length)];

        int outputLen = c.processBytes(array_to_encrypt, 0, array_to_encrypt.length, encrypted_array, 0);
        try
        {
            c.doFinal(encrypted_array, outputLen);
        }
        catch (CryptoException ce)
        {
            System.err.println(ce);
            System.exit(1);
        }
        return encrypted_array; 
    }


public static  String decryptDataToString(byte[] message, String key){
        Security.addProvider(new org.bouncycastle.jce.provider.BouncyCastleProvider());
        String decrypted =""; 
        try { 
            byte[] keyBytes = key.getBytes("UTF8"); 

            BlockCipher engine = new AESEngine();
            BufferedBlockCipher c = new PaddedBufferedBlockCipher(new CBCBlockCipher(engine));
            keyBytes = Arrays.copyOf(keyBytes, 32); // use only first 256 bit

            c.init(false, new ParametersWithIV(new KeyParameter(keyBytes), new byte[16]));
            byte[] cipherText = new byte[c.getOutputSize(message.length)];

            int outputLen = c.processBytes(message, 0, message.length, cipherText, 0);
            c.doFinal(cipherText, outputLen);
            decrypted = new String(cipherText,"UTF8");
        }
        catch (CryptoException ce)
        {
            System.err.println(ce);
            System.exit(1);
        } catch (UnsupportedEncodingException e) { 
            e.printStackTrace();
        }

        return decrypted;

    }

result (hex output)

clair message :3132333435 size: 5 encrypted message:561dd9f43ec183fe351776a46276991c size: 16 decrypted message:31323334350000000000000000000000 size: 16

Upvotes: 1

Views: 584

Answers (1)

tibtof
tibtof

Reputation: 7957

You should check How do I get started using BouncyCastle? The code for removing extra bytes is:

if (outputLength == output.length) {
    return output;
} else {
    byte[] truncatedOutput = new byte[outputLength];
    System.arraycopy(
            output, 0,
            truncatedOutput, 0,
            outputLength
        );
    return truncatedOutput;
}

and in your code traslates to:

outputLen += c.doFinal(cipherText, outputLen);
if (outputLen == cipherText.length) {
    decrypted = new String(cipherText,"UTF8");
} else {
    byte[] truncatedOutput = new byte[outputLen];
    System.arraycopy(
            cipherText, 0,
            truncatedOutput, 0,
            outputLen 
        );
    decrypted = new String(truncatedOutput,"UTF8");
}

Upvotes: 1

Related Questions