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

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

  1. 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/ ...

  2. .NET 云原生架构师训练营(权限系统 代码实现 Store.EntityFramework)--学习笔记

    目录 开发任务 代码实现 开发任务 DotNetNB.Security.Core:定义 core,models,Istore:实现 default memory store DotNetNB.Secu ...

  3. (转)笔记320 SQLSERVER中的加密函数 2013-7-11

    1 --SQLSERVER中的加密函数 2013-7-11 2 ENCRYPTBYASYMKEY() --非对称密钥 3 ENCRYPTBYCERT() --证书加密 4 ENCRYPTBYKEY() ...

  4. 【转】 svn 错误 以及 中文翻译

    直接Ctrl+F 搜索你要找的错 # # Simplified Chinese translation for subversion package # This file is distribute ...

  5. .NET安全审核检查表

    书籍名称:Web安全设计之道 -.NET代码安全,界面漏洞防范与程序优化   .NET安全审核检查表   检查项 任务描述 设计环节     Security descisions should no ...

  6. mac下php开发环境搭建+CI框架使用

    一.启动apache: apachectl start 停止: apachectl stop 配置文件: vi /etc/apache2/httpd.conf 一.修改端口 因为80端口不想被占用,8 ...

  7. Windows Phone Data Protection

    To encrypt the PIN // Convert the PIN to a byte[]. byte[] PinByte = Encoding.UTF8.GetBytes(TBPin.Tex ...

  8. Jenkins进阶系列之——08Jenkins纳入版本控制

    2014-07-25:更新shell脚本 2014-06-05:更新shell脚本 2014-01-09:更新shell脚本,修改Jenkins文件删除后不能自动从版本控制删除的bug 是不是有过这种 ...

  9. svn 结合rsync 的代码发布系统

    由开发提交到测试环境,经测试,在由运维统一上线.试验需求一台测试服务器,一台线上(生产环境)服务器.测试服务器上跑svn是开发用于代码管理,而线上跑的svn是运维用来代码上线的.结合rsync保持测试 ...

随机推荐

  1. sql命令创建数据库

    mysql create database 指定utf-8编码 CREATE DATABASE IF NOT EXISTS thinkphp DEFAULT CHARSET utf8 COLLATE ...

  2. I/O多路复用和异步I/O

    一.I/O模式 对于一次IO访问(以read举例),数据会先被拷贝到操作系统内核的缓冲区中,然后才会从操作系统内核的缓冲区拷贝到应用程序的地址空间.所以说,当一个read操作发生时,它会经历两个阶段: ...

  3. python使用pwd和grp操作unix用户及用户组

    1.pwd模块 pwd模块提供了一个unix密码数据库即/etc/passwd的操作接口,这个数据库包含本地机器用户帐户信息 常用操作如下: pwd.getpwuid(uid):返回对应uid的示例信 ...

  4. MySQL免安装版配置部署

    MySQL下载地址:http://dev.mysql.com/downloads/mysql/ 1.Windows下安装MySQL 我下的是最新版的MySQL,解压后,目录如下: 将解压目录下默认文件 ...

  5. NOIP 2000 方格取数

    https://www.luogu.org/problem/show?pid=1004 题目描述 设有N*N的方格图(N<=9),我们将其中的某些方格中填入正整数,而其他的方格中则放 人数字0. ...

  6. [USACO07FEB] Lilypad Pond

    https://www.luogu.org/problem/show?pid=1606 题目描述 FJ has installed a beautiful pond for his cows' aes ...

  7. 【POJ】2774 Long Long Message

    [题意]给定两个字符串S和T,求最长公共子串.len<=10^5. [算法]后缀自动机 [题解]对字符串S建SAM,然后令串T在S上跑匹配. 这是自动机最原本的功能——匹配,就是串T在SAM(S ...

  8. js和php的时间戳和时间的转化

    js时间戳转化为时间 //时间戳转时间 function time(sj) { var now = new Date(sj*1000); var year =now.getFullYear(); va ...

  9. $this->success()传值不完整

    public function manager_doExport() { $search=$_POST['search']; //前台输入2017-12-1,即,$search['starttime' ...

  10. python基础===如何在列表,字典,集合中根据条件筛选数据

    #常见的操作如下: data = [1, 5, -3, -2, 6, 0, 9] res = [] for x in data: if x>=0: res.append(x) print(res ...