亚洲香蕉成人av网站在线观看_欧美精品成人91久久久久久久_久久久久久久久久久亚洲_热久久视久久精品18亚洲精品_国产精自产拍久久久久久_亚洲色图国产精品_91精品国产网站_中文字幕欧美日韩精品_国产精品久久久久久亚洲调教_国产精品久久一区_性夜试看影院91社区_97在线观看视频国产_68精品久久久久久欧美_欧美精品在线观看_国产精品一区二区久久精品_欧美老女人bb

首頁 > 學院 > 邏輯算法 > 正文

php下的RSA算法實現

2024-09-08 23:18:44
字體:
來源:轉載
供稿:網友

/*
* implementation of the rsa algorithm
* (c) copyright 2004 edsko de vries, ireland
*
* licensed under the gnu public license (gpl)
*
* this implementation has been verified against [3]
* (tested java/php interoperability).
*
* references:
* [1] "applied cryptography", bruce schneier, john wiley & sons, 1996
* [2] "prime number hide-and-seek", brian raiter, muppetlabs (online)
* [3] "the bouncy castle crypto package", legion of the bouncy castle,
* (open source cryptography library for java, online)
* [4] "pkcs #1: rsa encryption standard", rsa laboratories technical note,
* version 1.5, revised november 1, 1993

*/

/*
* functions that are meant to be used by the user of this php module.
*
* notes:
* - $key and $modulus should be numbers in (decimal) string format
* - $message is expected to be binary data
* - $keylength should be a multiple of 8, and should be in bits
* - for rsa_encrypt/rsa_sign, the length of $message should not exceed
* ($keylength / 8) - 11 (as mandated by [4]).
* - rsa_encrypt and rsa_sign will automatically add padding to the message.
* for rsa_encrypt, this padding will consist of random values; for rsa_sign,
* padding will consist of the appropriate number of 0xff values (see [4])
* - rsa_decrypt and rsa_verify will automatically remove message padding.
* - blocks for decoding (rsa_decrypt, rsa_verify) should be exactly
* ($keylength / 8) bytes long.
* - rsa_encrypt and rsa_verify expect a public key; rsa_decrypt and rsa_sign
* expect a private key.

*/

function rsa_encrypt($message, $public_key, $modulus, $keylength)
{

    $padded = add_pkcs1_padding($message, true, $keylength / 8);
    $number = binary_to_number($padded);
    $encrypted = pow_mod($number, $public_key, $modulus);
    $result = number_to_binary($encrypted, $keylength / 8);
    
    return $result;
}


function rsa_decrypt($message, $private_key, $modulus, $keylength)
{

    $number = binary_to_number($message);
    $decrypted = pow_mod($number, $private_key, $modulus);
    $result = number_to_binary($decrypted, $keylength / 8);

    return remove_pkcs1_padding($result, $keylength / 8);
}


function rsa_sign($message, $private_key, $modulus, $keylength)
{

    $padded = add_pkcs1_padding($message, false, $keylength / 8);
    $number = binary_to_number($padded);
    $signed = pow_mod($number, $private_key, $modulus);
    $result = number_to_binary($signed, $keylength / 8);

    return $result;
}


function rsa_verify($message, $public_key, $modulus, $keylength)
{

    return rsa_decrypt($message, $public_key, $modulus, $keylength);
}


/*
* some constants

*/

define("bccomp_larger", 1);

/*
* the actual implementation.
* requires bcmath support in php (compile with --enable-bcmath)

*/

//--
// calculate (p ^ q) mod r
//
// we need some trickery to [2]:
// (a) avoid calculating (p ^ q) before (p ^ q) mod r, because for typical rsa
// applications, (p ^ q) is going to be _way_ too large.
// (i mean, __way__ too large - won't fit in your computer's memory.)
// (b) still be reasonably efficient.
//
// we assume p, q and r are all positive, and that r is non-zero.
//
// note that the more simple algorithm of multiplying $p by itself $q times, and
// applying "mod $r" at every step is also valid, but is o($q), whereas this
// algorithm is o(log $q). big difference.
//
// as far as i can see, the algorithm i use is optimal; there is no redundancy
// in the calculation of the partial results.
//--

