java 中的加密 - 最佳实践?
我正在开发一个 Java 客户端-服务器应用程序,它将使用私钥加密。
目前,我有一个带有 Cipher 类静态对象的类,我将其初始化为:myCipher.init(Cipher.DECRYPT_MODE, SecretKey, ivParameterSpec); 。
现在我的问题是:我有双向通信,我是否应该为每种方式都有一个单独的 Cipher 对象(一个用于加密,一个用于解密)并使用相同的初始化向量?或者我应该使用相同的对象并调用 Cipher.init() 来根据我是加密还是解密来更改模式?
在我的脑海中拥有不同的物体是有道理的,但我只是想确定一下。我尝试过谷歌搜索,但大多数示例仅显示一种加密方式。
目前我将初始化向量发送到未加密的服务器,这是正确的还是存在安全缺陷?
或者我的处理方式完全错误?
谢谢。
I am working on a client-server application in Java which is going to use private-key encryption.
Currently I have a class with a static object of the Cipher
class which I initialise like this: myCipher.init(Cipher.DECRYPT_MODE, secretKey, ivParameterSpec);
.
Now my question is: I have two-way communication, should I have a separate Cipher
object for each way (one for encrypting and one for decrypting) and use the same initialisation vector? Or should I just use the same object and call Cipher.init()
to change the modes depending on whether I am encrypting or decrypting?
It makes sense to have separate objects in my head, but I just wanted to be sure. I tried googling but most examples only show encryption one way.
Currently I send the initialisation vector to the server unencrypted, is this correct, or is there a security flaw?
Or am I approaching it completely the wrong way?
Thanks.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您的问题有两个方面:
您应该使用一个对象。这样可以节省内存。但如果内存可以使用,那就用两个吧。至少这将使您的代码更具可读性。
IV 不需要加密。 IV 是一个随机值,由发送方和接收方同步。关于 IV 最重要的事情是它不能用相同的密钥重复或重新使用!
Your question is two fold:
You should use one object. This saves memory. But if memory is spendable, go ahead with two. At least that'll make your code more readable.
IV needs not encrypted. IV is a random value, sync'd by both the sender and receiver. The most important thing about IV is it must not be repeated, or re-used, with the same key!