如何在.NET 中使用 DES 算法?
如何在 .NET 中使用 DES
?
下面是我在 Java 中执行此操作的方法:
public static String decrypt(byte[] pin, byte [] desKeyData ) throws Exception {
//if (ISOConstantsLibrary.DEBUG) System.out.println("original: " + pin + " key: " + ISOUtil.bcd2str(desKeyData, 0, 2 * desKeyData.length, false) );
String out = "";
try {
SecretKeySpec desKey = new SecretKeySpec(desKeyData, "DES");
Cipher cipher = Cipher.getInstance("DES/ECB/NoPadding");//DES/CBC/PKCS5Padding
byte[] encrypted_password = pin;
cipher.init(Cipher.DECRYPT_MODE, desKey);
byte[] decrypted_password = cipher.doFinal(encrypted_password);
out = new String(decrypted_password);
//if (ISOConstantsLibrary.DEBUG) System.out.println("Decrypted Password " + out);
}
catch (Exception e) {
e.printStackTrace();
}
return out;
}
Isthere alibraryfordecryptingDES
encryption in .NET?如果是这样,我该如何使用它?
How do I use DES
in .NET?
Here's how I'd do it in Java:
public static String decrypt(byte[] pin, byte [] desKeyData ) throws Exception {
//if (ISOConstantsLibrary.DEBUG) System.out.println("original: " + pin + " key: " + ISOUtil.bcd2str(desKeyData, 0, 2 * desKeyData.length, false) );
String out = "";
try {
SecretKeySpec desKey = new SecretKeySpec(desKeyData, "DES");
Cipher cipher = Cipher.getInstance("DES/ECB/NoPadding");//DES/CBC/PKCS5Padding
byte[] encrypted_password = pin;
cipher.init(Cipher.DECRYPT_MODE, desKey);
byte[] decrypted_password = cipher.doFinal(encrypted_password);
out = new String(decrypted_password);
//if (ISOConstantsLibrary.DEBUG) System.out.println("Decrypted Password " + out);
}
catch (Exception e) {
e.printStackTrace();
}
return out;
}
Is there a library for decrypting DES
encryption in .NET? If so, how do I use it?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
假设您的输入是一个流
,否则您当然可以轻松地将输入写入流中。
对于 ECB 模式,您还需要将 DES 对象的模式设置为 ECB:
Assuming your input is a stream
otherwise you can of course easily write the input into a stream.
For ECB mode you also need to set the Mode of the DES object to ECB:
您可以使用 DESCryptoServiceProvider。请参阅这篇文章。
You can use the DESCryptoServiceProvider. See this article.
作为补充其他答案的旁注。如果可以,请不要使用它,请使用 AESCryptoServiceProvider。
As a side note to complement the other answers. If you can, don't use it, use the AESCryptoServiceProvider.