DESKeySpec 不安全吗?
我知道 DES 目前被认为是一种不安全的加密算法。那么这是否意味着我们应该停止使用 JCA 中的 DESKeySpec 类来指定加密密钥?更具体地说,由于使用 DESKeySpec,下面的代码片段是否不安全?如果是,为什么,我应该如何纠正?
public void setPassword(String p) throws FacesException {
byte\[\] s`your text`={(byte)0xA9,(byte)0x9B,(byte)0xC8,(byte)0x32,(byte)0x56,(byte)0x34,(byte)0xE3,(byte)0x03};
try {
KeySpec keySpec=new DESKeySpec(p.getBytes("UTF8"));
SecretKey key=SecretKeyFactory.getInstance("DES").generateSecret(keySpec);
e=Cipher.getInstance(key.getAlgorithm());
d=Cipher.getInstance(key.getAlgorithm());
e.init(Cipher.ENCRYPT_MODE,key);
d.init(Cipher.DECRYPT_MODE,key);
}
catch ( Exception e) {
throw new FacesException("Error set encryption key",e);
}
}
I am aware that DES is considered as an insecure algorithm for encryption at the moment. So does that imply that we should stop using the class DESKeySpec in JCA for specifying a key for encryption? More specifically, is the below code snippet insecure due to the usage of DESKeySpec? If yes, why, and how should I rectify it?
public void setPassword(String p) throws FacesException {
byte\[\] s`your text`={(byte)0xA9,(byte)0x9B,(byte)0xC8,(byte)0x32,(byte)0x56,(byte)0x34,(byte)0xE3,(byte)0x03};
try {
KeySpec keySpec=new DESKeySpec(p.getBytes("UTF8"));
SecretKey key=SecretKeyFactory.getInstance("DES").generateSecret(keySpec);
e=Cipher.getInstance(key.getAlgorithm());
d=Cipher.getInstance(key.getAlgorithm());
e.init(Cipher.ENCRYPT_MODE,key);
d.init(Cipher.DECRYPT_MODE,key);
}
catch ( Exception e) {
throw new FacesException("Error set encryption key",e);
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
是的,DES 非常不安全,不应该在任何新项目中使用。任何新项目都应该使用 AES,除非您有非常充分且明智的其他理由。
但是,您应该对上述代码执行的操作完全取决于它的用途。如果此代码与需要 DES 的系统进行互操作,则您不能只更改它而不更改另一端。同样,如果您有使用 DES 加密的现有数据,您将需要一种方法来继续解密该数据。您不能仅将 AES 替换为 DES,并让现有加密继续有效。世界上有一些重要的系统仍然使用 DES(信用卡磁条可能是最普遍的)。
也就是说,您不能只是将“AES”交换到上面的代码中并拥有一个安全的加密系统。您需要选择适当的模式、随机 IV(很可能是 KDF)以及某种身份验证。 DES 和 AES 等加密算法是非常低级的工具,很容易被滥用。它们只是安全密码系统的一部分。正确的解决方案取决于您的具体情况,特别是您需要哪种类型的互操作性。
Yes, DES is very insecure, and shouldn't be used in any new projects. Any new project should be using AES unless you have a very strong and informed reason for something else.
However, what you should do about the above code completely depends on what it's being used for. If this code is interoperating with a system that requires DES, you can't just change it without changing the other side as well. Similarly, if you have existing data encrypted in DES, you'll need a way to continue to decrypt that. You can't just swap AES for DES and have exiting encryptions continue to work. There are important systems in the world that still use DES (credit card magnetic stripes being probably the most ubiquitous).
That said, you can't just swap "AES" into the above code and have a secure cryptosystem. You need to choose a proper mode, a random IV, most likely a KDF, and some kind of authentication. Encryption algorithms like DES and AES are very low-level tools that are easy to misuse. They are just one piece of a secure cryptosystem. The proper solution depends on your specific situation, particularly what kinds of interoperability you need.