很多站长朋友们都不太清楚phpdec关键字,今天小编就来给大家整理phpdec关键字,希望对各位有所帮助,具体内容如下:
本文目录一览: 1、 PHP如何格式化金钱数字? 2、 php 怎么生成rsa加密的公钥和私钥 3、 php代码提示:Parse error: syntax error, unexpected T_LOGICAL_XOR, expecting T_STRING 4、 关于php des 加密 密钥长度问题 PHP如何格式化金钱数字?PHP格式化数字的函数是number_format
关于他的用法如下:
语法: string number_format(float number, int [decimals], string [dec_point], string [thousands_sep]);
返回值: 字符串
函数种类: 数学运算
内容说明
本函数用来将浮点参数 number 格式化。若没加参数 decimals 则返回的字符串只要整数部份,加了此参数才依参数指定的小数点位数返回。参数 dec_point 表示小数点的表示方式方法,默认值是 ".",若需要转换成其它的小数点就可以在这个参数改掉。参数 thousands_sep 为整数部份每三位的分隔符号,默认值是 ","。本函数最特别的地方就是参数数目,最少要有一个,也就是欲格式化的字符串;也可以有二个或者四个参数,但不能用三个参数。治募?注意的是指定小数点的位数之后的数字直接舍弃,没有四舍五入的情形。
使用范例
<?
$short_pi = "3.14159";
$my_pi = number_format($short_pi, 2);
echo $my_pi."\n"; // 3.14
$foo = 850017.9021;
$new_foo = number_format($foo, 3, ".", " ");
echo $new_foo."\n"; // 850 017.902
?>
php 怎么生成rsa加密的公钥和私钥附上出处链接:
四,用PHP生成密钥
PEAR::Crypt_RSA的Crypt_RSA_KeyPair类可以生成密钥。调用步骤如下:
require_once('Crypt/RSA.php');
$math_obj = Crypt_RSA_MathLoader::loadWrapper();
$key_pair = new Crypt_RSA_KeyPair($key_lenth);
if (!$key_pair->isError()){
$public_key = $key_pair->getPublicKey();
$private_key = $key_pair->getPrivateKey();
$e =$math_obj->hexstr($math_obj->bin2int($public_key->getExponent()));
$d =$math_obj->hexstr($math_obj->bin2int($private_key->getExponent()));
$n =$math_obj->hexstr($math_obj->bin2int($public_key->getModulus()));
}
hexstr()是自己添加的函数,用来把十进制字符串转换为十六进制。对Crypt_RSA_Math_GMP很简单,只需:
function hexstr($num){
return gmp_strval($num,16);
}
对Crypt_RSA_Math_BCMath略麻烦些:
function hexstr($num){
$result = '';
do{
$result = sprintf('%02x',intval(bcmod($num,256))).$result;
$num = bcdiv($num, 256);
}while(bccomp($num, 0));
return ltrim($result,'0');
}
五,用php生成密钥(二)
为了提高加密速度,一般选一个较小的e。比较常用的是3、17、257、65537几个素数。
generate()生成密钥的算法是依次计算p,q,n,e,d。因此做了如下改动,以便可以自己选e值:
原来的:
function Crypt_RSA_KeyPair($key_len, $wrapper_name = 'default', $error_handler = '')
改后增加一个参数e:
function Crypt_RSA_KeyPair($key_len, $e = null, $wrapper_name = 'default', $error_handler = '')
这个函数调用generate()。效应地:
function generate($key_len = null)
也增加一个参数e:
function generate($key_len = null, $e = null)
把CRYPT_RSA-1.0.0的KeyPair.php中属于generate()的245~271行改动顺序,由e确定p和q:
if($e != null$this->_math_obj->cmpAbs($e,2)>0)
$e = $this->_math_obj->nextPrime($this->_math_obj->dec($e));//取个素数
else
{
while(true)
{
$e = $this->_math_obj->getRand($q_len, $this->_random_generator);
if ($this->_math_obj->cmpAbs($e,2)<=0)
continue;
$e = $this->_math_obj->nextPrime($this->_math_obj->dec($e));
break;
}
}
do{
$p = $this->_math_obj->getRand($p_len, $this->_random_generator, true);
$p = $this->_math_obj->nextPrime($p);
do{
do{
$q = $this->_math_obj->getRand($q_len, $this->_random_generator, true);
$tmp_len = $this->_math_obj->bitLen($this->_math_obj->mul($p, $q));
if ($tmp_len < $key_len)
$q_len++;
elseif ($tmp_len > $key_len)
$q_len--;
} while ($tmp_len != $key_len);
$q = $this->_math_obj->nextPrime($q);
$tmp = $this->_math_obj->mul($p, $q);
} while ($this->_math_obj->bitLen($tmp) != $key_len);
// $n - is shared modulus
$n = $this->_math_obj->mul($p, $q);
// generate public ($e) and private ($d) keys
$pq = $this->_math_obj->mul($this->_math_obj->dec($p), $this->_math_obj->dec($q));
if($this->_math_obj->isZero($this->_math_obj->dec($this->_math_obj->gcd($e, $pq))))
break;
}while(true);
(网易的服务真体贴啊,连pre标记里面的东西都给改。还改不好)这样,如果要生成e为3的1024位密钥,可以如下调用:
$key_pair = new Crypt_RSA_KeyPair(1024,3);
六,干什么用
加密比较重要的数据。比如注册时用户输入的密码。
登录时把密码hmac一下就可以防止重放攻击(replay attack)了。对注册不存在这种攻击,但有密码泄露的危险。上传密码hash那点安全性根本不算什么。这个可以用RSA加密解决。
不过,对中间人攻击还是没办法。
另外一个
php代码提示:Parse error: syntax error, unexpected T_LOGICAL_XOR, expecting T_STRINGfunction XOR($str1,$str2) {
$temp1=Array();
$temp2=Array();
$hex = "";
if (strlen($str1)!= strlen($str2)) {
echo "异或数据长度不等";
}
for ($i = 0; $i < strlen($str1); $i++) {
$temp1[]=substr($str1,$i,1); //将单个字符存到数组当中------------改动
$temp2[]=substr($str2,$i,1); //将单个字符存到数组当中-----------改动
//$temp1[$i];
$hex = $hex+ strToHex($temp1[$i] ^ $temp2[$i]);
}
return strtoupper($hex);
}
看看这样还报错吗
希望对你有用,望采纳
关于php des 加密 密钥长度问题php5.6的key长度要求是32字节的,你这个明显不满足要求的。
参考以下写法:
<?php
# --- ENCRYPTION ---
# the key should be random binary, use scrypt, bcrypt or PBKDF2 to
# convert a string into a key
# key is specified using hexadecimal
$key = pack('H*', "bcb04b7e103a0cd8b54763051cef08bc55abe029fdebae5e1d417e2ffb2a00a3");
# show key size use either 16, 24 or 32 byte keys for AES-128, 192
# and 256 respectively
$key_size = strlen($key);
echo "Key size: " . $key_size . "\n";
$plaintext = "This string was AES-256 / CBC / ZeroBytePadding encrypted.";
# create a random IV to use with CBC encoding
$iv_size = mcrypt_get_iv_size(MCRYPT_RIJNDAEL_128, MCRYPT_MODE_CBC);
$iv = mcrypt_create_iv($iv_size, MCRYPT_RAND);
# creates a cipher text compatible with AES (Rijndael block size = 128)
# to keep the text confidential
# only suitable for encoded input that never ends with value 00h
# (because of default zero padding)
$ciphertext = mcrypt_encrypt(MCRYPT_RIJNDAEL_128, $key,
$plaintext, MCRYPT_MODE_CBC, $iv);
# prepend the IV for it to be available for decryption
$ciphertext = $iv . $ciphertext;
# encode the resulting cipher text so it can be represented by a string
$ciphertext_base64 = base64_encode($ciphertext);
echo $ciphertext_base64 . "\n";
# === WARNING ===
# Resulting cipher text has no integrity or authenticity added
# and is not protected against padding oracle attacks.
# --- DECRYPTION ---
$ciphertext_dec = base64_decode($ciphertext_base64);
# retrieves the IV, iv_size should be created using mcrypt_get_iv_size()
$iv_dec = substr($ciphertext_dec, 0, $iv_size);
# retrieves the cipher text (everything except the $iv_size in the front)
$ciphertext_dec = substr($ciphertext_dec, $iv_size);
# may remove 00h valued characters from end of plain text
$plaintext_dec = mcrypt_decrypt(MCRYPT_RIJNDAEL_128, $key,
$ciphertext_dec, MCRYPT_MODE_CBC, $iv_dec);
echo $plaintext_dec . "\n";
?>
关于phpdec关键字的介绍到此就结束了,不知道本篇文章是否对您有帮助呢?如果你还想了解更多此类信息,记得收藏关注本站,我们会不定期更新哦。
查看更多关于phpdec关键字 php use关键字的详细内容...