加密.NET二进制序列化流我正在学习C#中的加密,但遇到了麻烦。我有一些与字符串完美配合的Rijndael加密代码。但是现在我在做序列化,BinaryWriter写的是类的数据,没有任何保护。我正在使用此代码进行测试;有没有办法“加密课程”或类似的东西?为了澄清这个问题,这是我的代码:FileStreamfile=newFileStream(Environment.CurrentDirectory+@"class.dat",FileMode.Create);使用(BinaryWritersw=newBinaryWriter(file)){byte[]byte=ConverteObjectEmByte(myVarClass);sw.Write(字节);这就是我读它的方式:MyClassnewMyVarClass;FileStreamfile=newFileStream(Environment.CurrentDirectory+@"class.dat",FileMode.Open);using(BinaryReadersr=newBinaryReader(file)){//218是我测试过的字节数组的大小(字节)myNewVarClass=(MyClass)ConverteByteEmObject(sr.ReadBytes(218));}谢谢!在传递给不同的流对象时,不是将转换为byte[]作为中间步骤,而是可以将多个流链接在一起,将一个流的输出传递到另一个流的输入。这种方法在这里很有意义,因为您将二进制序列化=>加密=>写入文件链接在一起。考虑到这一点,您可以将ConvertObjectEmByte更改为:}BinaryFormatterbf=newBinaryFormatter();bf.Serialize(outputStream,obj);}同样,ConvertByteEmObject可以变成:publicstaticobjectReadObjectFromStream(StreaminputStream){BinaryFormatterbinForm=newBinaryFormatter();objectobj=binForm.Deserialize(inputStream);返回对象;要添加加密/解密,我们可以编写创建CryptoStream对象的函数,我们可以使用这些二进制序列化函数链接这些对象。下面的示例函数与您链接的文章中的加密/解密函数略有不同,IV(初始化向量)现在随机生成并写入流(并从另一端的流中读取)。重要的是,为了安全起见,每个加密数据块的IV都是唯一的,并且您还应该使用随机数生成器来进行加密,例如RNGCryptoServiceProvider,而不是像Randomdevice这样的伪随机数生成器。publicstaticCryptoStreamCreateEncryptionStream(byte[]key,StreamoutputStream){byte[]iv=newbyte[ivSize];using(varrng=newRNGCryptoServiceProvider()){//使用加密随机数生成器rng.GetNonZeroBytes(iv);}//将IV写入流的开头outputStream.Write(iv,0,iv.Length);Rijndaelrijndael=newRijndaelManaged();rijndael.KeySize=keySize;CryptoStream加密器=newCryptoStream(outputStream,rijndael.CreateEncryptor(key,iv),CryptoStreamMode.Write);返回加密器;}publicstaticCryptoStreamCreateDecryptionStream(byte[]key,StreaminputStream){byte[]iv=newbyte[ivSize];if(inputStream.Read(iv,0,iv.Length)!=iv.Length){thrownewApplicationException("无法从流中读取IV。");}Rijndaelrijndael=newRijndaelManaged();rijndael.KeySize=keySize;CryptoStream解密器=newCryptoStream(inputStream,rijndael.CreateDecryptor(key,iv),CryptoStreamMode.Read);返回d加密器;最后,我们可以将它们粘合在一起:byte[]key=Convert.FromBase64String(cryptoKey);使用(FileStreamfile=newFileStream(Environment.CurrentDirectory+@"class.dat",FileMode.Create))使用(CryptoStreamcryptoStream=CreateEncryptionStream(key,file)){}MyClassnewMyVarClass;使用(FileStreamfile=newFileStream(Environment.CurrentDirectory+@"class.dat",FileMode.Open))使用(CryptoStringcryptoStream=CreateDecryptionStream(key,file))请注意,我们将文件流对象传递给CreateEncryptionStream(和CreateDecryptionStream),然后将cryptoStream对象传递给WriteObjectToStream(和ReadObjectfromStream)。您还会注意到使用块的流进行内部节流,以便在我们完成时自动清除它们他们。这是完整的测试程序:以上是《C#学习教程:加密.NET二进制序列化流》分享的全部内容。如果对你有用,需要进一步了解C#学习教程,希望大家多多关注——usingSystem;使用System.Runtime.Serialization.Formatters.Binary;使用System.Security.Cryptography;命名空间CryptoStreams{classProgram{[Serializable]publicclassMyClass{publicstringTestValue{get;放;}publicintSomeInt{得到;放;}}publicstaticvoidWriteObjectToStream(StreamoutputStream,Objectobj){if(object.ReferenceEquals(null,obj)){返回;}BinaryFormatterbf=newBinaryFormatter();bf.Serialize(outputStream,obj);}publicstaticobjectReadObjectFromStream(StreaminputStream){BinaryFormatterbinForm=newBinaryFormatter();objectobj=binForm.Deserialize(inputStream);返回对象;}privateconststringcryptoKey="Q3JpcHRvZ3JhZmlhcyBjb20gUmluamRhZWwgLyBBRVM=";私人常量intkeySize=256;私有常量ivSize=16;//块大小为128位publicstaticCryptoStreamCreateEncryptionStream(byte[]key,StreamoutputStream){byte[]iv=newbyte[ivSize];using(varrng=newRNGCryptoServiceProvider()){//使用加密随机数生成器rng.GetNonZeroBytes(iv);}//将IV写入流的开头outputStream.Write(iv,0,iv.Length);Rijndaelrijndael=newRijndaelManaged();rijndael.KeySize=keySize;CryptoStream加密器=newCryptoStream(outputStream,rijndael.CreateEncryptor(key,iv),CryptoStreamMode.Write);返回加密器;}publicstaticCryptoStreamCreateDecryptionStream(byte[]key,StreaminputStream){byte[]iv=newbyte[ivSize];if(inputStream.Read(iv,0,iv.Length)!=iv.Length){thrownewApplicationException("无法从流中读取IV。");}Rijndaelrijndael=newRijndaelManaged();rijndael.KeySize=keySize;CryptoStream解密器=newCryptoStream(inputStream,rijndael.CreateDecryptor(key,iv),CryptoStreamMode.Read);返回解密器;}staticvoidMain(string[]args){MyClassmyVarClass=newMyClass{SomeInt=1234,TestValue="Hello"};byte[]key=Convert.FromBase64String(cryptoKey);使用(FileStreamfile=newFileStream(Environment.CurrentDirectory+@“class.dat”,FileMode.Create)){使用(CryptoStreamcryptoStream=CreateEncryptionStream(key,file)){WriteObjectToStream(cryptoStream,myVarClass);}}MyClassnewMyVarClass;使用(FileStreamfile=newFileStream(Environment.CurrentDirectory+@"class.dat",FileMode.Open))}Console.WriteLine("newMyVarClass.SomeInt:{0};newMyVarClass.TestValue:{1}",newMyVarClass.SomeInt,newMyVarClass.TestValue);}}}本文收集自网络,不代表立场。如涉及侵权,请点击右侧联系管理员删除。如有转载请注明出处:
