php密钥加密
❶ python3的密钥加密和php的密钥加密结果不一样,PHP的代码不变,怎么实现让python3密钥加密后和PHP的一样
明显python代码有误用散列的情况...
将错就错可得php
<?php//ver>=5.1.2
$pkey="91ebf1be3-OptServiceAPI-d580ea24";
$text="admin";
$s=hash('sha256',$pkey.$text);
echo$s;
刚没看到是要py匹配php...
手上没有低版本测,所以这个代码要py 3.7以上才能运行
importhmac
pkey="91ebf1be3-OptServiceAPI-d580ea24";
text="admin";
s=hmac.digest(pkey.encode("UTF-8"),text.encode("UTF-8"),"sha256")
print(s.hex())
❷ PHP SHA256怎么带密钥加解密
SHA256是数据摘要算法,是不可逆的,解不了密,一整本小说加密了剩下256bit 这要能解密不是逆天了
❸ php 怎么生成rsa加密的公钥和私钥
附上出处链接:http://bbs.csdn.net/topics/370014844
四,用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->getMolus()));
}
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 molus
$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加密解决。
不过,对中间人攻击还是没办法。
另外一个
http://www.mingup.cn/php/2011/0121/101568.html
❹ PHP中哪种加密方式好
aes/des加密速度快,适合大量数据,des容易破解,一般用3重des,后来又出现了更快更安全的aes
rsa是公钥加密,速度慢,只能处理少量数据,优点是公钥即使在不安全的网络上公开,也能保证安全
常见情况是双方用rsa协商出一个密钥后通过aes/3des给数据加密。
bcrypt,是一个跨平台的文件加密工具。由它加密的文件可在所有支持的操作系统和处理器上进行转移。它的口令必须是8至56个字符,并将在内部被转化为448位的密钥。
综上所述用bcrypt还是好点,最好用md5安全性高,更多问题到后盾网论坛问题助专区http://bbs.hounwang.com/
❺ php中RSA加密,明文超长,需要分段加密该怎么做
这方面的话我不是很了解,一般来说,加密分为两个部分,一个是非对称加密,一个是对称加密,使用对称加密加密正文信息,使用非对称加密加密对称加密的密钥,然后发送加密数据(消息摘要和数字签名就不讨论了),这是正规的数据加密策略,对称加密默认支持大数据分段加密策略,你只需要从接口中完成加密即可,而且对称加密速度比非对称加密快很多,如果你需要使用这个策略建议使用AES。
如果你不愿意使用对称加密,只愿意使用AES加密,那你就必须丧失速度了,而且自己处理分段加密,因为RSA加密通常是117个字节就要分段(这个长度可能和密钥长度有关,我是用的接口是117),你需要自己把数据变成N个117字节的数据段来完成加密,解密也需要自己完成字节拼装。详细还是建议你去后盾人平台去看看视频教学吧,那里面有的,讲的很清楚。
❻ PHP 加密要怎么搞
如果是不需要可逆的加密,可以用md5(标准密钥长度128位)、sha1(标准密钥长度160位)、md4、CRC-32。这个函数是将字符串变成32个长度的不重复的乱码,多用于存储用户密码。
如果需要可逆的加密,可以使用base64函数,但是容易被人反过来看到原文。复杂一点可以用openssl拓展生成密钥,利用手中的密钥生成。
❼ 关于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*', "");
# 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";
?>
❽ php有哪些需要密匙的加密算法
函数名称:encrypt
函数作用:加密解密字符串
使用方法:
加密 :encrypt('str','E','qingdou');
解密 :encrypt('被加密过的字符串','D','qingdou');
❾ 如何对PHP文件进行加密
超级加密3000就可以
1、下载安装超级加密3000这款软件
2、在这个文件上面右键单击,选择超级加密
3、然后在弹出的密码输入窗口输入需要设置的密码,然后点击确定就可以了
❿ php中aes加密和rsa加密的区别
这个跟php没有关系,单纯的是两个密码学的算法。如果真想搞清楚区别,你需要有密码学的基础知识。
我简单说一下,这两个都是标准的密码学算法,应用广泛。AES是一个对称加密算法,常常用于对数据进行加密,RSA是一个非对称(公钥)加密算法,常常用于对AES加密用的密钥进行加密,或者进行数字签名等。
至于对称加密算法和非对称加密算法的区别说起来就越来越多了。你只要知道以下事实就好:
对称加密算法加解密密钥相同,而非对称加密算法加解密密钥不同
对称加密算法相对于非对称加密算法而言往往加解密速度很快
非对称加密算法具有任何有公钥的人都能加密数据,但是只有有私钥的人才能解密数据的特点