AES/CBC/PKCS5Padding 与 AES/CBC/PKCS7Padding 与 256 密钥大小性能 java

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/20770072/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-13 04:13:06  来源:igfitidea点击:

AES/CBC/PKCS5Padding vs AES/CBC/PKCS7Padding with 256 key size performance java

javaencryptionaespkcs#7pkcs#5

提问by dbw

I am currently using AES/CBC/PKCS5Paddingfor encrypting files in Java with 256 byteskey size, but while searching I found on stackexchange PKCS#5-PKCS#7 Paddingand it is mentioned,

我目前正在使用256 字节密钥大小的AES/CBC/PKCS5PaddingJava 加密文件,但是在搜索时我在 stackexchange PKCS#5-PKCS#7 Padding 上发现并提到了它,

PKCS#5 padding is a subset of PKCS#7 padding for 8 byte block sizes

PKCS#5 填充是 8 字节块大小的 PKCS#7 填充的子集

So I want to know

所以我想知道

  1. Will the performance of AES/CBC/PKCS7Paddingwill be better then AES/CBC/PKCS5Paddingfor the above configuration?
  2. How can we configure the block size in Java as it is mentioned

    PKCS#7 padding would work for any block size from 1 to 255 bytes.

  1. 对于以上配置,性能AES/CBC/PKCS7Padding会更好AES/CBC/PKCS5Padding吗?
  2. 我们如何像提到的那样在 Java 中配置块大小

    PKCS#7 填充适用于从 1 到 255 字节的任何块大小。

My sample code is,

我的示例代码是,

SecureRandom rnd = new SecureRandom();
IvParameterSpec iv = new IvParameterSpec(rnd.generateSeed(16));

KeyGenerator generator = KeyGenerator.getInstance("AES");
generator.init(256);
SecretKey k = generator.generateKey();

Cipher c = Cipher.getInstance("AES/CBC/PKCS5Padding");
c.init(Cipher.ENCRYPT_MODE, k, iv);

采纳答案by Henry

The block size is a property of the used cipher algorithm. For AES it is always 16 bytes.

块大小是所用密码算法的属性。对于 AES,它总是 16 字节。

So strictly speaking, PKCS5Padding cannot be used with AES since it is defined only for a block size of 8 bytes. I assume, AES/CBC/PKCS5Padding is interpreted as AES/CBC/PKCS7Padding internally.

所以严格来说,PKCS5Padding 不能与 AES 一起使用,因为它仅定义为 8 字节的块大小。我假设,AES/CBC/PKCS5Padding 在内部被解释为 AES/CBC/PKCS7Padding。

The only difference between these padding schemes is that PKCS7Padding has the block size as a parameter, while for PKCS5Padding it is fixed at 8 bytes. When the Block size is 8 bytes they do exactly the same.

这些填充方案之间的唯一区别是 PKCS7Padding 将块大小作为参数,而对于 PKCS5Padding,它固定为 8 个字节。当块大小为 8 字节时,它们的作用完全相同。