PHP网页设计php下的RSA算法完成
多个成员之间重复做相同的工作,很容易因为交流沟通的时候没有进行一致性的文档要求而出现不明错误,严重影响开发进度,导致在预定时间内无法完成该项目或者完成的项目跟原先计划所要实现的项目功能不符合。 /** 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
* (tested Java/PHP interoperability).
*
* References:
* "Applied Cryptography", Bruce Schneier, John Wiley & Sons, 1996
* "Prime Number Hide-and-Seek", Brian Raiter, Muppetlabs (online)
* "The Bouncy Castle Crypto Package", Legion of the Bouncy Castle,
* (open source cryptography library for Java, online)
* "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 ).
* - 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 )
* - 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 :
// (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
//--
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 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);
}
?>即使你理解不了PHP,但是也必须先跟它混个脸熟,看,一遍遍的看,看的同时一边琢磨,一边按照它所教的打代码,即使你搞不清楚那些代码到底是干嘛的,但是起码你应该找找感觉。 本人接触php时间不长,算是phper中的小菜鸟一只吧。由于刚开始学的时候没有名师指,碰过不少疙瘩,呗很多小问题卡过很久,白白浪费不少宝贵的时间,在次分享一些子的学习的心得。 曾经犯过一个很低级的错误,我在文件命名的时候用了一个横线\\\\\\\'-\\\\\\\' 号,结果找了好几个小时的错误,事实是命名的时候 是不能用横线 \\\\\\\'-\\\\\\\' 的,应该用的是下划线\\\\\\\'_\\\\\\\' ; 这些中手常用的知识,当你把我说的这些关键字都可以熟练运用的时候,你可以选择自己 因为blog这样的可以让你接触更多要学的知识,可以接触用到类,模板,js ,ajax 对于懒惰的朋友,我推荐php的集成环境xampp或者是wamp。这两个软件安装方便,使用简单。但是我还是强烈建议自己动手搭建开发环境。 当留言板完成的时候,下步可以把做1个单人的blog程序,做为目标, 这些中手常用的知识,当你把我说的这些关键字都可以熟练运用的时候,你可以选择自己 微软最近出的新字体“微软雅黑”,虽然是挺漂亮的,不过firefox支持的不是很好,所以能少用还是少用的好。 首推的搜索引擎当然是Google大神,其次我比较喜欢 百度知道。不过搜出来的结果往往都是 大家copy来copy去的,运气的的概率很大。 首推的搜索引擎当然是Google大神,其次我比较喜欢 百度知道。不过搜出来的结果往往都是 大家copy来copy去的,运气的的概率很大。 写js我最烦的就是 ie 和 firefox下同样的代码 结果显示的结果千差万别,还是就是最好不要用遨游去调试,因为有时候遨游是禁用js的,有可能代码是争取结果被遨游折腾的认为是代码写错。 因为blog这样的可以让你接触更多要学的知识,可以接触用到类,模板,js ,ajax 首推的搜索引擎当然是Google大神,其次我比较喜欢 百度知道。不过搜出来的结果往往都是 大家copy来copy去的,运气的的概率很大。 爱上php,他也会爱上你。 有时候汉字的空格也能导致页面出错,所以在写代码的时候,要输入空格最好用引文模式。 遇到出错的时候,我经常把错误信息直接复制到 google的搜索栏,一般情况都是能搜到结果的,不过有时候会搜出来一大片英文的出来,这时候就得过滤一下,吧中文的弄出来,挨着式方法。 至于模板嘛,各位高人一直以来就是争论不休,我一只小菜鸟就不加入战团啦,咱们新手还是多学点东西的好。 我要在声明一下:我是个菜鸟!!我对php这门优秀的语言也是知之甚少。但是我要在这里说一下php在网站开发中最常用的几个功能: 兴趣是最好的老师,百度是最好的词典。
页:
[1]