如果我使用相同的输入字符串、相同的键和相同的iv,我希望得到相同的加密结果。然而,每次我运行我的加密程序,它都会产生不同的结果。还有其他的随机数被加密代码使用吗?
我的加密码
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);
}
}
}谢谢,尼克
发布于 2015-12-16 16:40:53
是的,在填充算法ISO 10126中存在随机字节。消息的最后一个块被随机字节填充到(块大小-1),最后添加的字节保存随机序列的长度。
这是一种避免某些攻击的重要机制,有关更多细节,请参见(cryptography)。
注: ISO 10126已于2007年撤回,应不再使用。考虑其他填充算法。
https://stackoverflow.com/questions/34317194
复制相似问题