c# - 快速轻量级 .NET 客户端加密 -> 服务器解密

标签 c# .net encryption

我有一个简单的客户端/服务器设置。客户端和服务端都有私钥。

.NET 为我提供了什么

ClientData->ClientEncrypt with KEY->Transmit to Server->ServerDecrypt with KEY->ClientData

任何人都可以推荐任何快速简单的库来阅读吗?

谢谢

最佳答案

RijndaelManaged :

这是 an example :

private static string CreateEncryptedString(string myString, string hexiv, string key)
        {
            RijndaelManaged alg = new RijndaelManaged();
            alg.Padding = PaddingMode.Zeros;
            alg.Mode = CipherMode.CBC;
            alg.BlockSize = 16 * 8;
            alg.Key = ASCIIEncoding.UTF8.GetBytes(key);
            alg.IV = StringToByteArray(hexiv);
            ICryptoTransform encryptor = alg.CreateEncryptor(alg.Key, alg.IV);

            MemoryStream msStream = new MemoryStream();
            CryptoStream mCSWriter = new CryptoStream(msStream, encryptor, CryptoStreamMode.Write);
            StreamWriter mSWriter = new StreamWriter(mCSWriter);
            mSWriter.Write(myString);
            mSWriter.Flush();
            mCSWriter.FlushFinalBlock();

            var EncryptedByte = new byte[msStream.Length];
            msStream.Position = 0;
            msStream.Read(EncryptedByte, 0, (int)msStream.Length);

            return ByteArrayToHexString(EncryptedByte);

        }
        public static byte[] StringToByteArray(String hex)
        {
            int NumberChars = hex.Length;
            byte[] bytes = new byte[NumberChars / 2];
            for (int i = 0; i < NumberChars; i += 2)
                bytes[i / 2] = Convert.ToByte(hex.Substring(i, 2), 16);
            return bytes;
        }
        public static string ByteArrayToHexString(byte[] ba)
        {
            StringBuilder hex = new StringBuilder(ba.Length * 2);
            foreach (byte b in ba)
                hex.AppendFormat("{0:x2}", b);
            return hex.ToString();
        }

你可以很容易地得出一个解密算法和例子(或者只是谷歌一下!)

关于c# - 快速轻量级 .NET 客户端加密 -> 服务器解密,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1497287/

相关文章:

c# - 无法加载类型 'newVersion_Default'

c# - 属性 'Context' 是 F# 中的静态错误

c# - WinRT 中的应用程序间通信

c# - 生成附属程序集时出现警告 AL1073

php - 做专,跟上

ios - 在 iOS 上通过蓝牙进行类似 TLS 的加密?

c# - 错误 : String or binary data would be truncated. 语句已终止。 ASPX

.net - 从 XmlDocument 到 XmlDocument 的 XslCompiledTransform

c++ - 读/写文件,字节丢失,可能是字符溢出

iphone - 我可以在 iPhone 上使用哪些安全框架和工具来保护我的数据?