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.9 KiB

  1. //-----------------------------------------------------------------------
  2. // <copyright file="TripleDES.cs" company="Origtek">
  3. // TripleDES 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. /// 3DES Encrypt Decrypt
  13. /// </summary>
  14. public partial class TripleDES : 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 != 16) && (byaKey.Length != 24)))
  39. {
  40. throw new ArgumentException("Key == null || ((Key.Length != 16) && (Key.Length != 24))");
  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 (TripleDESCryptoServiceProvider 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. swEncrypt.Write(plainText);
  63. }
  64. encrypted = msEncrypt.ToArray();
  65. }
  66. }
  67. }
  68. //// Return the encrypted bytes from the memory stream.
  69. return encrypted;
  70. }
  71. /// <summary>
  72. /// Main decrypt function
  73. /// </summary>
  74. /// <param name="cipherText">
  75. /// Plain text to be decrypt
  76. /// </param>
  77. /// <param name="byaKey">
  78. /// decrypt key
  79. /// </param>
  80. /// <param name="byaIV">
  81. /// decrypt initial vector
  82. /// </param>
  83. /// <returns>
  84. /// decrypt result
  85. /// </returns>
  86. public override string DecryptByteToString(byte[] cipherText, byte[] byaKey, byte[] byaIV)
  87. {
  88. // Check arguments.
  89. if (cipherText == null || cipherText.Length <= 0)
  90. {
  91. throw new ArgumentNullException("cipherText");
  92. }
  93. if (byaKey == null || ((byaKey.Length != 16) && (byaKey.Length != 24)))
  94. {
  95. throw new ArgumentException("Key == null || ((Key.Length != 16) && (Key.Length != 24))");
  96. }
  97. if (byaIV == null || byaIV.Length != 8)
  98. {
  99. throw new ArgumentException("IV == null || IV.Length != 8");
  100. }
  101. //// Declare the string used to hold
  102. //// the decrypted text.
  103. string plaintext = null;
  104. //// Create an Aes object
  105. //// with the specified key and IV.
  106. using (TripleDESCryptoServiceProvider desAlg = new())
  107. {
  108. desAlg.Key = byaKey;
  109. desAlg.IV = byaIV;
  110. //// Create a decrytor to perform the stream transform.
  111. ICryptoTransform decryptor = desAlg.CreateDecryptor(desAlg.Key, desAlg.IV);
  112. // Create the streams used for decryption.
  113. using (MemoryStream msDecrypt = new(cipherText))
  114. {
  115. using (CryptoStream csDecrypt = new(msDecrypt, decryptor, CryptoStreamMode.Read))
  116. {
  117. using (StreamReader srDecrypt = new(csDecrypt))
  118. {
  119. // Read the decrypted bytes from the decrypting stream
  120. // and place them in a string.
  121. plaintext = srDecrypt.ReadToEnd();
  122. }
  123. }
  124. }
  125. }
  126. return plaintext;
  127. }
  128. }
  129. }