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.

151 lines
5.3 KiB

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