程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> 網頁編程 >> PHP編程 >> PHP綜合 >> Discuz! Passport 通行證整合

Discuz! Passport 通行證整合

編輯:PHP綜合


解決辦法:
第一步: 到dz的數據庫表cdb_settings 找到下面這幾行修改為

第二步: 刪除dz安裝目錄/forumdata/cache/cache_settings.php
第三步: 重新訪問論壇


登陸和注冊整合流程
用戶從登陸或注冊表單提交帳號密碼信息 ==>
主站程序檢驗用戶登陸或注冊,成功(注冊需要生成新用戶)則 ==>
設置主站自身的cookie或session    ==>
url傳遞 返回地址forward和編碼後的用戶信息和其他信息 到dz/api/passport.php

整合之前請先仔細閱讀官方passport技術文檔: http://www.discuz.net/usersguide/advanced_passport.htm
<?php
//該文檔保存為login.php
//首先將接口技術文檔裡的加密解密函數拷貝
//為了不讓代碼太亂,我拷貝到文檔的結尾處
//假設自己的用戶數據庫表裡用戶名字段為UserName, 密碼字段為Pwd, Email字段為 Email
//注冊頁實現方法差不多,可自行實現,疑問加我QQ:2666556

$act=(isset($_GET['act']))?$_GET['act']:"login";
if(function_exists($act)) $act();else login();       

function login()
{       
        $ErrMsg=UserCheck();
        if($ErrMsg!="")echo $ErrMsg;       
        //後面加上顯示你的登陸表單的代碼 如
?>
<form action=login.php?act=login method=post>
用戶名:<input name=username>
密碼:<input name=password>
<input name=submit type=submit value=登陸></form>
<?php

}//end function

function logout()//登出
{
        $passportkey="1234567890";//這裡換成你論壇通行證設置的passportkey
        $auth=$_COOKIE['auth'];
        setcookie("auth", "",time() - 3600);
        $forward=$_GET['forward'];
        if($forward=="")$forward="../../index.php";//這裡換成你的主頁絕對地址或相對地址                                       
        $verify = md5('logout'.$auth.$forward.$passportkey);
        $auth=rawurlencode($auth);
        $forward=rawurlencode($forward);
        header("Location: bbs/api/passport.php?action=logout&auth=$auth&forward=$forward&verify=$verify");
}

function UserCheck()
{       
        $passportkey="1234567890";//這裡換成你論壇通行證設置的passportkey

        //===========驗證輸入=====================
        if(!isset($_POST['submit'])) return; // login表單的按鈕需要與此同名
        $usnm=$_POST['username'];//username換成你登陸表單裡的用戶名域                       
        $pwd=$_POST['password'];//password換成你登陸表單裡的密碼域                       
        if($usnm=="") return "請輸入用戶名!";
        if($pwd=="") return "請輸入密碼!";

        //=========數據庫處理==========================
        $db=mysql_connect("localhost", "root", "");
        mysql_select_db("your_db_name");
$sql="Select * from `user` where UserName='".$usnm."' Limit 1";       
        $rs = mysql_query($sql,$db)        ;
        $row = mysql_fetch_array($rs);
        if(!$row)return "該用戶不存在";
        if($row["Pwd"]!=md5($pwd))return "密碼錯誤";
        mysql_free_result($rs);       

        //==============header到bbs=====================       
        $member = array
        (
                        'time'     => time(),
                        'username' => $row["UserName"],
                        'password' => $row["Pwd"],
                        'email'    => $row["Email"]
        );
        $auth = passport_encrypt(passport_encode($member), $passportkey);
        setcookie("auth",$auth,($_POST["Cookie"]? time()+(int)$_POST["Cookie"] :0));
        $forward=$_POST['forward'];
        if($forward=="")$forward="../../index.php";                                       
        $verify = md5('login'.$auth.$forward.$passportkey);
        $auth=rawurlencode($auth);
        $forward=rawurlencode($forward);
        header("Location: bbs/api/passport.php?action=login&auth=$auth&forward=$forward&verify=$verify");                       

}


