src/DotNetCommon.Core/Encrypt/DESEncrypt.cs
(开头部分) 7KB这里只显示每个文件的开头 60 行。登录后可以解锁完整代码。
using System;
using System.IO;
using System.Security.Cryptography;
using System.Text;
using DotNetCommon.Extensions;
namespace DotNetCommon.Encrypt
{
/// <summary>
/// DES加解密
/// </summary>
public static class DESEncrypt
{
/// <summary>
/// DES加密字符串
/// </summary>
/// <param name="encryptString">待加密的字符串</param>
/// <param name="encryptKey">加密密钥,必须为8位ASCII码,否则不成功</param>
/// <exception cref="Exception">加密过程失败</exception>
/// <returns>加密后的字符串(16进制表示法)</returns>
public static string Encrypt(string encryptString, string encryptKey)
{
if (string.IsNullOrEmpty(encryptString)) return encryptString;
if (string.IsNullOrWhiteSpace(encryptKey) || encryptKey.Length != 8) throw new Exception("秘钥的长度必须为8!");
byte[] rgbKey = ASCIIEncoding.ASCII.GetBytes(encryptKey);
byte[] rgbIV = ASCIIEncoding.ASCII.GetBytes(MD5Encrypt.MD5(encryptKey).Substring(0, 8));
byte[] inputByteArray = Encoding.UTF8.GetBytes(encryptString);
DESCryptoServiceProvider des = new DESCryptoServiceProvider();
des.Key = rgbKey;
des.IV = rgbIV;
MemoryStream mStream = new MemoryStream();
CryptoStream cStream = new CryptoStream(mStream, des.CreateEncryptor(), CryptoStreamMode.Write);
cStream.Write(inputByteArray, 0, inputByteArray.Length);
cStream.FlushFinalBlock();
StringBuilder ret = new StringBuilder();
foreach (byte b in mStream.ToArray())
{
ret.AppendFormat("{0:X2}", b);
}
return ret.ToString();
}
/// <summary>
/// DES解密字符串
/// </summary>
/// <param name="decryptString">待解密的字符串</param>
/// <param name="decryptKey">加密密钥,必须为8位ASCII码,否则不成功</param>
/// <exception cref="Exception"></exception>
/// <returns>解密后的字符串</returns>
public static string Decrypt(string decryptString, string decryptKey)
{
if (string.IsNullOrEmpty(decryptString)) return decryptString;
if (string.IsNullOrWhiteSpace(decryptKey) || decryptKey.Length != 8) throw new Exception("秘钥的长度必须为8!");
DESCryptoServiceProvider des = new DESCryptoServiceProvider();
int len;
len = decryptString.Length / 2;
byte[] inputByteArray = new byte[len];
int x, i;
后面还有 117 行代码,解锁后查看完整代码
24 小时内免费解锁 3 个项目,之后 1 积分/个。 规则说明
AI 解读
登录后可用,每次 10 积分,解读结果公开显示在下面。
还没有人解读过这个文件。
