Java 默认加密/AES 行为

发布于 2025-01-13 04:28:25 字数 234 浏览 5 评论 0原文

有谁知道默认的 Java 加密行为的用途是什么:

SecretKeySpec localSecretKeySpec = new SecretKeySpec(arrayOfByte, "AES");
Cipher localCipher = Cipher.getInstance("AES");

具体来说,我希望了解这些类如何生成 IV,以及仅指定“AES”时的默认加密模式是什么。谢谢。

Does anyone know what the default Java crypto behavior is for:

SecretKeySpec localSecretKeySpec = new SecretKeySpec(arrayOfByte, "AES");
Cipher localCipher = Cipher.getInstance("AES");

Specifically I am looking to understand how those classes generate the IV, as well as what is the default encryption mode when just specifying "AES". Thanks.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

记忆消瘦 2025-01-20 04:28:25

对于 Oracle JDK 7(已测试),AES 的默认密码是 AES/ECB/PKCS5Padding。 Java安全文档没有提到这一点(http://docs.oracle.com/javase/6/docs/technotes/guides/security/StandardNames.html#algspec),必须进行一些JUnit测试才能找出答案。

For Oracle JDK 7 (tested), the default cipher for AES is AES/ECB/PKCS5Padding. The Java Security documentation doesn't mention about this though (http://docs.oracle.com/javase/6/docs/technotes/guides/security/StandardNames.html#algspec), have to do some JUnit testing to find out.

仙女 2025-01-20 04:28:25

详细信息是特定于提供商的。 JCA 参考指南 指出:

(创建密码对象)如果未指定模式或填充,则使用特定于提供程序的模式和填充方案的默认值。例如,SunJCE 提供程序使用 ECB 作为默认模式,并使用 PKCS5Padding 作为 DES、DES-EDE 和 Blowfish 密码的默认填充方案。这意味着在 SunJCE 提供程序的情况下:Cipher.getInstance("DES") 和 Cipher.getInstance("DES/ECB/PKCS5Padding") 是等效的语句。

我总是使用完整的形式(算法/模式/填充),不仅因为我认为在实现中遗漏这样的“细节”是不好的做法,而且还为了实现独立于所选提供者(通常是一个提供者)的密文。加密存储/传输,则无法确定稍后/另一端将使用相同的提供程序)。

The details are provider specific. The JCA Reference Guide says that:

(Creating a Cipher Object) If no mode or padding is specified, provider-specific default values for the mode and padding scheme are used. For example, the SunJCE provider uses ECB as the default mode, and PKCS5Padding as the default padding scheme for DES, DES-EDE and Blowfish ciphers. This means that in the case of the SunJCE provider: Cipher.getInstance("DES") and Cipher.getInstance("DES/ECB/PKCS5Padding") are equivalent statements.

I would always use the full form (algorithm/mode/padding), not only because I think that leaving out such "details" to the implementation is bad practice, but also for achieving a ciphertext that is independent of the chosen provider (one usually encrypts for storage/transmission, then one cannot be sure that the same provider will be used later/on the other end).

九厘米的零° 2025-01-20 04:28:25

这些详细信息是特定于提供商的,依赖默认模式和填充可能非常危险。如果您对当前与 Java 捆绑的默认提供程序使用的值感兴趣,您将必须寻找相关算法的源代码。例如,它用于 RSA 算法的默认值位于此处。另外,Java™ 加密体系结构 (JCA) 参考指南有相当多的信息可以回答你们中的一些其他问题。

Those details are provider specific, and relying on the default mode and padding can be very dangerous. If you are interested in what the values that the default provider currently bundled with Java uses you'll have to hunt down the source code for the algorithm in question. For instance, the default values it uses for the RSA algorithm are here. Also, the Java™ Cryptography Architecture (JCA) Reference Guide has quite a bit of information that could answer some of you other questions.

浅唱々樱花落 2025-01-20 04:28:25

这取决于提供商。不同的提供者可能有不同的默认参数。这是 Java 8 的链接。

https: //docs.oracle.com/javase/8/docs/technotes/guides/security/SunProviders.html#ciphertrans

javax.crypto.Cipher.getInstance(String conversion) 工厂
方法使用以下形式的转换生成密码
算法/模式/填充。如果模式/填充被省略,SunJCE
SunPKCS11 提供商使用 ECB 作为默认模式和 PKCS5Padding
作为许多对称密码的默认填充。

建议使用完全指定的转换
算法、模式和填充,而不是依赖默认值。

注意:ECB 适用于单个数据块,并且可以
并行化,但通常不应该用于多个块
数据。

因此,您不应只使用 AES,而应指定模式和填充。此外,虽然 getInstance 方法可以有提供者的另一个参数,但不建议这样做,因为

应用程序与可能不可用的特定提供商绑定
关于其他 Java 实现

It depends on the Providers. Different providers might have different default parameters. This is the link for Java 8.

https://docs.oracle.com/javase/8/docs/technotes/guides/security/SunProviders.html#ciphertrans

The javax.crypto.Cipher.getInstance(String transformation) factory
method generates Ciphers using transformations of the form
algorithm/mode/padding. If the mode/padding are omitted, the SunJCE
and SunPKCS11 providers use ECB as the default mode and PKCS5Padding
as the default padding for many symmetric ciphers.

It is recommended to use transformations that fully specify the
algorithm, mode, and padding instead of relying on the defaults.

Note: ECB works well for single blocks of data and can be
parallelized, but generally should not be used for multiple blocks of
data.

Therefore, you should not just use AES but specify the mode and padding. Furthermore, although the getInstance method could have another parameter for the provider, this is not recommended because

applications are tied to specific providers that may not be available
on other Java implementations

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文