function pow_mod($p, $q, $r)
{

    // extract powers of 2 from $q
$factors = array();
    $div = $q;
    $power_of_two = 0;
    while(bccomp($div, "0") == bccomp_larger)
    {

        $rem = bcmod($div, 2);
        $div = bcdiv($div, 2);
    
        if($rem) array_push($factors, $power_of_two);
        $power_of_two++;
    }


    // calculate partial results for each factor, using each partial result as a
    // starting point for the next. this depends of the factors of two being
    // generated in increasing order.

$partial_results = array();
    $part_res = $p;
    $idx = 0;
    foreach($factors as $factor)
    {

        while($idx < $factor)
        {

            $part_res = bcpow($part_res, "2");
            $part_res = bcmod($part_res, $r);

            $idx++;
        }
        
        array_pus(
$partial_results, $part_res);
    }


    // calculate final result
$result = "1";
    foreach($partial_results as $part_res)
    {

        $result = bcmul($result, $part_res);
        $result = bcmod($result, $r);
    }


    return $result;
}


//--
// function to add padding to a decrypted string
// we need to know if this is a private or a public key operation [4]
//--

function add_pkcs1_padding($data, $ispublickey, $blocksize)
{

    $pad_length = $blocksize - 3 - strlen($data);

    if($ispublickey)
    {

        $block_type = "/x02";
    
        $padding = "";
        for($i = 0; $i < $pad_length; $i++)
        {

            $rnd = mt_rand(1, 255);
            $padding .= chr($rnd);
        }
    }

    else
    {

        $block_type = "/x01";
        $padding = str_repeat("/xff", $pad_length);
    }

    
    return "/x00" . $block_type . $padding . "/x00" . $data;
}


//--
// remove padding from a decrypted string
// see [4] for more details.
//--

function remove_pkcs1_padding($data, $blocksize)
{

    assert(strlen($data) == $blocksize);
    $data = substr($data, 1);

    // we cannot deal with block type 0
if($data{0} == '/0')
        die("block type 0 not implemented.");

    // then the block type must be 1 or 2
assert(($data{0} == "/x01") || ($data{0} == "/x02"));

    // remove the padding
$offset = strpos($data, "/0", 1);
    return substr($data, $offset + 1);
}


//--
// convert binary data to a decimal number
//--

function binary_to_number($data)
{

    $base = "256";
    $radix = "1";
    $result = "0";

    for($i = strlen($data) - 1; $i >= 0; $i--)
    {

        $digit = ord($data{$i});
        $part_res = bcmul($digit, $radix);
        $result = bcadd($result, $part_res);
        $radix = bcmul($radix, $base);
    }


    return $result;
}


//--
// convert a number back into binary form
//--

function number_to_binary($number, $blocksize)
{

    $base = "256";
    $result = "";

    $div = $number;
    while($div > 0)
    {

        $mod = bcmod($div, $base);
        $div = bcdiv($div, $base);
        
        $result = chr($mod) . $result;
    }


    return str_pad($result, $blocksize, "/x00", str_pad_left);
}

