using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.Configuration;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Security.Cryptography;
using System.Text;
using System.Threading.Tasks;
namespace SolarPower.Models
{
///
/// Encryption Decryption Function - 加解密
///
public class EDFunction
{
const string SHA256_KEY = "REWOPRALOS"; //自訂金鑰
const string DES_KEY = "RALOSKEY"; //DES_KEY金鑰(8位字元)
const string DES_IV = "RALOS_IV"; //DES_IV初始化向量字串(8位字元)
const string AES_KEY = "RALOSKEY"; //AES_KEY金鑰
const string AES_IV = "RALOS_IV"; //AES_IV初始化向量字串
///
/// 單向加密SHA256
///
/// 加密字串
///
public string GetSHA256Encryption(string text)
{
string signRet = string.Empty;
byte[] key_byte = Encoding.Default.GetBytes(SHA256_KEY);
using (HMACSHA256 mac = new HMACSHA256(key_byte))
{
byte[] source = Encoding.Default.GetBytes(text);//將字串轉為Byte[]
byte[] crypto = mac.ComputeHash(source);//進行SHA256加密
signRet = Convert.ToBase64String(crypto);//把加密後的字串從Byte[]轉為字串
}
return signRet;
}
///
/// DES加密
///
/// 待加密資料
///
public string DESEncrypt(string text)
{
try
{
byte[] byKey = Encoding.ASCII.GetBytes(DES_KEY); //密鑰
byte[] byIV = Encoding.ASCII.GetBytes(DES_IV); //IV值
byte[] data = Encoding.Unicode.GetBytes(text);
DESCryptoServiceProvider cryptoProvider = new DESCryptoServiceProvider();//加密、解密對象
MemoryStream MStream = new MemoryStream();//內存流對象
//用內存流實例化加密流對象
CryptoStream CStream = new CryptoStream(MStream, cryptoProvider.CreateEncryptor(byKey, byIV), CryptoStreamMode.Write);
CStream.Write(data, 0, data.Length);//向加密流中寫入數據
CStream.FlushFinalBlock();//將數據壓入基礎流
byte[] temp = MStream.ToArray();//從內存流中獲取字節序列
CStream.Close();//關閉加密流
MStream.Close();//關閉內存流
return Convert.ToBase64String(temp);//返回加密後的字符串
}
catch
{
return text;
}
}
///
/// DES解密
///
/// 待解密字符串
///
public string DESDecrypt(string text)
{
try
{
byte[] byKey = Encoding.ASCII.GetBytes(DES_KEY); //DES密鑰
byte[] byIV = Encoding.ASCII.GetBytes(DES_IV); //IV值
byte[] data = Convert.FromBase64String(text);
DESCryptoServiceProvider descsp = new DESCryptoServiceProvider();
MemoryStream MStream = new MemoryStream();
//用內存流實例化解密流對象
CryptoStream CStream = new CryptoStream(MStream, descsp.CreateDecryptor(byKey, byIV), CryptoStreamMode.Write);
CStream.Write(data, 0, data.Length);//向加密流中寫入數據
CStream.FlushFinalBlock();//將數據壓入基礎流
byte[] temp = MStream.ToArray();//從內存流中獲取字節序列
CStream.Close();//關閉加密流
MStream.Close();//關閉內存流
return Encoding.Unicode.GetString(temp);//返回解密後的字符串
}
catch
{
return text;
}
}
///
/// AES加密
///
///
///
public string AESEncrypt(string text)
{
var encrypt = "";
try
{
AesCryptoServiceProvider aes = new AesCryptoServiceProvider();
MD5CryptoServiceProvider md5 = new MD5CryptoServiceProvider();
SHA256CryptoServiceProvider sha256 = new SHA256CryptoServiceProvider();
byte[] keyData = sha256.ComputeHash(Encoding.UTF8.GetBytes(AES_KEY));
byte[] ivData = md5.ComputeHash(Encoding.UTF8.GetBytes(AES_IV));
byte[] dataByteArray = Encoding.UTF8.GetBytes(text);
using (MemoryStream ms = new MemoryStream())
{
using (
CryptoStream cs = new CryptoStream(ms, aes.CreateEncryptor(keyData, ivData), CryptoStreamMode.Write)
)
{
cs.Write(dataByteArray, 0, dataByteArray.Length);
cs.FlushFinalBlock();
encrypt = Convert.ToBase64String(ms.ToArray());
}
}
return encrypt;
}
catch
{
return text;
}
}
///
/// AES解密
///
///
///
public string AESDecrypt(string text)
{
var decrypt = "";
try
{
SymmetricAlgorithm aes = new AesCryptoServiceProvider();
MD5CryptoServiceProvider md5 = new MD5CryptoServiceProvider();
SHA256CryptoServiceProvider sha256 = new SHA256CryptoServiceProvider();
byte[] keyData = sha256.ComputeHash(Encoding.UTF8.GetBytes(AES_KEY));
byte[] ivData = md5.ComputeHash(Encoding.UTF8.GetBytes(AES_IV));
byte[] dataByteArray = Convert.FromBase64String(text);
using (MemoryStream ms = new MemoryStream())
{
using (
CryptoStream cs = new CryptoStream(ms, aes.CreateDecryptor(keyData, ivData), CryptoStreamMode.Write)
)
{
cs.Write(dataByteArray, 0, dataByteArray.Length);
cs.FlushFinalBlock();
decrypt = Encoding.UTF8.GetString(ms.ToArray());
}
}
return decrypt;
}
catch
{
return text;
}
}
}
///
/// 回傳結果
///
/// 資料型別
public class ApiResult
{
public string Code { get; set; }
public string Msg { get; set; }
public S1 Data { get; set; }
}
public class DapperConnection
{
///
/// 新增SQL字串
///
/// Table名稱
/// Add新增欄位名稱
///
//public string InsertQueryTxt(string TableName, List TableValue)
//{
// var sqlId = "";
// var sqlValue = "";
// foreach (var value in TableValue)
// {
// sqlId += value + ",";
// sqlValue += "@" + value + ",";
// }
// var connTxt = "INSERT INTO " + TableName + "(" + sqlId.Substring(0, sqlId.Length - 1) + ") VALUES" + "(" + sqlValue.Substring(0, sqlValue.Length - 1) + ");";
// return connTxt;
//}
///
/// 修改SQL字串
///
/// Table名稱
/// Update變動欄位名稱
/// where條件
///
//public string UpdateQueryTxt(string TableName, List TableValue, string sWhere = "")
//{
// var sqlTxt = "";
// foreach (var value in TableValue)
// {
// sqlTxt += value + "=@" + value + ",";
// }
// var connTxt = "UPDATE " + TableName + " SET " + sqlTxt.Substring(0, sqlTxt.Length - 1);
// if (sWhere != "")
// {
// connTxt += " WHERE " + sWhere;
// }
// return connTxt;
//}
//private string GenerateInsertQuery()
//{
// var insertQuery = new StringBuilder($"INSERT INTO {_tableName} ");
// insertQuery.Append("(");
// var properties = GenerateListOfProperties(GetProperties);
// properties.ForEach(prop => { insertQuery.Append($"[{prop}],"); });
// insertQuery
// .Remove(insertQuery.Length - 1, 1)
// .Append(") VALUES (");
// properties.ForEach(prop => { insertQuery.Append($"@{prop},"); });
// insertQuery
// .Remove(insertQuery.Length - 1, 1)
// .Append(")");
// return insertQuery.ToString();
//}
//private string GenerateUpdateQuery()
//{
// var updateQuery = new StringBuilder($"UPDATE {_tableName} SET ");
// var properties = GenerateListOfProperties(GetProperties);
// properties.ForEach(property =>
// {
// if (!property.Equals("Id"))
// {
// updateQuery.Append($"{property}=@{property},");
// }
// });
// updateQuery.Remove(updateQuery.Length - 1, 1); //remove last comma
// updateQuery.Append(" WHERE Id=@Id");
// return updateQuery.ToString();
//}
}
}