//=============================================================
//=============以下為拷貝過來的函數============================
function passport_encrypt($txt, $key) {
        srand((double)microtime() * 1000000);
        $encrypt_key = md5(rand(0, 32000));       
        $ctr = 0;
        $tmp = '';
        for($i = 0; $i < strlen($txt); $i++) {
                        $ctr = $ctr == strlen($encrypt_key) ? 0 : $ctr;
                        $tmp .= $encrypt_key[$ctr].($txt[$i] ^ $encrypt_key[$ctr++]);
        }
        return base64_encode(passport_key($tmp, $key));
}

function passport_decrypt($txt, $key) {
        $txt = passport_key(base64_decode($txt), $key);
        $tmp = '';
        for ($i = 0; $i < strlen($txt); $i++) {
                 $tmp .= $txt[$i] ^ $txt[++$i];
        }
        return $tmp;
}

function passport_key($txt, $encrypt_key) {
        $encrypt_key = md5($encrypt_key);
        $ctr = 0;
        $tmp = '';
        for($i = 0; $i < strlen($txt); $i++) {
                        $ctr = $ctr == strlen($encrypt_key) ? 0 : $ctr;
                        $tmp .= $txt[$i] ^ $encrypt_key[$ctr++];
        }
        return $tmp;
}

function passport_encode($array) {
        $arrayenc = array();
        foreach($array as $key => $val) {
                   $arrayenc[] = $key.'='.urlencode($val);
        }
        return implode('&', $arrayenc);

}
//=========================================================================
//===========================拷貝結束======================================
?>

第二篇:php168 v3.5與discuz v4.10的整合
168整站 與 discuz的passport整合


php168整站v3.5與discuz v4.1.0的整合(帶視頻)

使用說明
請注意自己所用的程序的版本號
login.php 登陸和退出的整合
register.php 注冊的整合


整合前請先核對一下你使用的php168的login.php 和 register.php 是否和 整合文件.rar中[原文件] 文件夾下的文件相同
如果不同..請不要整合

(一)准備工作
1.下載php168 v3.5
2.下載discuz bbs v4.1.0


(二)安裝和整合 (請參考視頻安裝)

1.安裝php168 v3.5 (如已安裝 到第2步) (如何安裝請參考php168的安裝說明)
2.進入php168的後台管理界面關閉168的論壇整合功能 (新安裝可跳過)
3.在php168的安裝目錄下建立名稱為bbs的文件夾
4.下載安裝discuz bbs v4.1.0的upload/下的所有文件拷貝到bbs文件夾下
5.運行discuz bbs的install.php 進行安裝
6.到discuz的後台管理設置開啟通行證
具體參數如下
啟用通行證:       是
應用程序 URL 地址: http://localhost/php168/ 根據具體情況設置
通行證私有密匙:   1234567890 (如果這裡改了..兩個整合文件中的$passportkey的值也必須改成相同的值)
驗證字串有效期(秒): 3600
應用程序注冊地址:   register.php
應用程序登錄地址:   login.php
應用程序退出地址:   login.php?action=quit
積分同步選項:     不同步積分
7.將整合壓縮包中的login.php 和 register.php替換 php168下同名的文件
安裝完成


/請保留以下信息,以方便其他網友在整合時遇到問題可以獲得更多幫助
//整合貢獻者: huozhe3136 疑問請加我qq: 2666556 (提供整合效果的演示)
//視頻貢獻: 一熱心網友
//關於整合的更多內容請訪問http://www.discuz.net/thread-304838-1-1.html


程序打包下載地址:http://www.photoxtd.com/chinaylrc/php168_discuz.rar
視頻演示下載地址:http://www.photoxtd.com/chinaylrc/php168vcr.rar
apmserv5.12 下載地址: http://down.chinaz.com/s/7558.asp
/tech/uploadfile/200702/20070227023513192.rar


