在PHP中加密数据的最佳方法 [英] Best Way To Encrypt Data In PHP

查看:100
本文介绍了在PHP中加密数据的最佳方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

HY!

可以任何人给我一个想法,这是加密用户数据的最佳和最新方式,并在我们想要的时候解密它吗?

解决方案

您好,



您可以使用 mcrypt_encrypt mcrypt_decrypt 函数(PHP 4> = 4.0.2,PHP 5):

http://php.net/manual/en/function.mcrypt-encrypt.php [ ^ ]

http://php.net/manual/en/function.mcrypt-decrypt.php [ ^ ]

openssl_encrypt openssl_decrypt 函数(PHP 5> = 5.3.0):

http://php.net/manual/en/function.openssl-encrypt.php [ ^ ]

http://php.net/manual/en/function.openssl-decrypt.php [ ^ ]

http:// stackoverflow .com / a / 6639179 [ ^ ]



但是如果你想加密/解密密码,请使用散列,因为你无法解密散列字符串。

哈希方法(PHP 5> = 5.1.2,PECL哈希> = 1.1):

http://php.net/manual/en/function.hash.php [ ^ ]

问:但是如果你无法解密它,你如何检查一个给定的密码是正确的吗?

A:试试这个(例如,如果使用SHA-256对密码进行哈希处理:

< pre lang =php> <?php
if (hash(' sha256'


givenPsswd)==


hashedPsswd) // change


HY!
can any one give me an idea that which is the best and latest way to encrypt user data in php and also decrypt it whenever we want?

解决方案

Hi,

You can use the mcrypt_encrypt and mcrypt_decrypt functions (PHP 4 >= 4.0.2, PHP 5):
http://php.net/manual/en/function.mcrypt-encrypt.php[^]
http://php.net/manual/en/function.mcrypt-decrypt.php[^]
Or the openssl_encrypt and openssl_decrypt functions (PHP 5 >= 5.3.0):
http://php.net/manual/en/function.openssl-encrypt.php[^]
http://php.net/manual/en/function.openssl-decrypt.php[^]
http://stackoverflow.com/a/6639179[^]

But if you want to encrypt/decrypt a password, use hashing, because you can't decrypt a hashed string.
The hash method (PHP 5 >= 5.1.2, PECL hash >= 1.1):
http://php.net/manual/en/function.hash.php[^]
Q: But if you can't decrypt it, how do you check whether a given password is correct?
A: Try this (for example if your password is hashed using SHA-256:

<?php
if (hash('sha256',


givenPsswd) ==


hashedPsswd) // change


这篇关于在PHP中加密数据的最佳方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