我想要对自己的软件进行加密,
让用户使用时要输入注册码我们经常用的软件在包装上都有序列号

解决方案 »

  1.   

    你自己可以写算法进行加密,比如,你可以采用获取系统盘的串号,然后自己写个算法尽心家比,可以拥des加密或者md5加密.你可以看看这个类:
    using System;
    using System.Runtime.InteropServices; 
    using Microsoft.Win32;namespace test
    {
    /// <summary>
    /// GetSystemVol 的摘要说明。
    /// </summary>
    public class GetSystemVol
    {
    public GetSystemVol()
    {

    } #region 加密 [DllImport("kernel32.dll")] 
    private static extern int GetVolumeInformation( 
    string lpRootPathName, 
    string lpVolumeNameBuffer, 
    int nVolumeNameSize, 
    ref int lpVolumeSerialNumber, 
    int lpMaximumComponentLength, 
    int lpFileSystemFlags, 
    string lpFileSystemNameBuffer, 
    int nFileSystemNameSize 
    ); 
    /// <summary>
    /// 获取指定盘符的硬盘串号
    /// </summary>
    /// <param name="drvID">盘符</param>
    /// <returns></returns>
    public static string GetVolOf(string drvID)

    if(drvID.Length >1)
    {
    drvID = "C";
    }
    const int MAX_FILENAME_LEN = 256; 
    int retVal = 0; 
    int a =0; 
    int b =0; 
    string str1 = null; 
    string str2 = null;  int i = GetVolumeInformation( 
    drvID + @":\", 
    str1, 
    MAX_FILENAME_LEN, 
    ref retVal, 
    a, 
    b, 
    str2, 
    MAX_FILENAME_LEN 
    ); 
    return retVal.ToString("x"); 
    }  /// <summary>
    /// 获取字符串的MD5加密算法
    /// </summary>
    /// <param name="sDataIn"></param>
    /// <returns></returns>
    public static string GetMD5(string sDataIn)
    {
    System.Security.Cryptography.MD5CryptoServiceProvider md5=new System.Security.Cryptography.MD5CryptoServiceProvider(); 
    byte[]bytValue,bytHash;
    bytValue = System.Text.Encoding.UTF8.GetBytes(sDataIn);
    bytHash = md5.ComputeHash(bytValue);
    md5.Clear();
    string sTemp="";
    for(int i=0;i<bytHash.Length;i++)
    {
    sTemp+=bytHash[i].ToString("X").PadLeft(2,'0');
    }
    return sTemp.ToUpper();
    } #endregion #region 注册表操作 /// <summary>
    /// 读取指定名称的注册表的值
    /// </summary>
    /// <param name="name"></param>
    /// <returns></returns>
    public static string GetRegistData(string name) 

    string registData; 
    RegistryKey hkml = Registry.LocalMachine; 
    RegistryKey software = hkml.OpenSubKey("SOFTWARE",true); 
    RegistryKey aimdir = software.OpenSubKey("henanljw",true); 
    registData = aimdir.GetValue(name).ToString(); 
    return registData; 
    }
    /// <summary>
    /// 向注册表中写数据 
    /// </summary>
    /// <param name="name">注册表串值</param>
    /// <param name="tovalue">要写的数据值</param>
    /// <returns>写入成功返回true,否则返回false</returns>
    public static bool WTRegedit(string name,string tovalue) 
    {
    try
    {
    RegistryKey hklm = Registry.LocalMachine; 
    RegistryKey software = hklm.OpenSubKey("SOFTWARE",true); 
    RegistryKey aimdir = software.CreateSubKey("henanljw"); 
    aimdir.SetValue(name,tovalue); 
    return true;
    }
    catch
    {
    return false;
    }
    }  /// <summary>
    /// 删除注册表中指定的注册表项 
    /// </summary>
    /// <param name="name"></param>
    public static void DeleteRegist(string name) 

    string[] aimnames; 
    RegistryKey hkml = Registry.LocalMachine; 
    RegistryKey software = hkml.OpenSubKey("SOFTWARE",true); 
    // RegistryKey aimdir = software.OpenSubKey("henanljw",true); 
    // aimnames = aimdir.GetSubKeyNames(); 
    aimnames = software.GetSubKeyNames();
    foreach(string aimKey in aimnames) 

    if(aimKey == name) 
    software.DeleteSubKeyTree(name); 


    /// <summary>
    /// 判断指定注册表项是否存在
    /// </summary>
    /// <param name="name"></param>
    /// <returns></returns>
    public static bool IsRegeditExit(string name) 

    bool _exit = false; 
    string[] subkeyNames; 
    RegistryKey hkml = Registry.LocalMachine; 
    RegistryKey software = hkml.OpenSubKey("SOFTWARE",true); 
    // RegistryKey aimdir = software.OpenSubKey("henanljw",true); 
    subkeyNames = software.GetSubKeyNames(); 
    // subkeyNames = aimdir.GetValueNames(); 
    foreach(string keyName in subkeyNames) 

    if(keyName == name) 

    _exit = true; 
    return _exit; 


    return _exit; 
    }  #endregion
    }
    }