📄 ptym.cs
字号:
using System;
using System.Collections.Generic;
using System.Text;
using System.Security.Cryptography;
using System.IO;
namespace Ptym
{
public class Security
{
public Security()
{
}
//默认密钥向量
private static byte[] Keys = { 0x14, 0x14, 0x21, 0x14, 0x14, 0x21, 0x14, 0x14 };
private static string defaultiv ="yourself";
public static string[] EncryptDES(string encryptString)
{
return EncryptDES(encryptString, defaultiv);
}
public static string[] DecryptDES(string decryptString)
{
return DecryptDES(decryptString, defaultiv);
}
/// <summary>
/// DES加密字符串
/// </summary>
/// <param name="encryptString">待加密的字符串</param>
/// <param name="encryptKey">加密密钥,要求为8位</param>
/// <returns>返回数组,string[0]=0为成功,=1为失败,失败时反回原始值</returns>
public static string[] EncryptDES(string encryptString, string encryptKey)
{
string[] returnvalue = new string[2];
try
{
byte[] rgbKey = Encoding.UTF8.GetBytes(encryptKey.Substring(0, 8));
byte[] rgbIV = Keys;
byte[] inputByteArray = Encoding.UTF8.GetBytes(encryptString);
DESCryptoServiceProvider dCSP = new DESCryptoServiceProvider();
MemoryStream mStream = new MemoryStream();
CryptoStream cStream = new CryptoStream(mStream, dCSP.CreateEncryptor(rgbKey, rgbIV), CryptoStreamMode.Write);
cStream.Write(inputByteArray, 0, inputByteArray.Length);
cStream.FlushFinalBlock();
returnvalue[0] = "0";
returnvalue[1] = Convert.ToBase64String(mStream.ToArray());
}
catch
{
returnvalue[0] = "1";
returnvalue[1] = encryptString;
}
return returnvalue;
}
/// <summary>
/// DES解密字符串
/// </summary>
/// <param name="decryptString">待解密的字符串</param>
/// <param name="decryptKey">解密密钥,要求为8位,和加密密钥相同</param>
/// <returns>返回数组,string[0]=0为成功,=1为失败,失败时反回原始值</returns>
public static string[] DecryptDES(string decryptString, string decryptKey)
{
string[] returnvalue = new string[2];
try
{
byte[] rgbKey = Encoding.UTF8.GetBytes(decryptKey);
byte[] rgbIV = Keys;
byte[] inputByteArray = Convert.FromBase64String(decryptString);
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();
returnvalue[0] = "0";
returnvalue[1] = Encoding.UTF8.GetString(mStream.ToArray());
}
catch
{
returnvalue[0] = "1";
returnvalue[1] = decryptString;
}
return returnvalue;
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -