PHP using mcrypt and store the encrypted in MySQL

This is how I would do it. Create a class to do encryption/decryption:

class cipher
{
    private $securekey;
    private $iv_size;

    function __construct($textkey)
    {
        $this->iv_size = mcrypt_get_iv_size(
            MCRYPT_RIJNDAEL_128,
            MCRYPT_MODE_CBC
        );
        $this->securekey = hash(
            ‘sha256‘,
            $textkey,
            TRUE
        );
    }

    function encrypt($input)
    {
        $iv = mcrypt_create_iv($this->iv_size);
        return base64_encode(
            $iv . mcrypt_encrypt(
                MCRYPT_RIJNDAEL_128,
                $this->securekey,
                $input,
                MCRYPT_MODE_CBC,
                $iv
            )
        );
    }

    function decrypt($input)
    {
        $input = base64_decode($input);
        $iv = substr(
            $input,
            0,
            $this->iv_size
        );
        $cipher = substr(
            $input,
            $this->iv_size
        );
        return trim(
            mcrypt_decrypt(
                MCRYPT_RIJNDAEL_128,
                $this->securekey,
                $cipher,
                MCRYPT_MODE_CBC,
                $iv
            )
        );
    }
}

Then use it like this:

// Usage
$cipher = new cipher(‘my-secret-key‘);
$orignal_text = ‘my secret message‘;
$encrypted_text = $cipher->encrypt($orignal_text);   // store this in db
$decrypted_text = $cipher->decrypt($encrypted_text); // load $encrypted_text from db

// Debug
echo "Orignal Text  : $orignal_text\r\n";
echo "Encrypted Text: $encrypted_text\r\n";
echo "Decrypted Text: $decrypted_text";

This respectively outputs the following:

Orignal Text  : my secret message
Encrypted Text: Z21ifr5dHEdE9nO8vaDWb9QkjooqCK4UI6D/Ui+fkpmXWwmxloy8hM+7oimtw1wE
Decrypted Text: my secret message

来源:http://stackoverflow.com/questions/26756322/php-using-mcrypt-and-store-the-encrypted-in-mysql

时间: 2024-11-03 01:19:28

PHP using mcrypt and store the encrypted in MySQL的相关文章

Creating a Store Locator with PHP, MySQL & Google Maps(保存地图坐标 经纬度方法 google mysql)

Google Geo APIs Team August 2009 This tutorial is intended for developers who are familiar with PHP/MySQL, and want to learn how to use Google Maps with a MySQL database to create a store locator-type app. After completing this tutorial, you will hav

PHP mcrypt加密扩展使用总结

在开发中,很多时候我们在前后端交互中需要对一些敏感数据进行一定的加密.PHP中有提供了mcrypt的这样一个加密扩展实现对数据的加密解密. 一.mcrypt扩展的安装 在低版本的PHP中需要在配置文件php.ini中显式添加对扩展的引用,同时要保证扩展引用目录中有相应的扩展文件:在高版本的PHP中,Windows下似乎默认开启了mcrypt的扩展,既不需要在配置文件php.ini中做配置,在扩展引用目录中也没有看到相应的扩展文件,在linux下则需要安装对应的mcrypt.so扩展. mcryp

RHEL6.4下搭建apache和subversion(SVN)

1.说明 rhel6.4系统下搭建apache+svn 2.实现 1)在服务器上安装配置SVN服务: 2)SVN服务支持svnserve独立服务模式访问: 3)SVN服务支持Apache的http模式访问. 3.安装svn服务器 [[email protected] ~]# yum install -y subversion [[email protected] ~]# rpm -qa|grep subversion subversion-1.6.11-15.el6_7.x86_64 4.安装a

svn installation

# yum install mod_dav_svn.x86_64 subversion-svn2cl.noarch===========================================================================================================================================================================svnserve.conf:12: Opti

搭建svn服务器

一.搭建svn服务器1.环境检查 [[email protected] ~]# cat /etc/redhat-release  CentOS release 6.8 (Final) [[email protected]x-node01 ~]# getconf LONG_BIT 64 [[email protected] ~]# /etc/init.d/iptables status iptables: Firewall is not running. [[email protected] ~]

linux下安装svn(基于编码的方式)

svn是什么,相信能看到这里的同学应该不会有这个问题了,费话不多说,开始: 1.创建目录 mkdir /home/svn/ 2.获取安装svn所需源文件(svn的官方网址是http://subversion.tigris.org) wget http://subversion.tigris.org/downloads/subversion-1.6.1.tar.gzwget http://subversion.tigris.org/downloads/subversion-deps-1.6.1.t

svn web页面svn up报错

今天程序员找到我说svn web界面中svn up 报错,于是我马不停蹄的去看了下确实报错, 我就看了下日志果然报了很多错误根据最后一条判断----------------------------------------------------------------------- ATTENTION!  Your password for authentication realm: <http://10.15.200.30:80> product svn can only be stored

code_action

w https://raw.githubusercontent.com/laravel/laravel/master/config/database.php <?php return [ /* |-------------------------------------------------------------------------- | Default Database Connection Name |-----------------------------------------

svn学习之一(svn独立服务器搭建)svn钩子了解

SVN实战笔记#####################################################xingxing qq:1218761836 qq群:150181442##################################################### SVN实战  目录SVN实战    1一.SVN简介    1二.SVN运行方式    12.1 svn的访问模式3种    1三.SVN数据格式    23.1 svn 档案库数据格式    2四.