Fundamntally, it is impossible to reliably decrypt that output base on the code you have provided as you do not provide the Initialisation Vector (IV) to you Cipher.Init call. As such, a random IV is used.
The following code might work when the above problem is corrected. You'll need to replace null with the correct IV.
public static string Decrypt(string encrypted)
{
string secretKey = "1234567890123456";
byte[] keyBytes = Encoding.UTF8.GetBytes(secretKey);
byte[] ivBytes = null;
DESCryptoServiceProvider csp = new DESCryptoServiceProvider();
ICryptoTransform dec = csp.CreateDecryptor(keyBytes, ivBytes);
byte[] cipherText = Encoding.UTF8.GetBytes(encrypted);
string plainText = null;
using (MemoryStream ms = new MemoryStream(cipherText, false))
{
ms.Position = 0;
using (CryptoStream cryptStrm = new CryptoStream(ms, dec, CryptoStreamMode.Read))
{
StreamReader rdr = new StreamReader(cryptStrm);
plainText = rdr.ReadToEnd();
}
}
return plainText;
}
There are a lot of security concerns with what you are doing though (and equally as many in the code I have provided). If this is not a hobby/understanding project, I recommend you reconsider your security design:
- You shouldn't store keys in code
- You shouldn't store sensitive information in string objects
- you should overwrite sensitive information as soon as you no longer need it with 0s or other non-sensitive data
- You shouldn't be using DES - it is no longer secure
- In C# you can use SecureString to reduce exposure of sensitive information (however, it is more difficult to get data out of this)
- Idealy, encryption/decryption code should be done in a non-virtualised language due to virtual machines being able to move data around without telling you and can therefore leave traces of sensitive information lying around in memory
- ...