简体   繁体   English

对称AES加密和解密在任何Java版本上返回相同的值,并且支持Java 6,并且无需使用任何外部库

[英]Symmetric AES Encryption and Decryption returning same value on any Java versions and supporting Java 6 and with out using any external libraries

I want to get the same encrypted value on any version of Java and the code should support the Java 6 and with out using any external libraries. 我想在任何版本的Java上都获得相同的加密值,并且代码应支持Java 6,并且无需使用任何外部库。 I am new to Java and Cryptography. 我是Java和密码学的新手。 I really appreciate it someone help. 我真的很感激有人的帮助。 Thanks in advance. 提前致谢。

If you encrypt the same plaintext with the same key multiple times, you're supposed to get different results. 如果用相同的密钥多次加密相同的明文,那么您将获得不同的结果。 This is a security property and is due to randomized initialization vectors or nonces. 这是一种安全属性,并且是由于随机初始化向量或随机数引起的。

The only popular mode of operation that doesn't use an IV or a nonce is the ECB mode which is available in all Java versions. 唯一不使用IV或随机数的流行操作模式是在所有Java版本中都可用的ECB模式。 If you use that mode, you will always get the same result across all versions of Java. 如果使用该模式,则在所有Java版本中始终会得到相同的结果。

ECB mode is not very secure and should not be used. ECB模式不是很安全,因此不应使用。

If you simply need an implementation that is compatible across many versions of Java, then you could just use JNCryptor . 如果您只需要一个与许多Java版本兼容的实现,则可以使用JNCryptor It is very secure. 这是非常安全的。 Since you cannot compare the ciphertexts of a randomized mode, you will need to encrypt in one Java version and decrypt in another (don't forget the other direction). 由于您无法比较随机模式的密文,因此您将需要在一个Java版本中进行加密,而在另一个Java版本中进行解密(请不要忘记另一个方向)。

without using any external libraries 无需使用任何外部库

Cryptography is hard and you said that you're new to cryptography. 密码学很困难,您说您是密码学的新手。 You need to use vetted and secure libraries instead of trying to write your own, if you want to use it for more than learning. 如果您想使用它而不是学习,则需要使用经过审核的安全库,而不是尝试编写自己的库。 Libraries are there to help you produce more secure code. 库在那里可以帮助您生成更安全的代码。

Of course, most libraries are open source, so you could just copy their code into your project and then you don't have any dependencies, but before you do this, check if this is compatible with the license of the library. 当然,大多数库都是开源的,因此您可以将其代码复制到项目中,然后就没有任何依赖关系,但是在执行此操作之前,请检查它是否与库的许可证兼容。

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

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