如何在 .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
加密的库?如果可以,我该如何使用它?
最佳答案
假设您的输入是一个流
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
关于java - 如何在.NET 中使用 DES 算法?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1755791/