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. vim 常用快捷键(整理版)

    最常用: x 删除后面的字符 X 删除前一个字符  删除3个字符就是3x dd:删除一行   D 删除到行尾 J:删除换行符,使下一行并上来.     nJ:连接后面的n行 u:撤销上一次操作     ...

  2. java基础-关键词super与this

    转发:itbooks this是调用自己本身的构造函数,而super是调用父类中的构造函数. 这两个关键词是用在构造函数中的,这两个关键词的设计也是对封装特性的一种考虑,避免编写不必要的重复代码. c ...

  3. 对拍 bat

    :loop maker.exe std.exe my.exe fc std.out my.out goto loop pause

  4. bzoj 2502 清理雪道 (有源汇上下界最小流)

    2502: 清理雪道 Time Limit: 10 Sec  Memory Limit: 128 MB Description        滑雪场坐落在FJ省西北部的若干座山上. 从空中鸟瞰,滑雪场 ...

  5. jieba文本分词,去除停用词,添加用户词

    import jieba from collections import Counter from wordcloud import WordCloud import matplotlib.pyplo ...

  6. AJAX流程

    创建一个XHR对象 var xmlhttp; if (window.XMLHttpRequest) {// code for IE7+, Firefox, Chrome, Opera, Safari ...

  7. 【CodeForces】914 E. Palindromes in a Tree 点分治

    [题目]E. Palindromes in a Tree [题意]给定一棵树,每个点都有一个a~t的字符,一条路径回文定义为路径上的字符存在一个排列构成回文串,求经过每个点的回文路径数.n<=2 ...

  8. 【CodeForces】889 C. Maximum Element 排列组合+动态规划

    [题目]C. Maximum Element [题意]给定n和k,定义一个排列是好的当且仅当存在一个位置i,满足对于所有的j=[1,i-1]&&[i+1,i+k]有a[i]>a[ ...

  9. linux 下 genymotion 模拟器不能安装app

    提示: "应用未安装" 解决方法: 下载: Genymotion-ARM-Translation_v1.1.zip 进入genymotion 的tools用adb传进去: ./ad ...

  10. BAT-快速切换JDK1.6/1.7

    Code: @echo OFF rem 修改背景/字体颜色为黑/绿 color 0A rem 自动设置JDK环境变量,本程序不会对 [系统环境变量] 造成破环!不需要重启!! rem 没有%%JAVA ...