Files
TouchSocket/RRQMCore/Data/Security/DataLock.cs
2022-03-20 10:51:15 +08:00

69 lines
2.9 KiB
C#
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

//------------------------------------------------------------------------------
// 此代码版权除特别声明或在RRQMCore.XREF命名空间的代码归作者本人若汝棋茗所有
// 源代码使用协议遵循本仓库的开源协议及附加协议若本仓库没有设置则按MIT开源协议授权
// CSDN博客https://blog.csdn.net/qq_40374647
// 哔哩哔哩视频https://space.bilibili.com/94253567
// Gitee源代码仓库https://gitee.com/RRQM_Home
// Github源代码仓库https://github.com/RRQM
// API首页https://www.yuque.com/eo2w71/rrqm
// 交流QQ群234762506
// 感谢您的下载和使用
//------------------------------------------------------------------------------
//------------------------------------------------------------------------------
using System.IO;
using System.Security.Cryptography;
using System.Text;
namespace RRQMCore.Data.Security
{
/// <summary>
/// 数据锁,用于加密或解密
/// </summary>
public static class DataLock
{
/// <summary>
/// 使用3DES加密
/// </summary>
/// <param name="data">待加密字节</param>
/// <param name="encryptKey">加密口令长度为8</param>
/// <returns></returns>
public static byte[] EncryptDES(byte[] data, string encryptKey)
{
byte[] rgbKey = Encoding.UTF8.GetBytes(encryptKey.Substring(0, 8));
byte[] rgbIV = { 0x12, 0x34, 4, 0x78, 0x90, 255, 0xCD, 0xEF };
DESCryptoServiceProvider dCSP = new DESCryptoServiceProvider();
using (MemoryStream mStream = new MemoryStream())
{
using (CryptoStream cStream = new CryptoStream(mStream, dCSP.CreateEncryptor(rgbKey, rgbIV), CryptoStreamMode.Write))
{
cStream.Write(data, 0, data.Length);
cStream.FlushFinalBlock();
return mStream.ToArray();
}
}
}
/// <summary>
/// 使用3DES解密
/// </summary>
/// <param name="data">待解密字节</param>
/// <param name="decryptionKey">解密口令长度为8</param>
/// <returns></returns>
public static byte[] DecryptDES(byte[] data, string decryptionKey)
{
byte[] rgbKey = Encoding.UTF8.GetBytes(decryptionKey);
byte[] rgbIV = { 0x12, 0x34, 4, 0x78, 0x90, 255, 0xCD, 0xEF };
DESCryptoServiceProvider DCSP = new DESCryptoServiceProvider();
using (MemoryStream mStream = new MemoryStream())
{
using (CryptoStream cStream = new CryptoStream(mStream, DCSP.CreateDecryptor(rgbKey, rgbIV), CryptoStreamMode.Write))
{
cStream.Write(data, 0, data.Length);
cStream.FlushFinalBlock();
return mStream.ToArray();
}
}
}
}
}