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
243 lines
6.8 KiB
//-----------------------------------------------------------------------
|
|
// <copyright file="AEncryption.cs" company="Origtek">
|
|
// AEncryption belongs to Copyright (c) Origtek. All rights reserved.
|
|
// </copyright>
|
|
//-----------------------------------------------------------------------
|
|
|
|
namespace OT.COM.Encryption
|
|
{
|
|
using OT.COM.LogisticsUtil;
|
|
using System;
|
|
|
|
/// <summary>
|
|
/// Handle AEncryption Unit test.
|
|
/// </summary>
|
|
public abstract partial class AEncryption
|
|
{
|
|
|
|
/// <summary>
|
|
/// String encryption
|
|
/// </summary>
|
|
/// <param name="i_di">
|
|
/// Data to encrypt. Key/IV are byte []
|
|
/// </param>
|
|
/// <returns>
|
|
/// Encrypt result
|
|
/// </returns>
|
|
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;
|
|
}
|
|
|
|
/// <summary>
|
|
/// String encryption
|
|
/// </summary>
|
|
/// <param name="i_di">
|
|
/// Data to encrypt. Key/IV are string
|
|
/// </param>
|
|
/// <returns>
|
|
/// Encrypt result
|
|
/// </returns>
|
|
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)
|
|
});
|
|
}
|
|
|
|
/// <summary>
|
|
/// String decryption
|
|
/// </summary>
|
|
/// <param name="i_di">
|
|
/// Data to decrypt. Key/IV are byte []
|
|
/// </param>
|
|
/// <returns>
|
|
/// Decrypt result
|
|
/// </returns>
|
|
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;
|
|
}
|
|
|
|
/// <summary>
|
|
/// String decryption
|
|
/// </summary>
|
|
/// <param name="i_di">
|
|
/// Data to decrypt. Key/IV are string
|
|
/// </param>
|
|
/// <returns>
|
|
/// Decrypt result
|
|
/// </returns>
|
|
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)
|
|
});
|
|
}
|
|
|
|
/// <summary>
|
|
/// Main encrypt function
|
|
/// </summary>
|
|
/// <param name="plainText">
|
|
/// Plain text to be encrypt
|
|
/// </param>
|
|
/// <param name="byaKey">
|
|
/// Encrypt key
|
|
/// </param>
|
|
/// <param name="byaIV">
|
|
/// Encrypt initial vector
|
|
/// </param>
|
|
/// <returns>
|
|
/// Encrypt result byte
|
|
/// </returns>
|
|
public abstract byte[] EncryptStringToByte(string plainText, byte[] byaKey, byte[] byaIV);
|
|
|
|
/// <summary>
|
|
/// Main decrypt function
|
|
/// </summary>
|
|
/// <param name="cipherText">
|
|
/// Plain text to be decrypt
|
|
/// </param>
|
|
/// <param name="byaKey">
|
|
/// decrypt key
|
|
/// </param>
|
|
/// <param name="byaIV">
|
|
/// decrypt initial vector
|
|
/// </param>
|
|
/// <returns>
|
|
/// decrypt result
|
|
/// </returns>
|
|
public abstract string DecryptByteToString(byte[] cipherText, byte[] byaKey, byte[] byaIV);
|
|
|
|
/// <summary>
|
|
/// Data structure to process
|
|
/// </summary>
|
|
public class DataInfo
|
|
{
|
|
/// <summary>
|
|
/// Initializes a new instance of the <see cref="DataInfo" /> class.
|
|
/// </summary>
|
|
public DataInfo()
|
|
{
|
|
this.Text = null;
|
|
this.Key = null;
|
|
this.IV = null;
|
|
}
|
|
|
|
/// <summary>
|
|
/// Gets or sets data string
|
|
/// </summary>
|
|
public string Text { get; set; }
|
|
|
|
/// <summary>
|
|
/// Gets or sets Encrypt/Decrypt key
|
|
/// </summary>
|
|
public string Key { get; set; }
|
|
|
|
/// <summary>
|
|
/// Gets or sets Encrypt/Decrypt initial vector
|
|
/// </summary>
|
|
public string IV { get; set; }
|
|
}
|
|
|
|
/// <summary>
|
|
/// Data structure to process
|
|
/// </summary>
|
|
public class DataInfoByte
|
|
{
|
|
/// <summary>
|
|
/// Initializes a new instance of the <see cref="DataInfoByte" /> class.
|
|
/// </summary>
|
|
public DataInfoByte()
|
|
{
|
|
this.Text = null;
|
|
this.Key = null;
|
|
this.IV = null;
|
|
}
|
|
|
|
/// <summary>
|
|
/// Gets or sets data string
|
|
/// </summary>
|
|
public string Text { get; set; }
|
|
|
|
/// <summary>
|
|
/// Gets or sets Encrypt/Decrypt key
|
|
/// </summary>
|
|
public byte[] Key { get; set; }
|
|
|
|
/// <summary>
|
|
/// Gets or sets Encrypt/Decrypt initial vector
|
|
/// </summary>
|
|
public byte[] IV { get; set; }
|
|
}
|
|
|
|
/// <summary>
|
|
/// Data structure of encrypt/decrypt result
|
|
/// </summary>
|
|
public class ResultInfo
|
|
{
|
|
/// <summary>
|
|
/// Initializes a new instance of the <see cref="ResultInfo" /> class.
|
|
/// </summary>
|
|
public ResultInfo()
|
|
{
|
|
this.Success = false;
|
|
this.Message = null;
|
|
this.Result = null;
|
|
}
|
|
|
|
/// <summary>
|
|
/// Gets or sets a value indicating whether success or not
|
|
/// </summary>
|
|
public bool Success { get; set; }
|
|
|
|
/// <summary>
|
|
/// Gets or sets Encrypt/Decrypt result message
|
|
/// </summary>
|
|
public string Message { get; set; }
|
|
|
|
/// <summary>
|
|
/// Gets or sets Encrypt/Decrypt result data
|
|
/// </summary>
|
|
public string Result { get; set; }
|
|
}
|
|
}
|
|
|
|
}
|