喜悦国际村 
» 游客:  注册 | 登录 | 搜索 | 统计 | 喜悦证交所 | 帮助

RSS 订阅当前论坛  

[北京]代朋友公司招聘PHP高级程序员多名

上一主题 下一主题
     
标题: RC4 Encryption in PHP  
 
millken
高级会员
Rank: 4


UID 73040
精华 0
积分 736
帖子 479
金钱 736 喜悦币
威望 0
人脉 0
阅读权限 50
注册 2006-5-9
来自 REFERER
状态 离线
RC4 Encryption in PHP

What is RC4 ?
RC4 is fairly fast, secure and symmetric encryption algorithm. Developed by Ron Rivest in 1987 was kept trade secret until 9th September 1994 when it was posted on a Cypherpunks mailing list. Generally the key it uses is limited to 40 bits for various legal reasons but 128bits is the more common forms these days. To prove it's strength products like Oracle Secure SQL are examples. It's symmetric meaning it uses the same key and steps as to encrypt when decrypting.

Well there's nothing much to explain in the article, except for guidelines:
(1) The encrypt function is the function used both for encryption
(2) The decrypt function is used for decryption and is identical to encrypt()
(2) The tricky part is submitting data to the function, which needs to be urlencoded and urldecoded, as is required to output to the browser.
class.rc4crypt.php

<?php

/*  *****************************************************************************
**
**  RC4Crypt 2.1
**
**  $Id: class.rc4crypt.php,v 2.1 2004/05/03 07:10:41 Owner Exp Owner $
**
**  2004/04/28 -- Bug fix for incorrect output due to XORing stupidity
**                [Ariel Arjona ([email]beerfrick@users.sourceforge.net[/email])]
**  2003/11/15 -- Cleans urlencode's, fixed the invalid outputs
**  2003/02/22 -- A fix, which turned out to be a bug!
**  2001/02/24 -- Passes RC4 Vector Harness
**
**  Website     : [url]http://www.devhome.org[/url]
**  Email       : [email]god@mjsabby.com[/email]
**  Description : Provides the main functionality of the program
**
**  Copyright Notice:
**
**  RC4 is a registered trademark of the RSA Data Security Inc.
**  This is an implementation of the original algorithm. The author
**  of this program is NOT the original publisher of this algorithm.
**
**  (C) Copyright 2003, 2004 Mukul Sabharwal [[url]http://mjsabby.com[/url]]
**  All Rights Reserved
**
**  This program is free software; you can redistribute it and/or
**  modify it under the terms of the GNU General Public License
**  as published by the Free Software Foundation; either version 2
**  of the License, or (at your option) any later version.
**
**  This program is distributed in the hope that it will be useful,
**  but WITHOUT ANY WARRANTY; without even the implied warranty of
**  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
**  GNU General Public License for more details.
**
**  The GNU General Public License can be found at
**  [url]http://www.gnu.org/copyleft/gpl.html[/url]
**
**  You should have received a copy of the GNU General Public License
**  along with this program; if not, write to the Free Software
**  Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
**
**  ****************************************************************************/

class rc4crypt {

    function 
encrypt ($pwd$data)
    {
        
$key[] = '';
        
$box[] = '';

        
$pwd_length strlen($pwd);
        
$data_length strlen($data);

        for (
$i 0$i 256$i++)
        {
            
$key[$i] = ord($pwd[$i $pwd_length]);
            
$box[$i] = $i;
        }

        for (
$j $i 0$i 256$i++)
        {
            
$j = ($j $box[$i] + $key[$i]) % 256;
            
$tmp $box[$i];
            
$box[$i] = $box[$j];
            
$box[$j] = $tmp;
        }

        for (
$a $j $i 0$i $data_length$i++)
        {
            
$a = ($a 1) % 256;
            
$j = ($j $box[$a]) % 256;

            
$tmp $box[$a];
            
$box[$a] = $box[$j];
            
$box[$j] = $tmp;

            
$k $box[(($box[$a] + $box[$j]) % 256)];
            
$cipher .= chr(ord($data[$i]) ^ $k);

        }

        return 
$cipher;

    }

    function 
decrypt ($pwd$data)
    {
        return 
$this->encrypt($pwd$data);
    }
}

?>
test.php

<?php
include("./class.rc4crypt.php");

if (
strcmp('4.1.0'phpversion()) > 0) {
    global 
$HTTP_GET_VARS;
    
$_GET = &$HTTP_GET_VARS;
}

$pwd $_GET['pwd'];
$data $_GET['data'];

if (
get_magic_quotes_gpc()) {
    
$pwd stripslashes($pwd);
    
$data stripslashes($data);
}

$rc4 = new rc4crypt();

if (!
$_GET['decryption']) {

        
$e $rc4->encrypt($pwd$data);
        echo 
urlencode($e).'<br/><br/>';

        
$d $rc4->decrypt($pwd$e);
        echo 
$d;
} else {
    echo 
$rc4->decrypt($pwdurldecode($data));
}
?>




Blogger 免费搭建的个人BLOG
2007-1-1 11:29 PM#1
查看资料  Blog  发短消息  顶部
 
millken
高级会员
Rank: 4


UID 73040
精华 0
积分 736
帖子 479
金钱 736 喜悦币
威望 0
人脉 0
阅读权限 50
注册 2006-5-9
来自 REFERER
状态 离线
[推荐阅读] 关联不上MYSQL了。。。急!
DEMO:http://devhome.org/demos/rc4crypt/



Blogger 免费搭建的个人BLOG
2007-1-1 11:33 PM#2
查看资料  Blog  发短消息  顶部
 
LINUXPHP.NET
注册会员
Rank: 2
初级会员



UID 30562
精华 0
积分 92
帖子 118
金钱 92 喜悦币
威望 0
人脉 0
阅读权限 20
注册 2004-8-17
来自 LINUXPHP.NET
状态 离线
[推荐阅读] 关联不上MYSQL了。。。急!
标准么? 我用另外的 RC4 加密的 在你这里解密不了。
2007-10-31 06:16 PM#3
查看资料  访问主页  Blog  发短消息  QQ  Yahoo!  顶部
     


  可打印版本 | 推荐给朋友 | 订阅主题 | 收藏主题 | 开通个人空间  


 




Powered by Discuz! 6.1.0  © 2001-2010 Comsenz Inc.
Processed in 0.027796 second(s), 6 queries

(冀ICP备05009913号) 管理员:sadly 邮箱/MSN: sadly@phpx.com QQ:824008(长隐) 清除 Cookies - - Archiver - WAP