phpaespadding
Ⅰ php AES加密 openssl解密失败,帮忙看下代码哪里有问题
一般来说出问题都是那个padding 配置的问题。你看看你选的模式和这个填充是不是跟加密的配置一致。
Ⅱ php中aes加密和rsa加密的区别
这个跟php没有关系,单纯的是两个密码学的算法。如果真想搞清楚区别,你需要有密码学的基础知识。
我简单说一下,这两个都是标准的密码学算法,应用广泛。AES是一个对称加密算法,常常用于对数据进行加密,RSA是一个非对称(公钥)加密算法,常常用于对AES加密用的密钥进行加密,或者进行数字签名等。
至于对称加密算法和非对称加密算法的区别说起来就越来越多了。你只要知道以下事实就好:
对称加密算法加解密密钥相同,而非对称加密算法加解密密钥不同
对称加密算法相对于非对称加密算法而言往往加解密速度很快
非对称加密算法具有任何有公钥的人都能加密数据,但是只有有私钥的人才能解密数据的特点
Ⅲ php aes ecb pkcs7padding 加密
<?php
class CryptAES
{
protected $cipher = MCRYPT_RIJNDAEL_128;
protected $mode = MCRYPT_MODE_ECB;
protected $pad_method = NULL;
protected $secret_key = '';
protected $iv = '';
public function set_cipher($cipher)
{
$this->cipher = $cipher;
}
public function set_mode($mode)
{
$this->mode = $mode;
}
public function set_iv($iv)
{
$this->iv = $iv;
}
public function set_key($key)
{
$this->secret_key = $key;
}
public function require_pkcs5()
{
$this->pad_method = 'pkcs5';
}
protected function pad_or_unpad($str, $ext)
{
if ( is_null($this->pad_method) )
{
return $str;
}
else
{
$func_name = __CLASS__ . '::' . $this->pad_method . '_' . $ext . 'pad';
if ( is_callable($func_name) )
{
$size = mcrypt_get_block_size($this->cipher, $this->mode);
return call_user_func($func_name, $str, $size);
}
}
return $str;
}
protected function pad($str)
{
return $this->pad_or_unpad($str, '');
}
protected function unpad($str)
{
return $this->pad_or_unpad($str, 'un');
}
public function encrypt($str)
{
$str = $this->pad($str);
$td = mcrypt_mole_open($this->cipher, '', $this->mode, '');
if ( empty($this->iv) )
{
$iv = @mcrypt_create_iv(mcrypt_enc_get_iv_size($td), MCRYPT_RAND);
}
else
{
$iv = $this->iv;
}
mcrypt_generic_init($td, $this->secret_key, $iv);
$cyper_text = mcrypt_generic($td, $str);
$rt=base64_encode($cyper_text);
//$rt = bin2hex($cyper_text);
mcrypt_generic_deinit($td);
mcrypt_mole_close($td);
return $rt;
}
public function decrypt($str){
$td = mcrypt_mole_open($this->cipher, '', $this->mode, '');
if ( empty($this->iv) )
{
$iv = @mcrypt_create_iv(mcrypt_enc_get_iv_size($td), MCRYPT_RAND);
}
else
{
$iv = $this->iv;
}
mcrypt_generic_init($td, $this->secret_key, $iv);
//$decrypted_text = mdecrypt_generic($td, self::hex2bin($str));
$decrypted_text = mdecrypt_generic($td, base64_decode($str));
$rt = $decrypted_text;
mcrypt_generic_deinit($td);
mcrypt_mole_close($td);
return $this->unpad($rt);
}
public static function hex2bin($hexdata) {
$bindata = '';
$length = strlen($hexdata);
for ($i=0; $i<$length;$i += 2)
{
$bindata .= chr(hexdec(substr($hexdata, $i, 2)));
}
return $bindata;
}
public static function pkcs5_pad($text, $blocksize)
{
$pad = $blocksize - (strlen($text) % $blocksize);
return $text . str_repeat(chr($pad), $pad);
}
public static function pkcs5_unpad($text)
{
$pad = ord($text{strlen($text) - 1});
if ($pad > strlen($text)) return false;
if (strspn($text, chr($pad), strlen($text) - $pad) != $pad) return false;
return substr($text, 0, -1 * $pad);
}
}
$keyStr = '187522'; //私钥
$aes = new CryptAES();
$aes->set_key($keyStr);
/* $arr = array('1'=>'aaa1111','2'=>array(1,2));
$bb = serialize($arr);
//echo $aes->encrypt($arr);
$a = $aes->decrypt($aes->encrypt($bb));
$c=unserialize($a); */
?>
Ⅳ 如何在php中实现AES加密
文件加密建议:
1.下载 Abel文件加密器 1.1(软件大小39KB,简体中文,免费版,绿色软件,无须安装。)
iask.tech.sina/ishare/browse_file.php?fileid=87135
2.在非系统盘里建一文件夹,把压缩文件解压其中.
点击文件加密器(AbelLock)图标---出现“文件列表”,点击“添加”---出现打开,在“查找范围”,点"Abel文件加密器 1.1"右面的▲下拉框符号,选择你需加密文件的路经及文件,单击后会出现在“文件名”显示框中,点击“打开”---输入密码.确认密码后点击“加密”,点击退出.
Ⅳ 求教php AES/CBS/PKCS5Padding加密
要实现一个功能, 就是把字符串加密...
有一个java端的加密DEMO, 现在要转换成PHP实现..
但是PHP的得到的结果和JAVA得到的真的是完全不一样....
求大家帮帮忙,,, 看看哪里出问题了... THKS...
下面贴出JAVA端的DEMO:
Java code?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
public class Aes
{
private static byte[] iv = {1, 2, 3, 4, 5, 6, 7, 8, 9, 0, 1, 2, 3, 4, 5, 6};
public static String encrptAesBase64(String encryptString, String encryptKey)
throws Exception
{
if (encryptKey == null)
{
return null;
}
if (encryptString == null)
{
return null;
}
if (encryptKey.length() != 16)
{
return null;
}
IvParameterSpec zeroIv = new IvParameterSpec(iv);
byte[] keys = encryptKey.getBytes("UTF8");
SecretKeySpec key = new SecretKeySpec(keys, "AES");
Cipher cipher = Cipher.
getInstance("AES/CBC/PKCS5Padding");// 算法/模式/补码方式
cipher.init(Cipher.ENCRYPT_MODE, key, zeroIv);
byte[] encryptedData = cipher.doFinal(encryptString.getBytes("UTF8"));
String base64Str = new String(Base64.encodeBase64(encryptedData), "UTF8");
return URLEncoder.encode(base64Str, "utf-8");
}
}
下面贴出PHP的实现:
PHP code?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
<?php
class MagicCrypt
{
private $iv = "1234567890123456" ;
private $encryptKey="abcdefghijklmnop" ;
function encrypt($encryptStr)
{
$localIV = $this->iv ;
$encryptKey = $this->encryptKey ;
//Open mole
$mole = mcrypt_mole_open(MCRYPT_RIJNDAEL_128, '', MCRYPT_MODE_CBC, $localIV) ;
//print "mole = $mole <br/>" ;
mcrypt_generic_init($mole, $encryptKey, $localIV) ;
//Padding
$block = mcrypt_get_block_size(MCRYPT_RIJNDAEL_128, MCRYPT_MODE_CBC) ;
$pad = $block - (strlen($encryptStr) % $block); //Compute how many characters need to pad
$encryptStr .= str_repeat(chr($pad), $pad); // After pad, the str length must be equal to block or its integer multiples
//encrypt
$encrypted = mcrypt_generic($mole, $encryptStr);
//Close
mcrypt_generic_deinit($mole);
mcrypt_mole_close($mole);
return urlencode(base64_encode($encrypted)) ;
}
}
function main() {
$appid = 110 ;
$openid = "abcdefghijklmnop" ;
$appName="应用名称" ;
$encryptKey="abcdefghijklmnop" ;
$param0="abcdefghijklmnop" ;
$encryptString= "openId=" . $openid . "&appName=" . $appName . "¶m0=" . $param0 ;
$encryptObj = new MagicCrypt() ;
$result = $encryptObj->encrypt($encryptString) ;
print "hello new2 php result = $result <br/>" ;
$result = urlencode($result) ;
print "hello new3 php result = $result <br/> " ;
}
main();
?>
Ⅵ php AES加密对不上java的加密,请问如何实现
要注意特定的Padding实现跟算法的blockSize有关,这里php的blocksize是在php的aes加密前先对源字符串进行Padding,问题得到解决。
Ⅶ PHP中哪种加密方式好
aes/des加密速度快,适合大量数据,des容易破解,一般用3重des,后来又出现了更快更安全的aes
rsa是公钥加密,速度慢,只能处理少量数据,优点是公钥即使在不安全的网络上公开,也能保证安全
常见情况是双方用rsa协商出一个密钥后通过aes/3des给数据加密。
bcrypt,是一个跨平台的文件加密工具。由它加密的文件可在所有支持的操作系统和处理器上进行转移。它的口令必须是8至56个字符,并将在内部被转化为448位的密钥。
综上所述用bcrypt还是好点,最好用md5安全性高,更多问题到后盾网论坛问题助专区http://bbs.hounwang.com/
Ⅷ php aes加密~呢
AES加密算法
密码学中的高级加密标准(AdvancedEncryptionStandard,AES),又称Rijndael加密法,是美国联邦政府采用的一种区块加密标准。这个标准用来替代原先的DES,已经被多方分析且广为全世界所使用。
<?php
classCryptAES
{
protected$cipher=MCRYPT_RIJNDAEL_128;
protected$mode=MCRYPT_MODE_ECB;
protected$pad_method=NULL;
protected$secret_key='';
protected$iv='';
publicfunctionset_cipher($cipher)
{
$this->cipher=$cipher;
}
publicfunctionset_mode($mode)
{
$this->mode=$mode;
}
publicfunctionset_iv($iv)
{
$this->iv=$iv;
}
publicfunctionset_key($key)
{
$this->secret_key=$key;
}
publicfunctionrequire_pkcs5()
{
$this->pad_method='pkcs5';
}
protectedfunctionpad_or_unpad($str,$ext)
{
if(is_null($this->pad_method))
{
return$str;
}
else
{
$func_name=__CLASS__.'::'.$this->pad_method.'_'.$ext.'pad';
if(is_callable($func_name))
{
$size=mcrypt_get_block_size($this->cipher,$this->mode);
returncall_user_func($func_name,$str,$size);
}
}
return$str;
}
protectedfunctionpad($str)
{
return$this->pad_or_unpad($str,'');
}
protectedfunctionunpad($str)
{
return$this->pad_or_unpad($str,'un');
}
publicfunctionencrypt($str)
{
$str=$this->pad($str);
$td=mcrypt_mole_open($this->cipher,'',$this->mode,'');
if(empty($this->iv))
{
$iv=@mcrypt_create_iv(mcrypt_enc_get_iv_size($td),MCRYPT_RAND);
}
else
{
$iv=$this->iv;
}
mcrypt_generic_init($td,hex2bin($this->secret_key),$iv);
$cyper_text=mcrypt_generic($td,$str);
$rt=strtoupper(bin2hex($cyper_text));
mcrypt_generic_deinit($td);
mcrypt_mole_close($td);
return$rt;
}
publicfunctiondecrypt($str){
$td=mcrypt_mole_open($this->cipher,'',$this->mode,'');
if(empty($this->iv))
{
$iv=@mcrypt_create_iv(mcrypt_enc_get_iv_size($td),MCRYPT_RAND);
}
else
{
$iv=$this->iv;
}
mcrypt_generic_init($td,$this->secret_key,$iv);
//$decrypted_text=mdecrypt_generic($td,self::hex2bin($str));
$decrypted_text=mdecrypt_generic($td,base64_decode($str));
$rt=$decrypted_text;
mcrypt_generic_deinit($td);
mcrypt_mole_close($td);
return$this->unpad($rt);
}
publicstaticfunctionhex2bin($hexdata){
$bindata='';
$length=strlen($hexdata);
for($i=0;$i<$length;$i+=2)
{
$bindata.=chr(hexdec(substr($hexdata,$i,2)));
}
return$bindata;
}
publicstaticfunctionpkcs5_pad($text,$blocksize)
{
$pad=$blocksize-(strlen($text)%$blocksize);
return$text.str_repeat(chr($pad),$pad);
}
publicstaticfunctionpkcs5_unpad($text)
{
$pad=ord($text{strlen($text)-1});
if($pad>strlen($text))returnfalse;
if(strspn($text,chr($pad),strlen($text)-$pad)!=$pad)returnfalse;
returnsubstr($text,0,-1*$pad);
}
}
//密钥
$keyStr='';
//加密的字符串
$plainText='test';
$aes=newCryptAES();
$aes->set_key($keyStr);
$aes->require_pkcs5();
$encText=$aes->encrypt($plainText);
echo$encText;
?>
Ⅸ PHP如何实现AES加解密
php加载Mcrypt组件php_mycrypt.dll/.so,支持AES和3DES编码,
只是该模块没有提供补齐padding方法,要自己用PHP代码写PKCS7之类的补齐方法
Ⅹ php rsa/no/padding加密怎么实现
Java和PHP RSA加密实现互通
1:通过openssl 生成公钥和密钥文件(Linux)
(1) 生产私钥文件命令
openssl genrsa -out rsa_private_key.pem 1024
(2) 通过私钥文件生成公钥命令
openssl rsa -in rsa_private_key.pem -out rsa_public_key.pem -pubout
(3) 将传统格式的私钥转换成 PKCS#8 格式的的密钥文件
openssl pkcs8 -topk8 -in rsa_private_key.pem -outpkcs8_rsa_private_key.pem -nocrypt