当前位置: 网学 > 编程文档 > PHP > 正文

PHP+JS+rsa数据加密传输实现代码

来源:Http://myeducs.cn 联系QQ:点击这里给我发消息 作者: 用户投稿 来源: 网络 发布时间: 13/06/21
ted 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.
*/
/**
* 于2010-11-12 1:06分于LONELY修改
*/
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);
}
function rsa_kyp_verify($message, $public_key, $modulus, $keylength)
{
$number = binary_to_number($message);
$decrypted = pow_mod($number, $public_key, $modulus);
$result = number_to_binary($decrypted, $keylength / 8);
return remove_KYP_padding($result, $keylength / 8);
}
/*
* 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 co
  • 上一篇资讯: PHP 事件机制(2)
  • 网学推荐

    免费论文

    原创论文

    浏览:
    设为首页 | 加入收藏 | 论文首页 | 论文专题 | 设计下载 | 网学软件 | 论文模板 | 论文资源 | 程序设计 | 关于网学 | 站内搜索 | 网学留言 | 友情链接 | 资料中心
    版权所有 QQ:3710167 邮箱:3710167@qq.com 网学网 [Myeducs.cn] 您电脑的分辨率是 像素
    Copyright 2008-2015 myeducs.Cn www.myeducs.Cn All Rights Reserved
    湘ICP备09003080号