当前位置:   article > 正文

C#执行数据加密的DES.Create 方法

C#执行数据加密的DES.Create 方法

目录

一、重载

二、Create()

1.定义 

2.实例1,加密和解密文件中的数据

3.实例2,加密和解密内存中的数据

三、Create(String)

四、关于DES加密


        命名空间:
        System.Security.Cryptography
        数据集:
        System.Security.Cryptography.dll

        创建加密对象的实例以执行数据加密标准 (DES) 算法。

一、重载

Create()创建加密对象的实例以执行数据加密标准 (DES) 算法。
Create(String)创建加密对象的实例以执行数据加密标准 (DES) 算法的指定实现。

二、Create()

        创建加密对象的实例以执行数据加密标准 (DES) 算法。

1.定义 

  1. public static System.Security.Cryptography.DES Create ();
  2. 返回
  3. DES
  4. 一个加密对象。

2.实例1,加密和解密文件中的数据

  1. // DES.Create()
  2. // 创建和使用 DES 对象来加密和解密文件中的数据。
  3. using System.Security.Cryptography;
  4. using System.Text;
  5. namespace ConsoleApp8
  6. {
  7. class DESSample
  8. {
  9. static void Main()
  10. {
  11. try
  12. {
  13. byte[] key;
  14. byte[] iv;
  15. using (DES des = DES.Create()) // 以随机秘钥创建加密流对象,初始化IV
  16. {
  17. key = des.Key;
  18. iv = des.IV;
  19. }
  20. string original = "大海航行靠舵手"; //要加密的原始字符串
  21. string filename = "CText.enc"; //加密后要文件名和路径默认当前工作目录
  22. EncryptTextToFile(original, filename, key, iv);//把字符串加密成文件
  23. string decrypted = DecryptTextFromFile(filename, key, iv);//从文件中解密出字符串
  24. Console.WriteLine(decrypted); //输出解密后的字符串
  25. }
  26. catch (Exception e)
  27. {
  28. Console.WriteLine(e.Message);
  29. }
  30. }
  31. /// <summary>
  32. /// 自定义加密方法:
  33. /// 创建并打开文件流;
  34. /// 创建DES对象;
  35. /// 以随机秘钥创建DES加密器;
  36. /// 创建加密流;
  37. /// </summary>
  38. /// <param name="text">
  39. /// 把输入参数转成字节数组;
  40. /// 遍历字节数组并写入加密流</param>
  41. /// <param name="path">
  42. /// 文件路径,默认当前工作路径</param>
  43. /// <param name="key">
  44. /// 随机秘钥</param>
  45. /// <param name="iv">
  46. /// 随机矢量</param>
  47. public static void EncryptTextToFile(string text, string path, byte[] key, byte[] iv)
  48. {
  49. try
  50. {
  51. using FileStream fStream = File.Open(path, FileMode.Create);
  52. using DES des = DES.Create();
  53. using ICryptoTransform encryptor = des.CreateEncryptor(key, iv);
  54. using var cStream = new CryptoStream(fStream, encryptor, CryptoStreamMode.Write);
  55. byte[] toEncrypt = Encoding.UTF8.GetBytes(text);
  56. cStream.Write(toEncrypt, 0, toEncrypt.Length);
  57. }
  58. catch (CryptographicException e)
  59. {
  60. Console.WriteLine("A Cryptographic error occurred: {0}", e.Message);
  61. throw;
  62. }
  63. }
  64. /// <summary>
  65. /// 自定义的解密方法:
  66. /// 创建文件流对象并打开加密后的文件;
  67. /// 创建DES解密器;
  68. /// 创建加密流。
  69. /// 创建文件流阅读器对象,并把加密流转UTF8文本:
  70. /// 从 StreamReader 读回所有文本;
  71. /// StreamReader 从 CryptoStream 接收解密的字节;
  72. /// CryptoStream 从 FileStream 接收加密的字节。
  73. /// </summary>
  74. /// <param name="path">
  75. /// 文件路径,默认当前工作路径</param>
  76. /// <param name="key">
  77. /// 随机秘钥</param>
  78. /// <param name="iv">
  79. /// 矢量</param>
  80. /// <returns></returns>
  81. public static string DecryptTextFromFile(string path, byte[] key, byte[] iv)
  82. {
  83. try
  84. {
  85. using FileStream fStream = File.OpenRead(path);
  86. using DES des = DES.Create();
  87. using ICryptoTransform decryptor = des.CreateDecryptor(key, iv);
  88. using var cStream = new CryptoStream(fStream, decryptor, CryptoStreamMode.Read);
  89. using StreamReader reader = new(cStream, Encoding.UTF8);
  90. return reader.ReadToEnd();
  91. }
  92. catch (CryptographicException e)
  93. {
  94. Console.WriteLine("A Cryptographic error occurred: {0}", e.Message);
  95. throw;
  96. }
  97. }
  98. }
  99. }
  100. //运行结果:
  101. /*
  102. 大海航行靠舵手
  103. */

