255 lines
10 KiB
C#
255 lines
10 KiB
C#
using System;
|
|
using System.Collections.Generic;
|
|
using System.IO;
|
|
using System.Linq;
|
|
using System.Security.Cryptography;
|
|
using System.Text;
|
|
using System.Threading.Tasks;
|
|
|
|
namespace AllBase
|
|
{
|
|
public class BaseEncrypt
|
|
{
|
|
#region EBFB8THEA 加密
|
|
public static string _KEY = "XNNRSAA1"; //密钥
|
|
public static string _IV = "RSABPDD2"; //向量
|
|
|
|
/// <summary>
|
|
/// 加密
|
|
/// </summary>
|
|
/// <param name="data">需要加密的文本</param>
|
|
/// <returns></returns>
|
|
public static string EncryptEncode(string data, string _key = "", string _iv = "")
|
|
{
|
|
if (string.IsNullOrEmpty(_key))
|
|
_key = _KEY;
|
|
if (string.IsNullOrEmpty(_iv))
|
|
_iv = _IV;
|
|
|
|
byte[] byKey = System.Text.ASCIIEncoding.ASCII.GetBytes(_key);
|
|
byte[] byIV = System.Text.ASCIIEncoding.ASCII.GetBytes(_iv);
|
|
|
|
DESCryptoServiceProvider cryptoProvider = new DESCryptoServiceProvider();
|
|
int i = cryptoProvider.KeySize;
|
|
MemoryStream ms = new MemoryStream();
|
|
CryptoStream cst = new CryptoStream(ms, cryptoProvider.CreateEncryptor(byKey, byIV), CryptoStreamMode.Write);
|
|
|
|
StreamWriter sw = new StreamWriter(cst);
|
|
sw.Write(data);
|
|
sw.Flush();
|
|
cst.FlushFinalBlock();
|
|
sw.Flush();
|
|
string strRet = Convert.ToBase64String(ms.GetBuffer(), 0, (int)ms.Length);
|
|
|
|
sw.Dispose();
|
|
sw.Close();
|
|
cst.Dispose();
|
|
cst.Close();
|
|
ms.Dispose();
|
|
ms.Close();
|
|
return strRet;
|
|
}
|
|
|
|
/// <summary>
|
|
/// 解密
|
|
/// </summary>
|
|
/// <param name="data"></param>
|
|
/// <returns></returns>
|
|
public static string EncryptDecode(string data, string _key = "", string _iv = "")
|
|
{
|
|
try
|
|
{
|
|
if (string.IsNullOrEmpty(_key))
|
|
_key = _KEY;
|
|
if (string.IsNullOrEmpty(_iv))
|
|
_iv = _IV;
|
|
|
|
byte[] byKey = System.Text.ASCIIEncoding.ASCII.GetBytes(_key);
|
|
byte[] byIV = System.Text.ASCIIEncoding.ASCII.GetBytes(_iv);
|
|
byte[] byEnc;
|
|
|
|
try
|
|
{
|
|
data.Replace("_%_", "/");
|
|
data.Replace("-%-", "#");
|
|
byEnc = Convert.FromBase64String(data);
|
|
}
|
|
catch
|
|
{
|
|
return "";
|
|
}
|
|
|
|
DESCryptoServiceProvider cryptoProvider = new DESCryptoServiceProvider();
|
|
MemoryStream ms = new MemoryStream(byEnc);
|
|
CryptoStream cst = new CryptoStream(ms, cryptoProvider.CreateDecryptor(byKey, byIV), CryptoStreamMode.Read);
|
|
StreamReader sr = new StreamReader(cst);
|
|
var result = sr.ReadToEnd();
|
|
|
|
sr.Dispose();
|
|
sr.Close();
|
|
cst.Dispose();
|
|
cst.Close();
|
|
ms.Dispose();
|
|
ms.Close();
|
|
|
|
return result;
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
return "";
|
|
}
|
|
}
|
|
#endregion
|
|
|
|
#region 数字签名校验
|
|
public static string sPrivateKey = "<RSAKeyValue><Modulus>oOS7iOYBmFdhSPYmae8Vav6DNvzFqEp08UWzNr6Jq3WetYNrVriB7EGcmSWpK4b+vu+NPhFuXRt4WMo06kJLll7kDeVBYJooXCQQaNQT1Gv4l2zcWgutCBtzIAs8YGwUgjkFdnIN8bIGOdGbQAaxIJpnifCzX5Iw2qd2YaBQMjM=</Modulus><Exponent>AQAB</Exponent><P>3w7I1TzMMt7d9fYkQINQamyqVT+Ke/WU8Z+RMFpfiYmzu+EorIxeRe5yEG1TMYD2uHPX7K8K9IwxoTF2SxcXmQ==</P><Q>uKevEc6B4LTwebVcE8UYk4ZjRW2ADDe223ajc9yZKguboXTrv1euPEnEoWibKANZ7a6ktW+ok7M30T7+6hhHqw==</Q><DP>a0JWvIMhxgx5Oz8DK6p5TtO2DIwjcXwkBRQv4spySlbXAK9CucY9xu7kFpYbfSCAgy3hVMTPRczsSJDaSz1tcQ==</DP><DQ>Oc1r1RQivdeGC2NFLn8dIVkJDDupwKlUGUD7C5K7c1js38682Um2JfMllS0snIroBKoNryoDyDBqwZqJq3fsPw==</DQ><InverseQ>nOsce/v2jCJKY1/SeOsLiwa7n5fLIKEdIBBeLURLYuELI/9RyElBM5KwNUCXWylCkVc5DRGyoExOWzZgdUk5Kw==</InverseQ><D>H2lov6qAJzjwEmmUSFxYJyJ3hbXF9TxMJorAGXELucDl8nQs8apmGzUYGMJEi+34MFjej36huGXTkiowOIgm724PlVgjQGyr9hqBFO9WTKwXQrgZaoG235HBDGh5oq1fkBNZ415msYsqwmtif7a6qAL9tEJiPlBWpg5/EJA9wVE=</D></RSAKeyValue>";
|
|
public static string sPublicKey = "<RSAKeyValue><Modulus>oOS7iOYBmFdhSPYmae8Vav6DNvzFqEp08UWzNr6Jq3WetYNrVriB7EGcmSWpK4b+vu+NPhFuXRt4WMo06kJLll7kDeVBYJooXCQQaNQT1Gv4l2zcWgutCBtzIAs8YGwUgjkFdnIN8bIGOdGbQAaxIJpnifCzX5Iw2qd2YaBQMjM=</Modulus><Exponent>AQAB</Exponent></RSAKeyValue>";
|
|
|
|
/// <summary>
|
|
/// 数字签名
|
|
/// </summary>
|
|
/// <param name="plaintext">原文</param>
|
|
/// <param name="privateKey">私钥</param>
|
|
/// <returns>签名</returns>
|
|
public static string DataSign(string plaintext, string privateKey = "")
|
|
{
|
|
if (string.IsNullOrEmpty(privateKey))
|
|
privateKey = sPrivateKey;
|
|
|
|
UnicodeEncoding ByteConverter = new UnicodeEncoding();
|
|
byte[] dataToEncrypt = ByteConverter.GetBytes(plaintext);
|
|
|
|
using (RSACryptoServiceProvider RSAalg = new RSACryptoServiceProvider())
|
|
{
|
|
RSAalg.FromXmlString(privateKey);
|
|
//使用SHA1进行摘要算法,生成签名
|
|
byte[] encryptedData = RSAalg.SignData(dataToEncrypt, new SHA1CryptoServiceProvider());
|
|
return Convert.ToBase64String(encryptedData);
|
|
}
|
|
}
|
|
|
|
/// <summary>
|
|
/// 验证签名
|
|
/// </summary>
|
|
/// <param name="plaintext">原文</param>
|
|
/// <param name="signedData">签名</param>
|
|
/// <param name="publicKey">公钥</param>
|
|
/// <returns></returns>
|
|
public static bool DataVerify(string plaintext, string signedData, string publicKey = "")
|
|
{
|
|
if (string.IsNullOrEmpty(publicKey))
|
|
publicKey = sPublicKey;
|
|
|
|
using (RSACryptoServiceProvider RSAalg = new RSACryptoServiceProvider())
|
|
{
|
|
RSAalg.FromXmlString(publicKey);
|
|
UnicodeEncoding ByteConverter = new UnicodeEncoding();
|
|
byte[] dataToVerifyBytes = ByteConverter.GetBytes(plaintext);
|
|
byte[] signedDataBytes = Convert.FromBase64String(signedData);
|
|
return RSAalg.VerifyData(dataToVerifyBytes, new SHA1CryptoServiceProvider(), signedDataBytes);
|
|
}
|
|
}
|
|
#endregion
|
|
|
|
/// <summary>
|
|
/// 获取非对程加密Key
|
|
/// 键为公钥,值为私钥
|
|
/// </summary>
|
|
/// <returns></returns>
|
|
public static KeyValuePair<string, string> CreateRSAKey()
|
|
{
|
|
RSACryptoServiceProvider RSA = new RSACryptoServiceProvider();
|
|
string privateKey = RSA.ToXmlString(true);
|
|
string publicKey = RSA.ToXmlString(false);
|
|
|
|
return new KeyValuePair<string, string>(publicKey, privateKey);
|
|
}
|
|
|
|
private static byte[] Keys = { 0x12, 0x34, 0x56, 0x78, 0x90, 0xAB, 0xCD, 0xEF };
|
|
/// <summary>
|
|
/// 进行DES解密。
|
|
/// </summary>
|
|
/// <param name="pToDecrypt">要解密的base64串</param>
|
|
/// <param name="sKey">密钥,且必须为8位。</param>
|
|
/// <returns>已解密的字符串。</returns>
|
|
public static string DESDecode(string source, string sKey)
|
|
{
|
|
//try
|
|
//{
|
|
// byte[] rgbKey = Encoding.UTF8.GetBytes(sKey);
|
|
// byte[] rgbIV = Keys;
|
|
// byte[] inputByteArray = Convert.FromBase64String(source);
|
|
// DESCryptoServiceProvider DCSP = new DESCryptoServiceProvider();
|
|
// MemoryStream mStream = new MemoryStream();
|
|
// CryptoStream cStream = new CryptoStream(mStream, DCSP.CreateDecryptor(rgbKey, rgbIV), CryptoStreamMode.Write);
|
|
// cStream.Write(inputByteArray, 0, inputByteArray.Length);
|
|
// cStream.FlushFinalBlock();
|
|
// return Encoding.UTF8.GetString(mStream.ToArray());
|
|
//}
|
|
//catch
|
|
//{
|
|
// return source;
|
|
//}
|
|
|
|
|
|
try
|
|
{
|
|
byte[] inputByteArray = System.Convert.FromBase64String(source);//Encoding.UTF8.GetBytes(source);
|
|
using (DESCryptoServiceProvider des = new DESCryptoServiceProvider())
|
|
{
|
|
des.Key = ASCIIEncoding.ASCII.GetBytes(sKey);
|
|
des.IV = Keys;
|
|
System.IO.MemoryStream ms = new System.IO.MemoryStream();
|
|
using (CryptoStream cs = new CryptoStream(ms, des.CreateDecryptor(), CryptoStreamMode.Write))
|
|
{
|
|
cs.Write(inputByteArray, 0, inputByteArray.Length);
|
|
cs.FlushFinalBlock();
|
|
cs.Close();
|
|
}
|
|
string str = Encoding.UTF8.GetString(ms.ToArray());
|
|
ms.Close();
|
|
return str;
|
|
}
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
return "";
|
|
}
|
|
}
|
|
|
|
/// <summary>
|
|
/// DES加密方法
|
|
/// </summary>
|
|
/// <param name="encryptedValue">要加密的字符串</param>
|
|
/// <param name="key">密钥</param>
|
|
/// <returns>加密后的字符串</returns>
|
|
public static string DESEncrypt(string originalValue, string key)
|
|
{
|
|
try
|
|
{
|
|
using (DESCryptoServiceProvider sa = new DESCryptoServiceProvider { Key = Encoding.UTF8.GetBytes(key), IV = Keys })
|
|
{
|
|
using (ICryptoTransform ct = sa.CreateEncryptor())
|
|
{
|
|
byte[] by = Encoding.UTF8.GetBytes(originalValue);
|
|
using (var ms = new MemoryStream())
|
|
{
|
|
using (var cs = new CryptoStream(ms, ct, CryptoStreamMode.Write))
|
|
{
|
|
cs.Write(by, 0, by.Length);
|
|
cs.FlushFinalBlock();
|
|
}
|
|
return Convert.ToBase64String(ms.ToArray());
|
|
}
|
|
}
|
|
}
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
LogSave.Add("DES Encrypt Exception:" + ex.Message);
|
|
return "";
|
|
}
|
|
}
|
|
}
|
|
}
|