java Cipher.doFinal()在使用相同的输入,键和iv时返回不同的结果

时间:2023-01-21 18:26:57

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?

如果我使用相同的输入字符串,相同的密钥和相同的iv,我期望获得相同的加密结果。但是,每次运行我的加密程序时,它都会产生不同的结果。加密代码是否使用了其他随机数?

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

谢谢,尼克

1 个解决方案

#1


3  

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.

是的,填充算法ISO 10126中有随机字节。消息的最后一个块用随机字节填充到(块大小-1),最后添加的字节保存随机序列的长度。

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

这是避免某些攻击的重要机制,有关详细信息,请参阅https://en.m.wikipedia.org/wiki/Padding_(cryptography)。

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

注意:ISO 10126已于2007年撤销,不应再使用。考虑任何其他填充算法。

#1


3  

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.

是的,填充算法ISO 10126中有随机字节。消息的最后一个块用随机字节填充到(块大小-1),最后添加的字节保存随机序列的长度。

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

这是避免某些攻击的重要机制,有关详细信息,请参阅https://en.m.wikipedia.org/wiki/Padding_(cryptography)。

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

注意:ISO 10126已于2007年撤销,不应再使用。考虑任何其他填充算法。