Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/.net/22.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 如何在.NET中使用DES算法?_Java_.net_Des_Encryption - Fatal编程技术网

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

Java 如何在.NET中使用DES算法?,java,.net,des,encryption,Java,.net,Des,Encryption,如何在.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 * desKeyD

如何在.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;
}

在.NET中是否有用于解密
DES
加密的库?如果是,我该如何使用它?

您可以使用DESCryptoServiceProvider。请参见此图。

假设您的输入是一个流

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

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

DES现在被认为是不安全的 对于许多应用程序。这主要是 由于56位密钥大小太大 小的1999年1月,, 分布式.net与电子商务 边境基金会合作 在22小时内公开破解DES密钥 还有15分钟