当前位置:首页 » 密码管理 » encrypt加密php

encrypt加密php

发布时间: 2025-07-11 11:49:38

① 前端使用CryptoJS AES加密 ,后端php解密问题

PHP7.1 已经不能用mcrypt了,所以我用的是openssl_encrypt和openssl_decrypt。

<?php
$data="ThisisanAEScryptdemo.";
$privateKey="";//KEY16字节用aes-128-cbc,32字节用aes-256-cbc
$iv="4490d2ded4f2d4ad";//AES的IV是16个字节

//加密
//$encrypted=openssl_encrypt($data,'aes-128-cbc',$privateKey,0,$iv);
$encrypted=openssl_encrypt($data,'aes-256-cbc',$privateKey,0,$iv);
echo$encrypted,PHP_EOL;

//解密
$encryptedData=$encrypted;
//$decrypted=openssl_decrypt($encryptedData,'aes-128-cbc',$privateKey,0,$iv);
$decrypted=openssl_decrypt($encryptedData,'aes-256-cbc',$privateKey,0,$iv);
echo($decrypted);

输出结果如下:

EPcMQRXA53/hRkPyILFI4fF/9sW2X53tLiDT26khNsA=
ThisisanAEScryptdemo.

② 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加密代码,编码是UTF-8


$key=pack('H*',"");

//显示AES-128,192,256对应的密钥长度:
//16,24,32字节。
$key_size=strlen($key);
echo"Keysize:".$key_size." ";

$plaintext="ThisstringwasAES-256/CBC/ZeroBytePaddingencrypted.";


$iv_size=mcrypt_get_iv_size(MCRYPT_RIJNDAEL_128,MCRYPT_MODE_CBC);
$iv=mcrypt_create_iv($iv_size,MCRYPT_RAND);$ciphertext=mcrypt_encrypt(MCRYPT_RIJNDAEL_128,$key,
$plaintext,MCRYPT_MODE_CBC,$iv);

④ 各位用php将密码存入数据库,都用什么方法进行加密的

php将密码存入数据库,可以分内常见的4种方式:
1、直接md5加密存到到数据库
2、md5两次存到数据库
3、对需要加密的字符串和一个常量 进行混淆加密
4、生成一个随机的变量存到数据库中,然后对需要加密的字符串和这个随机变量加密

<?php$str="admin"; //需要加密的字符串$str2="php"; //增加一个常量混淆 $pass1=md5($str);$pass2=md5(md5($str));$pass3=md5($str.$str2);echo $pass1."<br>".$pass2."<br>".$pass3;?>
输出:

第四种

$str="admin"; //需要加密的字符串$encrypt=$row['encrypt']; // 生成的 随机加密字符串 存到数据库中$pass4=md5($str.$encrypt);//

⑤ 如何用php做AES加密解密,编码是UTF-8,跪谢求代码

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 = 'UITN25LMUQC436IM';
$plainText = 'this is a string will be AES_Encrypt';

$aes = new CryptAES();
$aes->set_key($keyStr);
$aes->require_pkcs5();
$encText = $aes->encrypt($plainText);
$decString = $aes->decrypt($encText);

echo $encText,"n",$decString;*/

⑥ thinkphp MD5加密问题

AUTH_CODE这个参数是自定义的,每个网站的参数都不一样,有些网站为了安全,这个值还是随机数,这样的话,就打打加强了开源程序的安全性。encrypt这个函数在这里就是读取配置文件中的随机数和MD5加密之后的文件再做个二次加密,所以就很安全咯
是否可以解决您的问题?

⑦ 求教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 当中 openssl_private_encrypt 加密的时候 为什么只能加密117个字符的长度的字符串,超过这个长度的字

PHP RSA使用非对称加解密就是 密钥/8 -11的长度。你可以使用AES/DES对称加解密这个不限制长度

热点内容
mysql属于什么数据库 发布:2025-07-12 13:55:52 浏览:166
源码抓捕 发布:2025-07-12 13:47:34 浏览:873
安卓哪里有李小龙 发布:2025-07-12 13:31:49 浏览:439
苹果保存账号密码在哪里找 发布:2025-07-12 13:31:07 浏览:99
东北大学c语言考试题 发布:2025-07-12 13:26:40 浏览:756
sha256在线加密 发布:2025-07-12 13:19:06 浏览:228
vbnet创建数据库连接 发布:2025-07-12 13:15:34 浏览:233
为什么社保卡在社康还要密码 发布:2025-07-12 13:11:42 浏览:812
取随机数php 发布:2025-07-12 12:58:16 浏览:841
如何配置组合音响 发布:2025-07-12 12:53:54 浏览:94