90 lines
2.6 KiB
90 lines
2.6 KiB
|
|
namespace CounsellorBL.Helper
|
|
{
|
|
using OT.COM.Encryption;
|
|
using OT.COM.LogisticsUtil;
|
|
using System;
|
|
|
|
public static class EncryptHelper
|
|
{
|
|
private static readonly byte[] _Key = System.Text.Encoding.Unicode.GetBytes(Util.GetSettingString("KEY"));
|
|
private static readonly byte[] _IV = System.Text.Encoding.Unicode.GetBytes(Util.GetSettingString("IV"));
|
|
private static readonly AES _aes = new AES();
|
|
/// <summary>
|
|
/// 函式名稱:Encrypt
|
|
/// 函式說明:加密API
|
|
/// 起始作者:Hercules
|
|
/// 起始日期:2016/06/17
|
|
/// 最新修改人: Hercules
|
|
/// 最新修日期: 2016/06/17
|
|
/// </summary>
|
|
/// <param name="i_sPlaintext">明碼</param>
|
|
/// <param name="o_sCiphertext">暗碼</param>
|
|
/// <returns>錯誤訊息,若成功則為Null</returns>
|
|
public static string Encrypt(string i_sPlaintext, out string o_sCiphertext)
|
|
{
|
|
string sMsg = null;
|
|
string sRes = null;
|
|
|
|
|
|
try
|
|
{
|
|
do
|
|
{
|
|
byte[] baRes = _aes.EncryptStringToByte(
|
|
i_sPlaintext,
|
|
_Key,
|
|
_IV);
|
|
sRes = Convert.ToBase64String(baRes);
|
|
}
|
|
while (false);
|
|
|
|
}
|
|
finally
|
|
{
|
|
o_sCiphertext = sRes;
|
|
}
|
|
|
|
return sMsg;
|
|
}
|
|
|
|
/// <summary>
|
|
/// 函式名稱:Decrypt
|
|
/// 函式說明:解密API
|
|
/// 起始作者:Hercules
|
|
/// 起始日期:2016/06/17
|
|
/// 最新修改人: Hercules
|
|
/// 最新修日期: 2016/06/17
|
|
/// </summary>
|
|
/// <param name="i_sCiphertext">暗碼</param>
|
|
/// <param name="o_sPlaintext">明碼</param>
|
|
/// <returns>錯誤訊息,若成功則為Null</returns>
|
|
public static string Decrypt(string i_sCiphertext, out string o_sPlaintext)
|
|
{
|
|
string sMsg = null;
|
|
string sRes = null;
|
|
|
|
try
|
|
{
|
|
do
|
|
{
|
|
byte[] baCiphertext = Convert.FromBase64String(i_sCiphertext);
|
|
sRes = _aes.DecryptByteToString(
|
|
baCiphertext,
|
|
_Key,
|
|
_IV);
|
|
}
|
|
while (false);
|
|
|
|
}
|
|
finally
|
|
{
|
|
o_sPlaintext = sRes;
|
|
}
|
|
|
|
return sMsg;
|
|
}
|
|
|
|
}
|
|
|
|
}
|