带安全密钥的 DES 编码 C#

发布于 2024-10-12 16:57:13 字数 85 浏览 5 评论 0原文

我知道这可能是一个常见问题,但我在任何地方都找不到答案。所以我有字节数组键和字节数组值,我需要生成新的 8 字节数组,该数组已在 C# 中使用 DES 加密

I know It's a probably common question but I cannot find answer anywhere. So I have byte array key and byte array value and I need to produce new 8 byte array that has been encrypted with DES in C#

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

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

发布评论

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

评论(2

朮生 2024-10-19 16:57:13

这是您的示例代码。请记住用随机数据填充尾随零,记住写入的字节和 DES 参数:Key、IV。

最好的祝愿 ;)

using System.Security.Cryptography;
using System.IO;
namespace hash
{
    public static class Program
    {
        static void Main(string[] args)
        {
            byte[] data = new byte[10000];
            DES des = DES.Create();
            int bytesWritten = 0;
            data = Encode(data, des, out bytesWritten);
        }

        private static byte[] Encode(byte[] data, DES des, out int bytesWritten)
        {
            using (var input = new MemoryStream(data))
            using (var output = new MemoryStream())
            using (var csp = new DESCryptoServiceProvider())
            using (var encStream = new CryptoStream(output, csp.CreateEncryptor(des.Key, des.IV), CryptoStreamMode.Write))
            {
                int length = 0;
                byte[] buffer = new byte[256];
                bytesWritten = 0;
                while ((length = input.Read(buffer, 0, 256)) > 0)
                {
                    if (length < 256)
                    {
                        byte[] pad = new byte[256];
                        using (var rng = RNGCryptoServiceProvider.Create())
                        {
                            rng.GetBytes(pad);
                            for (int i = 0; i < 256 - length; i++)
                            {
                                buffer[length + i] = pad[i];
                            }
                        }
                        encStream.Write(buffer, 0, length);
                        bytesWritten += length;
                        break;
                    }
                    encStream.Write(buffer, 0, 256);
                    bytesWritten += length;
                }
                return output.ToArray();
            }
        }
    }
}

Here is your sample code. Remember to pad trailig zeros with random data, remember the bytes written, and DES parametrs: Key, IV.

Best wishes ;)

using System.Security.Cryptography;
using System.IO;
namespace hash
{
    public static class Program
    {
        static void Main(string[] args)
        {
            byte[] data = new byte[10000];
            DES des = DES.Create();
            int bytesWritten = 0;
            data = Encode(data, des, out bytesWritten);
        }

        private static byte[] Encode(byte[] data, DES des, out int bytesWritten)
        {
            using (var input = new MemoryStream(data))
            using (var output = new MemoryStream())
            using (var csp = new DESCryptoServiceProvider())
            using (var encStream = new CryptoStream(output, csp.CreateEncryptor(des.Key, des.IV), CryptoStreamMode.Write))
            {
                int length = 0;
                byte[] buffer = new byte[256];
                bytesWritten = 0;
                while ((length = input.Read(buffer, 0, 256)) > 0)
                {
                    if (length < 256)
                    {
                        byte[] pad = new byte[256];
                        using (var rng = RNGCryptoServiceProvider.Create())
                        {
                            rng.GetBytes(pad);
                            for (int i = 0; i < 256 - length; i++)
                            {
                                buffer[length + i] = pad[i];
                            }
                        }
                        encStream.Write(buffer, 0, length);
                        bytesWritten += length;
                        break;
                    }
                    encStream.Write(buffer, 0, 256);
                    bytesWritten += length;
                }
                return output.ToArray();
            }
        }
    }
}
半世蒼涼 2024-10-19 16:57:13

这就是我一直在寻找的:D:D...谢谢你:D

   private static byte[] Encrypt(byte[] value, byte[] key)
    {
        DESCryptoServiceProvider cryptoProvider = new DESCryptoServiceProvider
                                                      {
                                                          Mode = CipherMode.ECB,
                                                          Padding = PaddingMode.None
                                                      };

        MemoryStream memoryStream = new MemoryStream();

        CryptoStream cryptoStream = new CryptoStream(memoryStream, cryptoProvider.CreateEncryptor(key, key), CryptoStreamMode.Write);

        cryptoStream.Write(value, 0, value.Length);
        cryptoStream.Close();

        return memoryStream.ToArray();
    }

This is what I was looking for :D:D...thank you :D

   private static byte[] Encrypt(byte[] value, byte[] key)
    {
        DESCryptoServiceProvider cryptoProvider = new DESCryptoServiceProvider
                                                      {
                                                          Mode = CipherMode.ECB,
                                                          Padding = PaddingMode.None
                                                      };

        MemoryStream memoryStream = new MemoryStream();

        CryptoStream cryptoStream = new CryptoStream(memoryStream, cryptoProvider.CreateEncryptor(key, key), CryptoStreamMode.Write);

        cryptoStream.Write(value, 0, value.Length);
        cryptoStream.Close();

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