ECGPrint/1200Gxml心电图绘制/DESEncrypt.cs

97 lines
3.6 KiB
C#
Raw Normal View History

2024-12-25 17:24:22 +08:00
using System;
using System.IO;
using System.Security.Cryptography;
using System.Text;
using System.Web.Security;
namespace _1200Gxml心电图绘制
{
public class DesEncrypt
{
#region ================
/// <summary>
/// 加密
/// </summary>
/// <param name="text"></param>
/// <returns></returns>
public static string Encrypt(string text)
{
return Encrypt(text, "da1ff5@1b453d1f66d83bc6e7c5cab4$");
}
/// <summary>
/// 加密数据
/// </summary>
/// <param name="text"></param>
/// <param name="sKey"></param>
/// <returns></returns>
public static string Encrypt(string text, string sKey)
{
var des = new DESCryptoServiceProvider();
var inputByteArray = Encoding.Default.GetBytes(text);
var hashPasswordForStoringInConfigFile =
FormsAuthentication.HashPasswordForStoringInConfigFile(sKey, "md5");
if (hashPasswordForStoringInConfigFile != null)
des.Key = Encoding.ASCII.GetBytes(hashPasswordForStoringInConfigFile.Substring(0, 8));
var passwordForStoringInConfigFile = FormsAuthentication.HashPasswordForStoringInConfigFile(sKey, "md5");
if (passwordForStoringInConfigFile != null)
des.IV = Encoding.ASCII.GetBytes(passwordForStoringInConfigFile.Substring(0, 8));
var ms = new MemoryStream();
var cs = new CryptoStream(ms, des.CreateEncryptor(), CryptoStreamMode.Write);
cs.Write(inputByteArray, 0, inputByteArray.Length);
cs.FlushFinalBlock();
var ret = new StringBuilder();
foreach (var b in ms.ToArray()) ret.AppendFormat("{0:X2}", b);
return ret.ToString();
}
#endregion
#region ================
/// <summary>
/// 解密
/// </summary>
/// <param name="text"></param>
/// <returns></returns>
public static string Decrypt(string text)
{
return Decrypt(text, "da1ff5@1b453d1f66d83bc6e7c5cab4$");
}
/// <summary>
/// 解密数据
/// </summary>
/// <param name="text"></param>
/// <param name="sKey"></param>
/// <returns></returns>
public static string Decrypt(string text, string sKey)
{
var des = new DESCryptoServiceProvider();
var len = text.Length / 2;
var inputByteArray = new byte[len];
int x, i;
for (x = 0; x < len; x++)
{
i = Convert.ToInt32(text.Substring(x * 2, 2), 16);
inputByteArray[x] = (byte)i;
}
var hashPasswordForStoringInConfigFile =
FormsAuthentication.HashPasswordForStoringInConfigFile(sKey, "md5");
if (hashPasswordForStoringInConfigFile != null)
des.Key = Encoding.ASCII.GetBytes(hashPasswordForStoringInConfigFile.Substring(0, 8));
var passwordForStoringInConfigFile = FormsAuthentication.HashPasswordForStoringInConfigFile(sKey, "md5");
if (passwordForStoringInConfigFile != null)
des.IV = Encoding.ASCII.GetBytes(passwordForStoringInConfigFile.Substring(0, 8));
var ms = new MemoryStream();
var cs = new CryptoStream(ms, des.CreateDecryptor(), CryptoStreamMode.Write);
cs.Write(inputByteArray, 0, inputByteArray.Length);
cs.FlushFinalBlock();
return Encoding.Default.GetString(ms.ToArray());
}
#endregion
}
}