//----------------------------------------------------------------------- // // AEncryption belongs to Copyright (c) Origtek. All rights reserved. // //----------------------------------------------------------------------- namespace OT.COM.Encryption { using OT.COM.LogisticsUtil; using System; /// /// Handle AEncryption Unit test. /// public abstract partial class AEncryption { /// /// String encryption /// /// /// Data to encrypt. Key/IV are byte [] /// /// /// Encrypt result /// public ResultInfo EncryptString(DataInfoByte i_di) { ResultInfo ri = new(); try { ri.Result = System.Text.Encoding.Unicode.GetString( this.EncryptStringToByte( i_di.Text, i_di.Key, i_di.IV)); ri.Success = true; } catch (Exception ex) { ri.Message = Util.GetLastExceptionMsg(ex); } return ri; } /// /// String encryption /// /// /// Data to encrypt. Key/IV are string /// /// /// Encrypt result /// public ResultInfo EncryptString(DataInfo i_di) { return this.EncryptString(new DataInfoByte() { Text = i_di.Text, Key = System.Text.Encoding.Unicode.GetBytes(i_di.Key), IV = System.Text.Encoding.Unicode.GetBytes(i_di.IV) }); } /// /// String decryption /// /// /// Data to decrypt. Key/IV are byte [] /// /// /// Decrypt result /// public ResultInfo DecryptString(DataInfoByte i_di) { ResultInfo ri = new(); try { ri.Result = this.DecryptByteToString( System.Text.Encoding.Unicode.GetBytes(i_di.Text), i_di.Key, i_di.IV); ri.Success = true; } catch (Exception ex) { ri.Message = Util.GetLastExceptionMsg(ex); } return ri; } /// /// String decryption /// /// /// Data to decrypt. Key/IV are string /// /// /// Decrypt result /// public ResultInfo DecryptString(DataInfo i_di) { return this.DecryptString(new DataInfoByte() { Text = i_di.Text, Key = System.Text.Encoding.Unicode.GetBytes(i_di.Key), IV = System.Text.Encoding.Unicode.GetBytes(i_di.IV) }); } /// /// Main encrypt function /// /// /// Plain text to be encrypt /// /// /// Encrypt key /// /// /// Encrypt initial vector /// /// /// Encrypt result byte /// public abstract byte[] EncryptStringToByte(string plainText, byte[] byaKey, byte[] byaIV); /// /// Main decrypt function /// /// /// Plain text to be decrypt /// /// /// decrypt key /// /// /// decrypt initial vector /// /// /// decrypt result /// public abstract string DecryptByteToString(byte[] cipherText, byte[] byaKey, byte[] byaIV); /// /// Data structure to process /// public class DataInfo { /// /// Initializes a new instance of the class. /// public DataInfo() { this.Text = null; this.Key = null; this.IV = null; } /// /// Gets or sets data string /// public string Text { get; set; } /// /// Gets or sets Encrypt/Decrypt key /// public string Key { get; set; } /// /// Gets or sets Encrypt/Decrypt initial vector /// public string IV { get; set; } } /// /// Data structure to process /// public class DataInfoByte { /// /// Initializes a new instance of the class. /// public DataInfoByte() { this.Text = null; this.Key = null; this.IV = null; } /// /// Gets or sets data string /// public string Text { get; set; } /// /// Gets or sets Encrypt/Decrypt key /// public byte[] Key { get; set; } /// /// Gets or sets Encrypt/Decrypt initial vector /// public byte[] IV { get; set; } } /// /// Data structure of encrypt/decrypt result /// public class ResultInfo { /// /// Initializes a new instance of the class. /// public ResultInfo() { this.Success = false; this.Message = null; this.Result = null; } /// /// Gets or sets a value indicating whether success or not /// public bool Success { get; set; } /// /// Gets or sets Encrypt/Decrypt result message /// public string Message { get; set; } /// /// Gets or sets Encrypt/Decrypt result data /// public string Result { get; set; } } } }