第三篇: asp程序與discuz v4.10的整合方法
1.文件discuz_passport.asp
<%
'請保留以下信息以備出現問題時使用
'貢獻者 huozhe3136 QQ:2666556
'=====================================================
'============仿discuz passport編碼函數================
'=====================================================

function passport_encrypt(txt, key)
                dim encrypt_key, encrypt_key_ary,txt_ary
                dim ctr,tmp,i     
        Randomize
                encrypt_key=md5(Int(32000* Rnd),32)
                encrypt_key_ary=strtoary(encrypt_key)               
        txt_ary=strtoary(txt)     
        ctr = 0
        tmp = ""               
        for i = 0 to StrLength(txt)-1
                        if ctr=len(encrypt_key)  then         ctr=0  else ctr=ctr                               
            tmp = tmp & encrypt_key_Ary(ctr) &  mxor(txt_ary(i),encrypt_key_ary(ctr))
            ctr=ctr+1                                                    
        next
                passport_encrypt=strAnsi2Unicode(base64Encode(passport_key(tmp, key)))               
end function


'=====================================================
'============仿discuz passport解碼函數================
'=====================================================
function passport_decrypt(txt, key)
                Dim txt_ary,tmp,i               
                txt = passport_key(base64Decode(strUnicode2Ansi(txt)), key)
                txt_ary = AnsiToAry(txt)
        ' 變量初始化
                tmp = ""
        for i = 0 To lenb(txt)-1
                  tmp = tmp & mxor(txt_ary(i),txt_ary(i+1))
                  i=i+1
                next
                passport_decrypt = strAnsi2Unicode(tmp)
end function 

'=====================================================
'==========仿discuz Passport 密匙處理函數=============
'=====================================================
function passport_key(ansitxt, encrypt_key)
                dim encrypt_key_tmp,encrypt_key_ary,txt_ary
                dim ctr,tmp,i
                encrypt_key_tmp = md5(encrypt_key,32)               
        encrypt_key_ary=StrToAry(encrypt_key_tmp)               
                txt_ary=AnsiToAry(ansitxt)                
                ctr = 0
        tmp = ""                  
        for i=0 to lenb(ansitxt)-1                       
                    if ctr=len(encrypt_key_tmp) then ctr=0 else ctr=ctr
            tmp= tmp & mxor(txt_ary(i),encrypt_key_ary(ctr))
            ctr=ctr+1
        next
                passport_key=tmp               
end function

'=====================================================
'=================將字符串轉換為Ansi數組==============
'=====================================================
function StrToAry(str)
   dim ary(),length,tmpstr,i
   tmpstr=strUnicode2Ansi(str)
   length=lenb(tmpstr)  
   redim ary(length)  
   for i=0 to length-1                                    
                ary(i)=midb(tmpstr,i+1,1)               
   next 
   StrToAry=ary
end function

'=====================================================
'=================將Ansi字符串轉換為Ansi數組==========
'=====================================================
function AnsiToAry(str)
   dim ary(),length,i
   length=lenb(str)  
   redim ary(length)  
   for i=0 to length-1                                    
                ary(i)=midb(str,i+1,1)               
   next 
   AnsiToAry=ary
end function


'=====================================================
'=================異或操作============================
'=====================================================
function mxor(chrb1,chrb2)
        if chrb1<>"" and chrb2 <>"" then
        mxor=chrb(ascb(chrb1) xor ascb(chrb2))
        end if       
end function


Function StrLength(str)
        ON ERROR RESUME NEXT
        Dim WINNT_CHINESE
        WINNT_CHINESE    = (len("中國")=2)
        If WINNT_CHINESE Then
                Dim l,t,c
                Dim i
                l=len(str)
                t=l
                For i=1 To l
                        c=asc(mid(str,i,1))
                        If c<0 Then c=c+65536
                        If c>255 Then
                                t=t+1
                        End If
                Next
                strLength=t
        Else
                strLength=len(str)
        End If
        If err.number<>0 Then err.clear