?>
發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
亚洲香蕉成人av网站在线观看_欧美精品成人91久久久久久久_久久久久久久久久久亚洲_热久久视久久精品18亚洲精品_国产精自产拍久久久久久_亚洲色图国产精品_91精品国产网站_中文字幕欧美日韩精品_国产精品久久久久久亚洲调教_国产精品久久一区_性夜试看影院91社区_97在线观看视频国产_68精品久久久久久欧美_欧美精品在线观看_国产精品一区二区久久精品_欧美老女人bb
色av中文字幕一区| 日韩国产一区三区| 欧美人与性动交| 97视频在线观看免费高清完整版在线观看| 中文欧美日本在线资源| 亚洲精品久久久久久久久| 日本三级久久久| 亚洲一级黄色片| 亚洲综合最新在线| 日韩久久精品成人| 98视频在线噜噜噜国产| 欧美成年人视频网站欧美| 亚洲人av在线影院| 亚洲欧洲自拍偷拍| 中文字幕亚洲一区在线观看| 成人国产在线激情| 亚洲毛片在线看| 国产精品成人av性教育| 欧美超级免费视 在线| 亚洲成人1234| 欧美一级电影在线| 91久久精品国产91久久| 欧美日韩一区二区免费视频| 欧美视频二区36p| 国产精品第1页| 日韩高清不卡av| 亚洲精品小视频在线观看| 日韩欧美综合在线视频| 日韩av在线一区| 亚洲欧美一区二区三区四区| 欧美日韩激情美女| 国产精品高清网站| 久久久av亚洲男天堂| 久久久噜噜噜久久中文字免| 久久精品亚洲精品| 亚洲欧洲av一区二区| 色婷婷综合成人av| 日韩中文字幕在线视频| 久久视频在线观看免费| 2019中文字幕在线| 97在线观看免费高清| 亚洲天堂av在线播放| 欧美成人免费小视频| 欧美猛交免费看| 国产精品久久久久久久久男| 久久久久久久电影一区| 欧美日韩在线第一页| 精品伊人久久97| 国产日韩精品视频| 午夜精品三级视频福利| 精品国产欧美成人夜夜嗨| 国产精品无av码在线观看| 欧美一区二区色| 91av在线国产| 欧美大片免费观看在线观看网站推荐| 91在线观看免费高清完整版在线观看| 亚洲激情免费观看| 欧美日韩国产综合视频在线观看中文| 91在线视频精品| 一本色道久久88综合日韩精品| 97精品国产91久久久久久| 国产999精品久久久影片官网| 亚洲免费福利视频| 中文国产成人精品| 992tv在线成人免费观看| 欧美日产国产成人免费图片| 久久久午夜视频| 亚洲综合精品伊人久久| 国产专区欧美专区| 欧美在线xxx| 91香蕉国产在线观看| 欧美老妇交乱视频| 欧美电影免费观看| 久久综合九色九九| 福利视频第一区| 日韩美女主播视频| 欧美精品第一页在线播放| 国产日韩在线看片| 亚洲综合色av| 欧美黑人巨大精品一区二区| 欧美性猛交xxxx| 国产精品盗摄久久久| 欧美激情极品视频| 97精品国产97久久久久久免费| 欧美激情中文字幕在线| 国产精品久久久久久久久久东京| 日韩成人免费视频| 国产福利精品在线| 日产精品99久久久久久| 亚洲欧洲中文天堂| 亚洲成人精品视频| 国产一级揄自揄精品视频| www.99久久热国产日韩欧美.com| 在线色欧美三级视频| 久久久精品国产网站| 亚洲v日韩v综合v精品v| 欧美在线视频a| 深夜福利91大全| 777午夜精品福利在线观看| 2019亚洲日韩新视频| 亚洲一区二区三区视频| 国产一区二中文字幕在线看| 久久躁日日躁aaaaxxxx| 成人久久一区二区三区| 久久精品视频播放| 久久久久免费精品国产| 成人国内精品久久久久一区| 国产精品69精品一区二区三区| 国产精品一区二区三区成人| 欧美精品videos另类日本| 久久av在线看| 中文字幕欧美日韩在线| 国产成人福利夜色影视| 九色精品美女在线| 最近日韩中文字幕中文| 青青草一区二区| 北条麻妃99精品青青久久| 日产日韩在线亚洲欧美| 久久久之久亚州精品露出| 欧美日韩色婷婷| 国产精自产拍久久久久久蜜| 69精品小视频| 视频直播国产精品| 午夜美女久久久久爽久久| 欧美高清视频免费观看| 97精品视频在线播放| 亚洲二区在线播放视频| 亚洲精品在线看| 日韩一区二区三区xxxx| 欧美黑人极品猛少妇色xxxxx| 久久99久久久久久久噜噜| 欧美怡红院视频一区二区三区| 92看片淫黄大片欧美看国产片| 久久国产视频网站| 欧美亚州一区二区三区| 久久亚洲国产精品成人av秋霞| 国产精品视频一区二区三区四| 日韩有码在线视频| 欧美福利视频网站| 色综合久综合久久综合久鬼88| 国产精品99久久99久久久二8| 亚洲美女中文字幕| 日韩av大片在线| 日韩欧美亚洲成人| 91精品久久久久久久久青青| 成人国产精品一区| 日本免费在线精品| 久久精品中文字幕一区| 久久久国产精品x99av| 色偷偷av一区二区三区乱| 成人精品aaaa网站| 亚洲女人天堂色在线7777| 成人a在线观看| 美女久久久久久久久久久| 久久精品久久久久电影| 17婷婷久久www| 久久久久久久久久久成人| 日韩免费av片在线观看| www国产精品com| 欧美在线视频免费| 国产日韩在线看片| 日本成人精品在线| 国产99久久精品一区二区永久免费| 亚洲天堂av高清|