user4238474
user4238474

Reputation:

java Cipher.doFinal() returning different results when using same input, key and iv

I was expecting to get the same encryted result if I use the same input string, same key and same iv. However, each time I run my encrypt program it generates different results. Is there some other random number that's being used by the encryption code?

my encrypt code:

import javax.crypto.Cipher;
import javax.crypto.spec.SecretKeySpec;
import javax.crypto.spec.IvParameterSpec;
import java.nio.charset.StandardCharsets;
import javax.xml.bind.DatatypeConverter;
import java.security.NoSuchAlgorithmException;
import java.security.InvalidKeyException;
import javax.crypto.IllegalBlockSizeException;
import javax.crypto.NoSuchPaddingException;
import java.security.InvalidAlgorithmParameterException;
import javax.crypto.BadPaddingException;

public class Encrypt
{
    private static byte[] _key = new byte[] {
            (byte) 0x02, (byte) 0xb2, (byte) 0xc4, (byte) 0x9e,
            (byte) 0xf9, (byte) 0x44, (byte) 0x99, (byte) 0xc9,
            (byte) 0x80, (byte) 0x65, (byte) 0xcd, (byte) 0x8f,
            (byte) 0x69, (byte) 0x2b, (byte) 0x74, (byte) 0x34};

    private static byte[] _iv = new byte[] {
            (byte) 0x69, (byte) 0x2b, (byte) 0x74, (byte) 0x34,
            (byte) 0x02, (byte) 0xb2, (byte) 0xc4, (byte) 0x9e,
            (byte) 0xf9, (byte) 0x44, (byte) 0x99, (byte) 0xc9,
            (byte) 0x80, (byte) 0x65, (byte) 0xcd, (byte) 0x8f};

    public static void main(String[] args)
            throws NoSuchAlgorithmException, InvalidKeyException, IllegalBlockSizeException,
            NoSuchPaddingException, InvalidAlgorithmParameterException,
            BadPaddingException
    {
        if (args.length != 1)
            System.out.println("Error, expecting a single string to encrypt as a command line argument.");
        else
            {
            Cipher cipher = Cipher.getInstance("AES/CBC/ISO10126Padding");
            cipher.init(Cipher.ENCRYPT_MODE, new SecretKeySpec(_key, "AES"),
                    new IvParameterSpec(_iv));
            byte[] encrypted = cipher.doFinal(args[0].getBytes(StandardCharsets.US_ASCII));
            String result = DatatypeConverter.printBase64Binary(encrypted);
            System.out.println("result: " + result);
            }
    }
}

Thanks, Nick

Upvotes: 3

Views: 2516

Answers (1)

vojta
vojta

Reputation: 5651

Yes, there are random bytes in your padding algorithm ISO 10126. Last block of your message is padded with random bytes to the (block size -1) and the very last added byte holds the length of the random sequence.

It is an important mechanism to avoid some attacks, see https://en.m.wikipedia.org/wiki/Padding_(cryptography) for more details.

Note: ISO 10126 was withdrawn 2007 and should be no longer used. Consider any other padding algorithm.

Upvotes: 4

Related Questions