简体   繁体   English

Java中的AES密钥大小

[英]AES key size in Java

Testing RSA to encrypt an AES key, I realized that RSA has only 1 block with a limited size (settable by the programmer) do store the encrypted key. 测试RSA加密AES密钥,我意识到 RSA只有一个有限大小的块(程序员可设置)存储加密密钥。 The question is, when I use: 问题是,当我使用时:

KeyGenerator.getInstance("AES").generateKey()

the AES keys will have a constant size in every computer and jvm implementation? 每个计算机和jvm实现中AES密钥的大小是否恒定?

There is an init method in the KeyGenerator that allows you to specify the number of bits. KeyGenerator中有一个init方法,允许您指定位数。

KeyGenerator keyGenerator = KeyGenerator.getInstance("AES");
keyGenerator.init(128);
SecretKey key = keyGenerator.generateKey();

Will that do what you need? 这会做你需要的吗?

The default appears to be 128 bits, but I would not assume that all JVM's use the same default, or that it will always be the default. 默认值似乎是128位,但我不认为所有JVM都使用相同的默认值,或者它始终是默认值。

Suns Java Cryptography Extension documentation states that multiple key sizes are supported for AES keys and doesn't provide any information on the default size. Suns Java Cryptography Extension文档指出AES密钥支持多种密钥大小,并且不提供有关默认大小的任何信息。

The maximum size of keys can also vary depending on the jurisdictional files used by different versions of Suns JVM. 密钥的最大大小也可以根据不同版本的Suns JVM使用的管辖区文件而有所不同。

KeyGenerator has several init() methods; KeyGenerator有几个init()方法; you should call one of them before generating a key. 你应该在生成密钥之前调用其中一个。 The Javadoc for KeyGenerator specifies that in case you do not call one of the init() method, then "each provider must supply (and document) a default initialization." KeyGenerator的Javadoc指定,如果您不调用其中一个init()方法,则“每个提供程序必须提供(并记录)默认初始化”。

So this is provider-specific. 所以这是特定于提供者的。 Since you initialize the key generator with the "AES" algorithm name, one may assume that you will get a key with a size suitable for AES, ie 128, 192 or 256 bits (16, 24 and 32 bytes, respectively). 由于使用“AES”算法名称初始化密钥生成器,可以假设您将获得具有适合AES的大小的密钥,即128,192或256位(分别为16,24和32字节)。 But which one you get is up to the actual provider, which may depend upon the JVM and possibly its configuration. 但是你得到的是实际的提供者,这可能取决于JVM及其配置。

https://docs.oracle.com/javase/7/docs/api/javax/crypto/Cipher.html https://docs.oracle.com/javase/7/docs/api/javax/crypto/Cipher.html

Every implementation of the Java platform is required to support the following standard Cipher transformations with the keysizes in parentheses: Java平台的每个实现都需要支持以下标准Cipher转换,并在括号中使用密钥:

AES/CBC/NoPadding (128)
AES/CBC/PKCS5Padding (128)
AES/ECB/NoPadding (128)
AES/ECB/PKCS5Padding (128)
DES/CBC/NoPadding (56)
DES/CBC/PKCS5Padding (56)
DES/ECB/NoPadding (56)
DES/ECB/PKCS5Padding (56)
DESede/CBC/NoPadding (168)
DESede/CBC/PKCS5Padding (168)
DESede/ECB/NoPadding (168)
DESede/ECB/PKCS5Padding (168)
RSA/ECB/PKCS1Padding (1024, 2048)
RSA/ECB/OAEPWithSHA-1AndMGF1Padding (1024, 2048)
RSA/ECB/OAEPWithSHA-256AndMGF1Padding (1024, 2048)

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

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