最簡單的授權檔案

zxg131x發表於2010-07-20

製作一個簡單的授權檔案

 

以.licx 字尾的文字檔案,在其中儲存經過加密的授權碼,然後通過簡單的加/解密,和檔案讀寫操作,就可以完成相應的授權驗證。

 

 

licenses.licx
用記事本開啟就有FreeTextBoxControls.FreeTextBox,   FreeTextBox,   Version=3.0.3300.3,   Culture=neutral,   PublicKeyToken=5962a4e684a48b87

看看加密,解密
public   string   DesEncrypt(string   strText,   string   strEncrKey)
{
byte[]   byKey=null;
byte[]   IV=   {0x12,   0x34,   0x56,   0x78,   0x90,   0xAB,   0xCD,   0xEF};
try
{
byKey   =   System.Text.Encoding.UTF8.GetBytes(strEncrKey.Substring(0,strEncrKey.Length));
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(System.Exception   error)
{
return   "error: "   +error.Message+ "/r ";
}
}

public   string   DesDecrypt(string   strText,string   sDecrKey)
{
byte[]   byKey   =   null;
byte[]   IV=   {0x12,   0x34,   0x56,   0x78,   0x90,   0xAB,   0xCD,   0xEF};
byte[]   inputByteArray   =   new   Byte[strText.Length];
try
{
byKey   =   System.Text.Encoding.UTF8.GetBytes(sDecrKey.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   =   new   System.Text.UTF8Encoding();
return   encoding.GetString(ms.ToArray());
}
catch(System.Exception   error)
{
return   "error: "+error.Message+ "/r ";
}

}

例項化一個類,變數名jiami
jiami.DesEncrypt( "www.mytopwin.com ");
得到wIToenOWESRztJZx42D9MSbLpu7HvK2H

解密
jiami.DesDecrypt( "wIToenOWESRztJZx42D9MSbLpu7HvK2H ");
的到www.mytopwin.com

相關文章