1类库:

2使用方法


using System;
using System.Collections.Generic;
using System.Text;
using System.IO;
using System.Security.Cryptography;
namespace Szj
{
internal sealed class Util
{
const string EncryptText = "惺惺惜惺惺";
/// <summary>
/// 加密
/// </summary>
/// <param name="strText"></param>
/// <returns></returns>
public static String Encrypt(String strText)
{
Byte[] byKey = { };
Byte[] IV = { 0x12, 0x34, 0x56, 0x78, 0x90, 0xAB, 0xCD, 0xEF };
try
{
byKey = System.Text.Encoding.UTF8.GetBytes(EncryptText.Substring(0, 8));
DESCryptoServiceProvider des = new DESCryptoServiceProvider();
Byte[] inputByteArray = Encoding.UTF8.GetBytes(strText);
MemoryStream ms = new MemoryStream();
CryptoStream cs = new CryptoStream(ms, des.CreateEncryptor(byKey, IV), CryptoStreamMode.Write);
cs.Write(inputByteArray, 0, inputByteArray.Length);
cs.FlushFinalBlock();
return Convert.ToBase64String(ms.ToArray());
}
catch (Exception ex)
{
return ex.Message;
}
}
/// <summary>
/// 解密
/// </summary>
/// <param name="strText"></param>
/// <returns></returns>
public static String Decrypt(String strText)
{
Byte[] byKey = { };
Byte[] IV = { 0x12, 0x34, 0x56, 0x78, 0x90, 0xAB, 0xCD, 0xEF };
Byte[] inputByteArray = new byte[strText.Length];
try
{
byKey = System.Text.Encoding.UTF8.GetBytes(EncryptText.Substring(0, 8));
DESCryptoServiceProvider des = new DESCryptoServiceProvider();
inputByteArray = Convert.FromBase64String(strText);
MemoryStream ms = new MemoryStream();
CryptoStream cs = new CryptoStream(ms, des.CreateDecryptor(byKey, IV), CryptoStreamMode.Write);
cs.Write(inputByteArray, 0, inputByteArray.Length);
cs.FlushFinalBlock();
System.Text.Encoding encoding = System.Text.Encoding.UTF8;
return encoding.GetString(ms.ToArray());
}
catch (Exception ex)
{
return ex.Message;
}
}
public static string MD5(string inStr)
{
MD5CryptoServiceProvider md5 = new MD5CryptoServiceProvider();
byte[] InBytes = Encoding.GetEncoding("GB2312").GetBytes(inStr);
byte[] OutBytes = md5.ComputeHash(InBytes);
string OutString = "";
for (int i = 0; i < OutBytes.Length; i++)
{
OutString += OutBytes[i].ToString("x2");
}
return OutString;
}
}
}
from窗体
if (RegDog.VerifyMachineCode(txtMachineCode.Text.Trim(), txtRegCode.Text.Trim()))
{
MessageBox.Show("注册成功!");
this.DialogResult = DialogResult.OK;
}
else
{
MessageBox.Show("错误的注册码");
}

本文介绍了一个C#实现的加密解密类,包括DES加密算法的使用,并展示了如何通过这些加密方法来验证软件注册码的有效性。
647

被折叠的 条评论
为什么被折叠?



