You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

147 lines
4.8 KiB

  1. //-----------------------------------------------------------------------
  2. // <copyright file="DES.cs" company="Origtek">
  3. // DES belongs to Copyright (c) Origtek. All rights reserved.
  4. // </copyright>
  5. //-----------------------------------------------------------------------
  6. namespace OT.COM.Encryption
  7. {
  8. using System;
  9. using System.IO;
  10. using System.Security.Cryptography;
  11. /// <summary>
  12. /// DES Encrypt Decrypt
  13. /// </summary>
  14. public partial class DES : AEncryption
  15. {
  16. /// <summary>
  17. /// Main encrypt function
  18. /// </summary>
  19. /// <param name="plainText">
  20. /// Plain text to be encrypt
  21. /// </param>
  22. /// <param name="byaKey">
  23. /// Encrypt key
  24. /// </param>
  25. /// <param name="byaIV">
  26. /// Encrypt initial vector
  27. /// </param>
  28. /// <returns>
  29. /// Encrypt result byte
  30. /// </returns>
  31. public override byte[] EncryptStringToByte(string plainText, byte[] byaKey, byte[] byaIV)
  32. {
  33. //// Check arguments.
  34. if (plainText == null || plainText.Length <= 0)
  35. {
  36. throw new ArgumentNullException("plainText");
  37. }
  38. if (byaKey == null || byaKey.Length != 8)
  39. {
  40. throw new ArgumentException("Key == null || Key.Length != 8");
  41. }
  42. if (byaIV == null || byaIV.Length != 8)
  43. {
  44. throw new ArgumentException("IV == null || IV.Length != 8");
  45. }
  46. byte[] encrypted;
  47. //// Create an Aes object
  48. //// with the specified key and IV.
  49. using (DESCryptoServiceProvider desAlg = new())
  50. {
  51. desAlg.Key = byaKey;
  52. desAlg.IV = byaIV;
  53. //// Create a decrytor to perform the stream transform.
  54. ICryptoTransform encryptor = desAlg.CreateEncryptor(desAlg.Key, desAlg.IV);
  55. //// Create the streams used for encryption.
  56. using (MemoryStream msEncrypt = new())
  57. {
  58. using (CryptoStream csEncrypt = new(msEncrypt, encryptor, CryptoStreamMode.Write))
  59. {
  60. using (StreamWriter swEncrypt = new(csEncrypt))
  61. {
  62. //// Write all data to the stream.
  63. swEncrypt.Write(plainText);
  64. }
  65. encrypted = msEncrypt.ToArray();
  66. }
  67. }
  68. }
  69. //// Return the encrypted bytes from the memory stream.
  70. return encrypted;
  71. }
  72. /// <summary>
  73. /// Main decrypt function
  74. /// </summary>
  75. /// <param name="cipherText">
  76. /// Plain text to be decrypt
  77. /// </param>
  78. /// <param name="byaKey">
  79. /// decrypt key
  80. /// </param>
  81. /// <param name="byaIV">
  82. /// decrypt initial vector
  83. /// </param>
  84. /// <returns>
  85. /// decrypt result
  86. /// </returns>
  87. public override string DecryptByteToString(byte[] cipherText, byte[] byaKey, byte[] byaIV)
  88. {
  89. //// Check arguments.
  90. if (cipherText == null || cipherText.Length <= 0)
  91. {
  92. throw new ArgumentNullException("cipherText");
  93. }
  94. if (byaKey == null || byaKey.Length != 8)
  95. {
  96. throw new ArgumentException("Key == null || Key.Length != 8");
  97. }
  98. if (byaIV == null || byaIV.Length != 8)
  99. {
  100. throw new ArgumentException("IV == null || IV.Length != 8");
  101. }
  102. //// Declare the string used to hold
  103. //// the decrypted text.
  104. string plaintext = null;
  105. //// Create an Aes object
  106. //// with the specified key and IV.
  107. using (DESCryptoServiceProvider desAlg = new())
  108. {
  109. desAlg.Key = byaKey;
  110. desAlg.IV = byaIV;
  111. //// Create a decrytor to perform the stream transform.
  112. ICryptoTransform decryptor = desAlg.CreateDecryptor(desAlg.Key, desAlg.IV);
  113. //// Create the streams used for decryption.
  114. using (MemoryStream msDecrypt = new(cipherText))
  115. {
  116. using (CryptoStream csDecrypt = new(msDecrypt, decryptor, CryptoStreamMode.Read))
  117. {
  118. using (StreamReader srDecrypt = new(csDecrypt))
  119. {
  120. //// Read the decrypted bytes from the decrypting stream
  121. //// and place them in a string.
  122. plaintext = srDecrypt.ReadToEnd();
  123. }
  124. }
  125. }
  126. }
  127. return plaintext;
  128. }
  129. }
  130. }