简体   繁体   English

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

[英]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. 如果使用相同的输入字符串,相同的键和相同的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 谢谢,尼克

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. 注意:ISO 10126已于2007年撤消,不应再使用。 Consider any other padding algorithm. 考虑其他填充算法。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM