我需要在c#中使用DES算法执行解密。我有加密图像的代码。我不知道代码要用图像标题执行解密。
以下是我的加密代码。
FileStream fsInput = new FileStream(source, FileMode.Open, FileAccess.Read);
FileStream fsEncrypted = new FileStream(destination, FileMode.Create, FileAccess.Write);
DESCryptoServiceProvider DES = new DESCryptoServiceProvider();
DES.Key = ASCIIEncoding.ASCII.GetBytes(sKey);
DES.IV = ASCIIEncoding.ASCII.GetBytes(sKey);
ICryptoTransform desencrypt = DES.CreateEncryptor();
CryptoStream cryptostream = new CryptoStream(fsEncrypted, desencrypt, CryptoStreamMode.Write);
byte[] headerBuffer = new byte[54];
fsInput.Read(headerBuffer, 0, headerBuffer.Length);
var biCompression = BitConverter.ToInt32(headerBuffer, 30);
if (biCompression != 0 && biCompression != 3)
{
throw new Exception("Compression is not in the correct format");
}
fsEncrypted.Write(headerBuffer, 0, headerBuffer.Length);
fsInput.CopyTo(cryptostream);
cryptostream.Close();
fsInput.Close();
fsEncrypted.Close();
答案 0 :(得分:2)
您可以使用如下所示的Decrypt方法解密加密图像:
private static void Decrypt( string encryptedFilePath, string decryptedOutputLocation)
{
var key = "12345678"; //this will be the key you used for encryption
FileStream fsInput = new FileStream(encryptedFilePath, FileMode.Open, FileAccess.Read);
FileStream fsDecrypted = new FileStream(decryptedOutputLocation, FileMode.Create, FileAccess.Write);
DESCryptoServiceProvider DES = new DESCryptoServiceProvider();
DES.Key = ASCIIEncoding.ASCII.GetBytes(key.ToCharArray());
DES.IV = ASCIIEncoding.ASCII.GetBytes(key.ToCharArray());
var desDecryptor = DES.CreateDecryptor();
CryptoStream cryptostream = new CryptoStream(fsDecrypted, desDecryptor,
CryptoStreamMode.Write);
byte[] headerBuffer = new byte[54];
fsInput.Read(headerBuffer, 0, headerBuffer.Length);
fsDecrypted.Write(headerBuffer, 0, headerBuffer.Length);
fsInput.CopyTo(cryptostream);
cryptostream.Close();
fsInput.Close();
fsDecrypted.Close();
}
BTW,在你的代码中
if (biCompression != 0 && biCompression != 3)
{
throw new Exception("Compression is not in the correct format");
}
这将始终抛出异常。这也是你应该看的地方。