如何在.NET 中使用 DES 算法?

发布于 2024-08-12 08:47:36 字数 1006 浏览 4 评论 0原文

如何在 .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 alibraryfordecryptingDESencryption 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 技术交流群。

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

发布评论

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

评论(3

他夏了夏天 2024-08-19 08:47:36

假设您的输入是一个流

using System.Security.Cryptography

string key;
Stream input;
string output;
DESCryptoServiceProvider DES = new DESCryptoServiceProvider();
//Set key and initialization vector for DES algorithm
DES.Key = ASCIIEncoding.ASCII.GetBytes(key);
DES.IV = ASCIIEncoding.ASCII.GetBytes(key);

//Create CryptoStream layer to decrypt input on reading
CryptoStream decryptStream = new CryptoStream(input, DES.CreateDecryptor(), CryptoStreamMode.Read);
//return decrypted
return new StreamReader(decryptStream ).ReadToEnd();

,否则您当然可以轻松地将输入写入流中。
对于 ECB 模式,您还需要将 DES 对象的模式设置为 ECB:

DES.Mode = CipherMode.ECB

Assuming your input is a stream

using System.Security.Cryptography

string key;
Stream input;
string output;
DESCryptoServiceProvider DES = new DESCryptoServiceProvider();
//Set key and initialization vector for DES algorithm
DES.Key = ASCIIEncoding.ASCII.GetBytes(key);
DES.IV = ASCIIEncoding.ASCII.GetBytes(key);

//Create CryptoStream layer to decrypt input on reading
CryptoStream decryptStream = new CryptoStream(input, DES.CreateDecryptor(), CryptoStreamMode.Read);
//return decrypted
return new StreamReader(decryptStream ).ReadToEnd();

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:

DES.Mode = CipherMode.ECB
你怎么这么可爱啊 2024-08-19 08:47:36

您可以使用 DESCryptoServiceProvider。请参阅这篇文章

You can use the DESCryptoServiceProvider. See this article.

暖风昔人 2024-08-19 08:47:36

作为补充其他答案的旁注。如果可以,请不要使用它,请使用 AESCryptoServiceProvider

DES 现在被认为是不安全的
对于许多应用程序。这主要是
由于 56 位密钥大小太
小的; 1999年1月,
分布式网络和电子
前沿基金会合作
22 小时内公开破解 DES 密钥
15分钟

As a side note to complement the other answers. If you can, don't use it, use the AESCryptoServiceProvider.

DES is now considered to be insecure
for many applications. This is chiefly
due to the 56-bit key size being too
small; in January, 1999,
distributed.net and the Electronic
Frontier Foundation collaborated to
publicly break a DES key in 22 hours
and 15 minutes

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