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.

243 lines
6.8 KiB

  1. //-----------------------------------------------------------------------
  2. // <copyright file="AEncryption.cs" company="Origtek">
  3. // AEncryption belongs to Copyright (c) Origtek. All rights reserved.
  4. // </copyright>
  5. //-----------------------------------------------------------------------
  6. namespace OT.COM.Encryption
  7. {
  8. using OT.COM.LogisticsUtil;
  9. using System;
  10. /// <summary>
  11. /// Handle AEncryption Unit test.
  12. /// </summary>
  13. public abstract partial class AEncryption
  14. {
  15. /// <summary>
  16. /// String encryption
  17. /// </summary>
  18. /// <param name="i_di">
  19. /// Data to encrypt. Key/IV are byte []
  20. /// </param>
  21. /// <returns>
  22. /// Encrypt result
  23. /// </returns>
  24. public ResultInfo EncryptString(DataInfoByte i_di)
  25. {
  26. ResultInfo ri = new();
  27. try
  28. {
  29. ri.Result = System.Text.Encoding.Unicode.GetString(
  30. this.EncryptStringToByte(
  31. i_di.Text,
  32. i_di.Key,
  33. i_di.IV));
  34. ri.Success = true;
  35. }
  36. catch (Exception ex)
  37. {
  38. ri.Message = Util.GetLastExceptionMsg(ex);
  39. }
  40. return ri;
  41. }
  42. /// <summary>
  43. /// String encryption
  44. /// </summary>
  45. /// <param name="i_di">
  46. /// Data to encrypt. Key/IV are string
  47. /// </param>
  48. /// <returns>
  49. /// Encrypt result
  50. /// </returns>
  51. public ResultInfo EncryptString(DataInfo i_di)
  52. {
  53. return this.EncryptString(new DataInfoByte()
  54. {
  55. Text = i_di.Text,
  56. Key = System.Text.Encoding.Unicode.GetBytes(i_di.Key),
  57. IV = System.Text.Encoding.Unicode.GetBytes(i_di.IV)
  58. });
  59. }
  60. /// <summary>
  61. /// String decryption
  62. /// </summary>
  63. /// <param name="i_di">
  64. /// Data to decrypt. Key/IV are byte []
  65. /// </param>
  66. /// <returns>
  67. /// Decrypt result
  68. /// </returns>
  69. public ResultInfo DecryptString(DataInfoByte i_di)
  70. {
  71. ResultInfo ri = new();
  72. try
  73. {
  74. ri.Result = this.DecryptByteToString(
  75. System.Text.Encoding.Unicode.GetBytes(i_di.Text),
  76. i_di.Key,
  77. i_di.IV);
  78. ri.Success = true;
  79. }
  80. catch (Exception ex)
  81. {
  82. ri.Message = Util.GetLastExceptionMsg(ex);
  83. }
  84. return ri;
  85. }
  86. /// <summary>
  87. /// String decryption
  88. /// </summary>
  89. /// <param name="i_di">
  90. /// Data to decrypt. Key/IV are string
  91. /// </param>
  92. /// <returns>
  93. /// Decrypt result
  94. /// </returns>
  95. public ResultInfo DecryptString(DataInfo i_di)
  96. {
  97. return this.DecryptString(new DataInfoByte()
  98. {
  99. Text = i_di.Text,
  100. Key = System.Text.Encoding.Unicode.GetBytes(i_di.Key),
  101. IV = System.Text.Encoding.Unicode.GetBytes(i_di.IV)
  102. });
  103. }
  104. /// <summary>
  105. /// Main encrypt function
  106. /// </summary>
  107. /// <param name="plainText">
  108. /// Plain text to be encrypt
  109. /// </param>
  110. /// <param name="byaKey">
  111. /// Encrypt key
  112. /// </param>
  113. /// <param name="byaIV">
  114. /// Encrypt initial vector
  115. /// </param>
  116. /// <returns>
  117. /// Encrypt result byte
  118. /// </returns>
  119. public abstract byte[] EncryptStringToByte(string plainText, byte[] byaKey, byte[] byaIV);
  120. /// <summary>
  121. /// Main decrypt function
  122. /// </summary>
  123. /// <param name="cipherText">
  124. /// Plain text to be decrypt
  125. /// </param>
  126. /// <param name="byaKey">
  127. /// decrypt key
  128. /// </param>
  129. /// <param name="byaIV">
  130. /// decrypt initial vector
  131. /// </param>
  132. /// <returns>
  133. /// decrypt result
  134. /// </returns>
  135. public abstract string DecryptByteToString(byte[] cipherText, byte[] byaKey, byte[] byaIV);
  136. /// <summary>
  137. /// Data structure to process
  138. /// </summary>
  139. public class DataInfo
  140. {
  141. /// <summary>
  142. /// Initializes a new instance of the <see cref="DataInfo" /> class.
  143. /// </summary>
  144. public DataInfo()
  145. {
  146. this.Text = null;
  147. this.Key = null;
  148. this.IV = null;
  149. }
  150. /// <summary>
  151. /// Gets or sets data string
  152. /// </summary>
  153. public string Text { get; set; }
  154. /// <summary>
  155. /// Gets or sets Encrypt/Decrypt key
  156. /// </summary>
  157. public string Key { get; set; }
  158. /// <summary>
  159. /// Gets or sets Encrypt/Decrypt initial vector
  160. /// </summary>
  161. public string IV { get; set; }
  162. }
  163. /// <summary>
  164. /// Data structure to process
  165. /// </summary>
  166. public class DataInfoByte
  167. {
  168. /// <summary>
  169. /// Initializes a new instance of the <see cref="DataInfoByte" /> class.
  170. /// </summary>
  171. public DataInfoByte()
  172. {
  173. this.Text = null;
  174. this.Key = null;
  175. this.IV = null;
  176. }
  177. /// <summary>
  178. /// Gets or sets data string
  179. /// </summary>
  180. public string Text { get; set; }
  181. /// <summary>
  182. /// Gets or sets Encrypt/Decrypt key
  183. /// </summary>
  184. public byte[] Key { get; set; }
  185. /// <summary>
  186. /// Gets or sets Encrypt/Decrypt initial vector
  187. /// </summary>
  188. public byte[] IV { get; set; }
  189. }
  190. /// <summary>
  191. /// Data structure of encrypt/decrypt result
  192. /// </summary>
  193. public class ResultInfo
  194. {
  195. /// <summary>
  196. /// Initializes a new instance of the <see cref="ResultInfo" /> class.
  197. /// </summary>
  198. public ResultInfo()
  199. {
  200. this.Success = false;
  201. this.Message = null;
  202. this.Result = null;
  203. }
  204. /// <summary>
  205. /// Gets or sets a value indicating whether success or not
  206. /// </summary>
  207. public bool Success { get; set; }
  208. /// <summary>
  209. /// Gets or sets Encrypt/Decrypt result message
  210. /// </summary>
  211. public string Message { get; set; }
  212. /// <summary>
  213. /// Gets or sets Encrypt/Decrypt result data
  214. /// </summary>
  215. public string Result { get; set; }
  216. }
  217. }
  218. }