如何实现 System.Security.Cryptography.DES

发布于 2024-10-06 06:41:29 字数 2318 浏览 6 评论 0原文

我有以下带有扩展方法的静态加密类。

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(plaintextBytes, 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);
    }
}

不幸的是,解密方法总是抛出一个错误:Bad Data

我解决这个问题的唯一方法是使 SymmetricAlgorithm 对象也静态化,但这并不能解决问题。跨会话和回收提供帮助。

我错过了什么吗?

堆栈跟踪

[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

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(plaintextBytes, 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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

迷途知返 2024-10-13 06:41:29

你的问题是你有 2 个“sa = DES.Create();”在 Encrypt() 方法中,第二个方法会重置密钥和 iv,您还需要指定密钥和 IV。您使用一个密钥加密,然后当您解密时您使用另一密钥,因此它不会解密。

希望有帮助。

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};

加密和解密需要相同的密钥和 Iv。

DES 是一种非常弱的算法,您可能需要考虑 AES/Rijndael。即使 TripleDES 也比普通 DES 更好。

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.

提笔落墨 2024-10-13 06:41:29

您错误地认为密文与明文的长度相同。

任何合适的密码(甚至是 DES,这不是一个合适的密码)都会创建比明文大得多的密文。

您需要继续从 CipherStream 中读取数据,直到用完数据。


顺便请注意,通过直接针对 CipherStream 进行序列化和反序列化,您应该能够使代码变得更加简单。

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.

陌路黄昏 2024-10-13 06:41:29

http://www.obviex.com/samples/Encryption.aspx 实现了 RijndaelSimple 类

Implemented RijndaelSimple class from http://www.obviex.com/samples/Encryption.aspx

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文