经验首页 前端设计 程序设计 Java相关 移动开发 数据库/运维 软件/图像 大数据/云计算 其他经验
当前位置:技术经验 » 程序设计 » C# » 查看文章
再谈 C# 对象二进制序列化,序列化并进行 AES 加密
来源:cnblogs  作者:大豆男生  时间:2018/11/23 10:21:00  对本文有异议

对象的二进制序列化非常有用,也非常方便。

我们可以把对象序列化为字节数组,也可以把对象序列化到文件,还可以把对象序列化到文件并进行加密。 

先引用这些命名空间:

using System.IO;
using System.Runtime.Serialization.Formatters.Binary;
using System.Security.Cryptography;
using System.Text;

序列化对象到字节数组:

  1. /// <summary>
  2. /// 把对象序列化为字节数组
  3. /// </summary>
  4. public static byte[] SerializeObjectToBytes(object obj)
  5. {
  6. if (obj == null)
  7. return null;
  8. MemoryStream ms = new MemoryStream();
  9. BinaryFormatter formatter = new BinaryFormatter();
  10. formatter.Serialize(ms, obj);
  11. byte[] bytes = ms.ToArray();
  12. return bytes;
  13. }
  14. /// <summary>
  15. /// 把字节数组反序列化成对象
  16. /// </summary>
  17. public static object DeserializeObjectFromBytes(byte[] bytes)
  18. {
  19. object obj = null;
  20. if (bytes == null)
  21. return obj;
  22. MemoryStream ms = new MemoryStream(bytes)
  23. {
  24. Position = 0
  25. };
  26. BinaryFormatter formatter = new BinaryFormatter();
  27. obj = formatter.Deserialize(ms);
  28. ms.Close();
  29. return obj;
  30. }

 

序列化对象到文件:

  1. public static void SerializeObjectToFile(string fileName, object obj)
  2. {
  3. using (FileStream fs = new FileStream(fileName, FileMode.Create))
  4. {
  5. BinaryFormatter formatter = new BinaryFormatter();
  6. formatter.Serialize(fs, obj);
  7. }
  8. }
  9. /// <summary>
  10. /// 把文件反序列化成对象
  11. /// </summary>
  12. public static object DeserializeObjectFromFile(string fileName)
  13. {
  14. using (FileStream fs = new FileStream(fileName, FileMode.Open, FileAccess.Read, FileShare.Read))
  15. {
  16. BinaryFormatter formatter = new BinaryFormatter();
  17. object obj = formatter.Deserialize(fs);
  18. return obj;
  19. }
  20. }

 

序列化对象到文件,并进行 AES 加密:

  1. /// <summary>
  2. /// 把对象序列化到文件(AES加密)
  3. /// </summary>
  4. /// <param name="keyString">密钥(16位)</param>
  5. public static void SerializeObjectToFile(string fileName, object obj, string keyString)
  6. {
  7. using (AesCryptoServiceProvider crypt = new AesCryptoServiceProvider())
  8. {
  9. crypt.Key = Encoding.ASCII.GetBytes(keyString);
  10. crypt.IV = Encoding.ASCII.GetBytes(keyString);
  11. using (ICryptoTransform transform = crypt.CreateEncryptor())
  12. {
  13. FileStream fs = new FileStream(fileName, FileMode.Create);
  14. using (CryptoStream cs = new CryptoStream(fs, transform, CryptoStreamMode.Write))
  15. {
  16. BinaryFormatter formatter = new BinaryFormatter();
  17. formatter.Serialize(cs, obj);
  18. }
  19. }
  20. }
  21. }
  22. /// <summary>
  23. /// 把文件反序列化成对象(AES加密)
  24. /// </summary>
  25. /// <param name="keyString">密钥(16位)</param>
  26. public static object DeserializeObjectFromFile(string fileName, string keyString)
  27. {
  28. using (AesCryptoServiceProvider crypt = new AesCryptoServiceProvider())
  29. {
  30. crypt.Key = Encoding.ASCII.GetBytes(keyString);
  31. crypt.IV = Encoding.ASCII.GetBytes(keyString);
  32. using (ICryptoTransform transform = crypt.CreateDecryptor())
  33. {
  34. FileStream fs = new FileStream(fileName, FileMode.Open, FileAccess.Read, FileShare.Read);
  35. using (CryptoStream cs = new CryptoStream(fs, transform, CryptoStreamMode.Read))
  36. {
  37. BinaryFormatter formatter = new BinaryFormatter();
  38. object obj = formatter.Deserialize(cs);
  39. return obj;
  40. }
  41. }
  42. }
  43. }

 

 友情链接:直通硅谷  点职佳  北美留学生论坛

本站QQ群:前端 618073944 | Java 606181507 | Python 626812652 | C/C++ 612253063 | 微信 634508462 | 苹果 692586424 | C#/.net 182808419 | PHP 305140648 | 运维 608723728

W3xue 的所有内容仅供测试,对任何法律问题及风险不承担任何责任。通过使用本站内容随之而来的风险与本站无关。
关于我们  |  意见建议  |  捐助我们  |  报错有奖  |  广告合作、友情链接(目前9元/月)请联系QQ:27243702 沸活量
皖ICP备17017327号-2 皖公网安备34020702000426号