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
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/ ...
- .NET 云原生架构师训练营(权限系统 代码实现 Store.EntityFramework)--学习笔记
目录 开发任务 代码实现 开发任务 DotNetNB.Security.Core:定义 core,models,Istore:实现 default memory store DotNetNB.Secu ...
- (转)笔记320 SQLSERVER中的加密函数 2013-7-11
1 --SQLSERVER中的加密函数 2013-7-11 2 ENCRYPTBYASYMKEY() --非对称密钥 3 ENCRYPTBYCERT() --证书加密 4 ENCRYPTBYKEY() ...
- 【转】 svn 错误 以及 中文翻译
直接Ctrl+F 搜索你要找的错 # # Simplified Chinese translation for subversion package # This file is distribute ...
- .NET安全审核检查表
书籍名称:Web安全设计之道 -.NET代码安全,界面漏洞防范与程序优化 .NET安全审核检查表 检查项 任务描述 设计环节 Security descisions should no ...
- mac下php开发环境搭建+CI框架使用
一.启动apache: apachectl start 停止: apachectl stop 配置文件: vi /etc/apache2/httpd.conf 一.修改端口 因为80端口不想被占用,8 ...
- Windows Phone Data Protection
To encrypt the PIN // Convert the PIN to a byte[]. byte[] PinByte = Encoding.UTF8.GetBytes(TBPin.Tex ...
- Jenkins进阶系列之——08Jenkins纳入版本控制
2014-07-25:更新shell脚本 2014-06-05:更新shell脚本 2014-01-09:更新shell脚本,修改Jenkins文件删除后不能自动从版本控制删除的bug 是不是有过这种 ...
- svn 结合rsync 的代码发布系统
由开发提交到测试环境,经测试,在由运维统一上线.试验需求一台测试服务器,一台线上(生产环境)服务器.测试服务器上跑svn是开发用于代码管理,而线上跑的svn是运维用来代码上线的.结合rsync保持测试 ...
随机推荐
- RabbitMQ的原理和使用
转载:RabbitMQ从入门到精通 转载:轻松搞定RabbitMQ 转载:RabbitMQ Java入门教程 一.RabbitMQ AMQP,即Advanced Message Queuing Pro ...
- vue 和react
React 和 Vue 有许多相似之处,它们都有: 使用 Virtual DOM 提供了响应式 (Reactive) 和组件化 (Composable) 的视图组件. 将注意力集中保持在核心库,而将其 ...
- 2017北京国庆刷题Day1 morning
期望得分:100+100+100=300 实际得分:100+100+70=270 T1位运算1(bit) Time Limit:1000ms Memory Limit:128MB 题目描述 LYK ...
- TCP和UDP相关概念
位于传输层的协议,都是基于IP协议的. TCP是面向连接的.可靠的传输,UDP是无连接的.不可靠的传输.要进行TCp传输时候,需要进行三次握手,建立连接,然后才能发送数据,而且在发送过程中,有数据的确 ...
- jQuery 写的简单打字游戏
var off_x; //横坐标 var count=0; //总分 var speed=5000; //速度,默认是5秒. var keyErro=0; //输入错误次数 var keyRight= ...
- 51nod 1140 矩阵相乘结果的判断
给出三个N*N的矩阵A, B, C,问A * B是否等于C? Input 第1行,1个数N.(0 <= N <= 500) 第2 - N + 1行:每行N个数,对应矩阵A的元素.(0 ...
- 下载Google My Tracks
http://code.google.com/p/mytracks/source/browse/?name=2.0.2#hg%2FMyTracks%253Fstate%253Dclosed 需要类似的 ...
- 在Unity中实现屏幕空间阴影(1)
接着上篇文章,我们实现了SSR效果. 其中的在屏幕空间进行光线追踪的方法是通用的.借此我们再实现一种屏幕空间的效果,即屏幕空间阴影. 文中的图片来自Catlike coding http://catl ...
- NYOJ 267 郁闷的C小加(二) (字符串处理)
题目链接 描述 聪明的你帮助C小加解决了中缀表达式到后缀表达式的转换(详情请参考"郁闷的C小加(一)"),C小加很高兴.但C小加是个爱思考的人,他又想通过这种方法计算一个表达式的值 ...
- linux 在命令行中通过conda使用anaconda
在 ~/.bash_profile中添加 export PATH="/home/taoke/anaconda/bin:$PATH"