How to implement System.Security.Cryptography.DES
I have the following static encryption class with extension methods.
public static class Encryptor {
public static byte[] Encrypt(this object obj) {
SymmetricAlgorithm sa = DES.Create();
BinaryFormatter bf = new BinaryFormatter();
MemoryStream ms = new MemoryStream();
bf.Serialize(ms, obj);
byte[] plaintextBytes = new byte[ms.Length];
plaintextBytes = ms.ToArray();
sa = DES.Create();
MemoryStream msEncrypt = new MemoryStream();
CryptoStream csEncrypt = new CryptoStream(msEncrypt, sa.CreateEncryptor(), CryptoStreamMode.Write);
csEncrypt.Write(plaintext开发者_如何学编程Bytes, 0, plaintextBytes.Length);
csEncrypt.Close();
byte[] encryptedTextBytes = msEncrypt.ToArray();
msEncrypt.Close();
return encryptedTextBytes;
}
public static T Decrypt<T>(this byte[] bytes) {
SymmetricAlgorithm sa = DES.Create();
MemoryStream msDecrypt = new MemoryStream(bytes);
CryptoStream csDecrypt = new CryptoStream(msDecrypt, sa.CreateDecryptor(), CryptoStreamMode.Read);
byte[] decryptedTextBytes = new Byte[bytes.Length];
csDecrypt.Read(decryptedTextBytes, 0, bytes.Length);
csDecrypt.Close();
msDecrypt.Close();
BinaryFormatter bf = new BinaryFormatter();
MemoryStream ms = new MemoryStream(decryptedTextBytes);
return (T)bf.Deserialize(ms);
}
}
Unfortunately though, the decrypt method always throws an error: Bad Data
The only way I have gotten around this is by making the SymmetricAlgorithm
object static as well, but that doesn't help across sessions and recycles.
Am I missing something?
STACK TRACE
[CryptographicException: Bad Data.]
System.Security.Cryptography.CryptographicException.ThrowCryptographicException(Int32 hr) +33
System.Security.Cryptography.Utils._DecryptData(SafeKeyHandle hKey, Byte[] data, Int32 ib, Int32 cb, Byte[]& outputBuffer, Int32 outputOffset, PaddingMode PaddingMode, Boolean fDone) +0
System.Security.Cryptography.CryptoAPITransform.TransformFinalBlock(Byte[] inputBuffer, Int32 inputOffset, Int32 inputCount) +313
System.Security.Cryptography.CryptoStream.Read(Byte[] buffer, Int32 offset, Int32 count) +649
LinkSubmitter.Encryptor.Decrypt(Byte[] bytes) in C:\Users\Jeremy\Documents\Visual Studio 2010\Projects\LinkSubmitter\LinkSubmitter\Encryptor.cs:102
Your problem is that you have 2 "sa = DES.Create();" in the Encrypt() method, this second one resets the key and iv also you need to specify the Key and IV. You encrypt with one key then when you decrypt you are using another key so it's not going to decrypt.
Hope it helps.
SymmetricAlgorithm sa = DES.Create();
sa.Key = new [] {0x38, 0x4B, 0x1A, 0x0A, 0x0F, 0x1D, 0xFA, 0x0D};
sa.IV = new [] {0x30, 0x40, 0x10, 0x00, 0x00, 0x10, 0xF0, 0x00};
You need the same key and Iv for Encrypt and Decrypt.
DES is a very weak Algorithm you might want to think about AES/Rijndael. Even TripleDES is better then just plain DES.
You incorrectly assume that the ciphertext will be the same length as the plaintext.
Any decent cipher (and even DES, which isn't a decent cipher) will create ciphertext which is substantially larger than the plaintext.
You need to keep reading from the CipherStream until you run out of data.
Note, by the way, that you should be able to make your code much simpler by serializing and deserializing directly against the CipherStream.
Implemented RijndaelSimple class from http://www.obviex.com/samples/Encryption.aspx
精彩评论