3.实例2,加密和解密内存中的数据

  1. // DES.Create()
  2. // 创建和使用 DES 对象来加密和解密内存中的数据。
  3. using System.Security.Cryptography;
  4. using System.Text;
  5. namespace ConsoleApp9
  6. {
  7. class DESSample2
  8. {
  9. static void Main()
  10. {
  11. try
  12. {
  13. byte[] key;
  14. byte[] iv;
  15. using (DES des = DES.Create())
  16. {
  17. key = des.Key;
  18. iv = des.IV;
  19. }
  20. string original = "海燕在大海上飞翔";
  21. byte[] encrypted = EncryptToMemory(original, key, iv); // 加密字符串到缓存
  22. string decrypted = DecryptFromMemory(encrypted, key, iv); //自缓存解密到字符串
  23. Console.WriteLine(decrypted); //输出解密的字符串
  24. }
  25. catch (Exception e)
  26. {
  27. Console.WriteLine(e.Message);
  28. }
  29. }
  30. /// <summary>
  31. /// 自定义的加密方法:
  32. /// 从内存流中获取字节数组;
  33. /// 加密流读取内容并完成加密。
  34. /// </summary>
  35. public static byte[] EncryptToMemory(string text, byte[] key, byte[] iv)
  36. {
  37. try
  38. {
  39. using MemoryStream mStream = new(); //创建内存流
  40. using DES des = DES.Create(); //创建DES对象
  41. using ICryptoTransform encryptor = des.CreateEncryptor(key, iv);//创建加密器
  42. using var cStream = new CryptoStream(mStream, encryptor,
  43. CryptoStreamMode.Write); //创建加密流
  44. byte[] toEncrypt = Encoding.UTF8.GetBytes(text);//把输入字符串转字节数组
  45. cStream.Write(toEncrypt, 0, toEncrypt.Length); //将字节数组写入加密流并刷新
  46. byte[] ret = mStream.ToArray();
  47. return ret; //返回加密的内存数据
  48. }
  49. catch (CryptographicException e)
  50. {
  51. Console.WriteLine("A Cryptographic error occurred: {0}", e.Message);
  52. throw;
  53. }
  54. }
  55. /// <summary>
  56. /// 自缓存中解密:
  57. /// 创建缓存用于装载解密的数据,
  58. /// 注意:DES 加密的数据总是比解密的数据稍大;
  59. /// 用刚创建的缓存创建内存流;
  60. /// 不断地从 CryptoStream 中读取数据,直到完成(返回 0)。
  61. /// </summary>
  62. public static string DecryptFromMemory(byte[] encrypted, byte[] key, byte[] iv)
  63. {
  64. try
  65. {
  66. byte[] decrypted = new byte[encrypted.Length];
  67. int offset = 0;
  68. using DES des = DES.Create(); //创建DES对象
  69. using MemoryStream mStream = new(decrypted); //创建内存流
  70. using var cStream = new CryptoStream(mStream, des.
  71. CreateDecryptor(key, iv), CryptoStreamMode.Read); //创建解密流对象
  72. int read = 1;
  73. while (read > 0)
  74. {
  75. read = cStream.Read(decrypted, offset, decrypted.Length - offset);
  76. offset += read;
  77. }
  78. return Encoding.UTF8.GetString(decrypted, 0, offset); // 把解密的转文本并返回
  79. }
  80. catch (CryptographicException e)
  81. {
  82. Console.WriteLine("A Cryptographic error occurred: {0}", e.Message);
  83. throw;
  84. }
  85. }
  86. }
  87. }

三、Create(String)

        创建加密对象的实例以执行数据加密标准 (DES) 算法的指定实现。

  1. public static DES Create (string algName);
  2. 参数
  3. algName String
  4. 要使用的 DES 的特定实现的名称。
  5. 返回
  6. DES
  7. 一个加密对象。

四、关于DES加密

        DES,全称Data Encryption Standard,是一种对称加密算法。DES加密算法是以8字节为一个块进行加密,一个数据块一个数据块的加密,一个8字节的明文加密后的密文也是8字节。如果明文长度不为8字节的整数倍,添加值为0的字节凑满8字节整数倍。所以加密后的密文长度一定为8字节的整数倍。

        DES使用的密钥key为8字节,初始向量IV也是8字节。

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/article/detail/40347
推荐阅读
相关标签
  

闽ICP备14008679号