End Function

%>

2.文件 login.asp
<!--#include file="md5.asp"-->
<!--#include file="base64.asp"-->
<!--#include file="discuz_passport.asp"-->
<%
'asp的整合方法已更新,,中文注冊亂碼問題已解決
'================================================================
'=====================用法示例==疑問加我QQ:2666556===============
'================================================================
dim PassportKey,YourAspWeb_Url,DiscuzBbs_Url
PassportKey="1234567890"  '這裡換成你discuz論壇通行證設置的passportkey
YourAspWeb_Url="http://localhost/test2/login.asp"  '這裡換成你的主頁絕對地址或相對地址
DiscuzBbs_Url="http://localhost:8080/discuz/"          '這裡換成你的discuz論壇絕對地址或相對地址

select case request.QueryString("act")
        case "login"   call login()
        case "logout"  call logout()
end select

if request.Cookies("username")<>"" then
        dim str
        str=request.Cookies("username") & " 你好 你已登陸成功" &_
    " <a href=""t3.asp?act=logout"">點擊這裡退出登陸</a>" &_
    " <a href="""& DiscuzBbs_Url & """>點擊這裡訪問論壇</a><br>"
    response.Write(str)
end if

function logout()
        dim auth,forward,verify
        auth=request.Cookies("auth")       
        response.Cookies("username")=""
        response.Cookies("auth")=""       
        forward=request.QueryString("forward")
        if forward="" then forward=YourAspWeb_Url                                         
        verify = md5("logout"& auth & forward & PassportKey ,32)
        auth=server.URLEncode(auth)
        forward=server.URLEncode(forward)
        response.Redirect(DiscuzBbs_Url & "api/passport.php?action=logout&auth=" & auth & "&forward=" & forward & "&verify=" & verify)
end function

 

function login()
        dim auth,forward,verify,member_info
        '需根據實際情況修改,這裡只作最簡單的驗證
        if request.Form("username")="admin" and request.Form("password")="123456" then
                '設置自己的cookie 或 session
                response.Cookies("username")=request.Form("username")

                '========================================================
                '===============passport 整合開始========================
                '========================================================
                '假設從用戶數據庫裡取出email 為[email protected]
                member_info="time="  & datediff("s","1970-1-1 00:00:00",now) &_
                                                        "&username=" & request.Form("username") &_
                                                        "&password=" & md5(request.Form("password"),32) &_
                                                        "&email="    & "[email protected]
                auth = passport_encrypt(member_info , PassportKey)               
                response.Cookies("auth")=auth
                forward=request.form("forward")
                if forward="" then forward=YourAspWeb_Url       
                verify = md5("login" & auth & forward & PassportKey,32)
                auth=server.URLEncode(auth)
                forward=server.URLEncode(forward)       
                response.Redirect(DiscuzBbs_Url & "api/passport.php?action=login&auth=" & auth & "&forward=" & forward & "&verify=" & verify)                       
                '========================================================
                '===============passport 整合結束========================
                '========================================================

        else
                        response.Write("登陸失敗<br>")
        end if
end function

%>
<form name="form1" method="post" action="login.asp?act=login">
  用戶名:
  <input type="text" name="username">
  <br>
  密 碼:
  <input type="password" name="password">
  <br>
  <input type="hidden" name="forward" value="<%=request.QueryString("forward")%>">    
  <input type="submit" name="Submit" value=" 登 陸 ">
</form>

/tech/uploadfile/200702/20070227024221846.rar


第四篇 :Asp整合實例-PowerEasy2006 SP2普及版 與 discuz v4.10的整合

PowerEasy下載地址:http://www.powereasy.net/Soft/PE_soft/181.html

步驟1:
        下載安裝powerEasy,下載安裝discuz v4.10

步驟2:
        將user_chklogin.asp 覆蓋到 powereasy安裝根目錄/user/user_chklogin.asp
        將user_logout.asp   覆蓋到 powereasy安裝根目錄/user/user_logout.asp
        將user_regpost.asp  覆蓋到 powereasy安裝根目錄/reg/user_regpost.asp
        拷貝discuz_passport文件夾到Powereasy安裝根目錄

步驟3:  修改discuz_passport裡的config.asp文件部分變量
        Discuz_PassportKey="1234567890"                  '這裡換成你discuz論壇通行證設置的passportkey
        Discuz_YourAspWeb_Url="http://localhost:8080/"  '這裡換成你的主頁絕對地址或相對地址
        Discuz_Bbs_Url="http://localhost/discuz/"       '這裡換成你的discuz論壇絕對地址或相對地址

步驟4:
        到discuz論壇後台通行證設置開啟通行證
        啟用通行證:                 是
        應用程序 URL 地址:         http://localhost:8080/        '這裡換成你的主頁絕對地址或相對地址
        通行證私有密匙:         1234567890                '這裡必須與Discuz_Passportkey的值相同
        驗證字串有效期(秒):        3600
        應用程序注冊地址:        Reg/User_Reg.asp
        應用程序登錄地址:        index.asp
        應用程序退出地址:        User/User_Logout.asp
        具體請參照論壇設置圖片


第五篇 主站與discuz同服務器無需Passport的整合方法

<?php
//discuz同服務器下無passport登陸整合方法
//貢獻:huozhe3136 QQ:2666556
//假設discuz安裝在網站根目錄下的bbs/中
//用法事例
error_reporting(E_ALL);

$path_bbs="bbs/";//請修改為你的論壇的相對路徑
$check_username="admin";//測試時這個用戶名必須在論壇裡也存在
$check_password="123456";
define('IN_DISCUZ', TRUE);//為了包含discuz下的文件這個必須定義
require_once "$path_bbs/config.inc.php";//獲得論壇數據庫的配置
require_once "$path_bbs/include/db_mysql.class.php";

if(@$_GET[act]=="logout"){
    foreach ($_COOKIE as $key => $value) {
        setcookie($key,'',time() - 3600,$cookiepath,$cookiedomain,0);            
            header("Location:".$_SERVER['PHP_SELF']);         
    }
}
if(isset($_POST["submit"])){//檢查是否點擊了提交按鈕
    $username=trim($_POST['username']);
    $pwd=trim($_POST['password']);
    //主站驗證登陸,為簡單起見,這裡只作簡單的示范
    if($username==$check_username && $pwd==$check_password){//測試時這個用戶名必須在論壇裡也存在
        $db = new dbstuff;
        $db->connect($dbhost, $dbuser, $dbpw, $dbname, $pconnect);//$dbhost,$dbuser,$dbpw,$dbname,$pconnect是在bbs/config.inc.php裡定義的變量
        $sql="select * from {$tablepre}members where username='$username'";
        $rs = $db->query($sql);
        if($dzmember = $db->fetch_array($rs)){
            setcookie('myusername',$username,time()+3600,$cookiepath,$cookiedomain,0);//設置主站自身的cookie
            setcookie("{$tablepre}sid",'',time() - 3600,$cookiepath,$cookiedomain,0);//讓discuz的sid過期是為了讓discuz重新分配一個新的sid
            setcookie("{$tablepre}auth", authcode("$dzmember[password]\t$dzmember[secques]\t$dzmember[uid]", 'ENCODE'),time()+3600,$cookiepath,$cookiedomain,0);//$cookiepath和$cookiedomainbbs/config.inc.php裡定義的變量
            header("Location:".$_SERVER['REQUEST_URI']);
        }
    }
}

//下面這個函數是discuz的用戶cookie編碼函數,已修改,原型在$path_bbs/include/global.func.php
function authcode($string, $operation) {
    global $_SERVER, $_DCACHE,$path_bbs;
    require_once "$path_bbs/forumdata/cache/cache_settings.php";//需要獲取論壇的authkey
    $discuz_auth_key = md5($_DCACHE['settings']['authkey'].$_SERVER['HTTP_USER_AGENT']);
    $coded = '';
    $keylength = strlen($discuz_auth_key);
    $string = $operation == 'DECODE' ? base64_decode($string) : $string;
    for($i = 0; $i < strlen($string); $i += $keylength) {
        $coded .= substr($string, $i, $keylength) ^ $discuz_auth_key;
    }
    $coded = $operation == 'ENCODE' ? str_replace('=', '', base64_encode($coded)) : $coded;
    return $coded;
}
?>
<?php
if(@$_COOKIE['myusername']==''){
?>
<form action="" method="POST">
用戶名:<input name="username">測試用戶名為admin,請更改為你的用戶名<br>
密碼:<input type="password" name="password">測試密碼為123456<br>
<input type="submit" name="submit" value="測試">
</form>
<?php
}
else echo("登陸成功,<a href='{$path_bbs}index.php' target=_blank>請到論壇檢查登陸</a><br><a href='?act=logout'>退出登陸</a> ");
?>

第六篇 C#與discuz5.0的整合實例

using System;
using System.Security.Cryptography;
using System.Text;

/// <summary>
/// c#與discuz的passport整合事例
/// 請保留以下信息以備出現問題時使用
/// 整合貢獻:huozhe3136 QQ:2666556
/// 調試工具:Microsoft Visual Web Developer 2005
/// </summary>
public partial class _Default : System.Web.UI.Page {
    //------------------------需要修改的設置---------------------------------
    string webUrl           = "http://localhost:1627/webapp/Default.aspx";//這裡修改為你的測試主站的地址,不要缺少最後的'/'號
    string dzUrl            = "http://localhost:8080/discuz/";//這裡修改為你的dz地址,不要缺少最後的'/'號
    string dzPassportKey    = "1234567890";//這裡修改為你的dz後台通行證設置裡的possportkey
    string dzForward        = "";
    string dzAuth           = "";
    string dzVerify         = "";

 

    protected void Page_Load(object sender, EventArgs e){
        dzForward = Request.QueryString["forward"];
        if (Request.QueryString["act"] == "logout") logout();
        if(Session["username"]!=null) Response.Write(Session["username"].ToString()+ "登陸成功! <a href=\"default.aspx?act=logout\">點擊這裡退出登陸</a> <a href="+dzUrl+" target=_blank>訪問論壇</a><br>");
    }

    protected void logout(){
        Session["username"] = null;
        dzForward           = Request.QueryString["forward"];//獲取返回地址
        if (dzForward == "") dzForward = webUrl;//如果返回地址為空,則返回設置的頁
        dzVerify            = md5("logout" + dzForward + dzPassportKey);
        dzForward           = Server.UrlEncode(dzForward);
        Response.Redirect(dzUrl + "api/passport.php?action=logout&auth=&forward=" + dzForward + "&verify=" + dzVerify);

    }


    protected void Button1_Click(object sender, EventArgs e){
        string postUsername;
        string postPwd;
        postUsername        = username.Text.ToString().Trim();
        postPwd             = password.Text.ToString();

        if(postUsername=="admin" && postPwd=="123456"){
            Session["username"]=postUsername;//設置主站自身的cookie或session;

            //---------------------------passport注冊或登陸整合開始-----------------------------
            string memberInfo = "username=" + postUsername
                        + "&password=" + md5(postPwd)
                        + "&time="+ time()//獲取當前時間與格林威治的秒差
                        + "&email="+"[email protected]";//需要email的值,實際整合通常情況是從數據庫裡獲取用戶email
            dzAuth          = passport_encrypt(memberInfo , dzPassportKey);
            dzForward       = forward.Value.ToString().Trim();//獲取返回地址
            if(dzForward=="") dzForward = webUrl;//如果返回地址為空,則返回設置的頁
            dzVerify        = md5("login" + dzAuth + dzForward + dzPassportKey);
            dzAuth          = Server.UrlEncode(dzAuth);
            dzForward       = Server.UrlEncode(dzForward);
            Response.Redirect(dzUrl + "api/passport.php?action=login&auth=" + dzAuth + "&forward=" + dzForward + "&verify=" + dzVerify);
            //---------------------------passport注冊或登陸整合結束-----------------------------
        }else Response.Write("登陸失敗<br>");
    }


    //------------------------------------以下為一些關鍵函數------------------------------
    // 將字符串轉換為byte數組    
    public byte[] strToByteAry(string str){
        return Encoding.Default.GetBytes(str);
    }

   // c# dz通信證編碼函數
   public string passport_encrypt(string txt, string key){
        byte[] randomNumber = new byte[1];
        RNGCryptoServiceProvider Gen = new RNGCryptoServiceProvider();
        Gen.GetBytes(randomNumber);
        int rand = Convert.ToInt32(randomNumber[0]);
        string encrypt_key = md5(rand.ToString());

        byte[] txtAry = strToByteAry(txt);
        byte[] encryAry = strToByteAry(encrypt_key);
        int j = 0;
        int ctr = 0;
        byte[] tmp = new byte[2 * txtAry.Length];
        for (int i = 0; i < txtAry.Length; i++)
        {
            ctr = ctr == encryAry.Length ? 0 : ctr;
            tmp[j++] = encryAry[ctr];
            tmp[j++] = xor(txtAry[i], encryAry[ctr++]);
        }
        return Convert.ToBase64String(passport_key(tmp, key));
    }

   
    // c# dz通行證解碼函數
    public string passport_decrypt(string txt, string key){
        byte[] t = Convert.FromBase64String(txt);
        byte[] btxt = passport_key(t, key);
        byte[] btmp = new byte[btxt.Length];
        int j = 0;
        for (int i = 0; i < btxt.Length; i++, j++)
            btmp[j] = xor(btxt[i], btxt[++i]);
        return Encoding.Default.GetString(btmp);
    }



    // c# dz通行證密匙處理函數   
    public byte[] passport_key(byte[] btxt, string key){
        key = md5(key);
        byte[] keyAry = strToByteAry(key);
        int ctr = 0;
        byte[] tmp = new byte[btxt.Length];
        for (int i = 0; i < btxt.Length; i++){
            ctr = (ctr == keyAry.Length) ? 0 : ctr;
            tmp[i] = xor(btxt[i], keyAry[ctr++]);
        }
        return tmp;
    }

   
    // 針對byte的異或    
    public byte xor(byte a, byte b){
        return Convert.ToByte(a ^ b);
    }

    // 獲取UNIX時間戳   
    public string time(){
        DateTime dtStart = TimeZone.CurrentTimeZone.ToLocalTime(new DateTime(1970, 1, 1));
        DateTime dtNow = DateTime.Parse(DateTime.Now.ToString());
        TimeSpan toNow = dtNow.Subtract(dtStart);
        string timeStamp = toNow.Ticks.ToString();
        timeStamp = timeStamp.Substring(0, timeStamp.Length - 7);
        return timeStamp;
    }

   
    // md5加密
    public string md5(string str){
        MD5 md5Hasher = MD5.Create();
        string re="";
        byte[] data = md5Hasher.ComputeHash(Encoding.Default.GetBytes(str));
        for (int i = 0; i < data.Length; i++)
            re += data[i].ToString("x2");       
        return re;
    }
}

/tech/uploadfile/200702/20://www.tenwe.com/tech/uploadfile/200702/20070227024435342.rar" target=_blank>/tech/uploadfile/200702/20070227024435342.rar

070227024435342.rar

  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved