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保持测试 ...
随机推荐
- 「Python」人脸识别应用
人脸识别主要步骤: face_recognition 库的安装 安装此库,首先需要安装编译dlib,此处我们偷个懒,安装软件Anaconda(大牛绕过),此软件预装了dlib. 安装好后,我们直接通过 ...
- 数据分析与展示---Numpy入门
概括: 一:数据维度 (一)一维数据 (二)二维数据 (三)多维数据 (四)高维数据 二:Numpy的数组对象:ndarray (一)Numpy介绍 (二)N维数组对象ndarray (三)ndarr ...
- Bayesian optimisation for smart hyperparameter search
Bayesian optimisation for smart hyperparameter search Fitting a single classifier does not take long ...
- [转]C++中cin、cin.get()、cin.getline()、getline()函数的简单总结
参考原文:http://www.cnblogs.com/flatfoosie/archive/2010/12/22/1914055.html,另外做了一些修改~ 1.cin 2.cin.get() 3 ...
- [php]数组建立方式
1.$a[0]=..; $a[1]=..; $a[2]=..; $a[3]=..; 2.$a=array(1,2,3,4,5); 3.自定义数组 $a['logo']="qq"; ...
- css3同心圆闪烁扩散效果
<!DOCTYPE html><html lang="en"><head> <meta charset="UTF-8" ...
- # 2018高考&自主招生 - 游记
准备了一整个学期的高考和自主招生终于结束了....从育英出来, 以一个失败者的身份来写游记, 权当为明年的决战提供经验与总结. Day -1, June 5th 下午同学收拾考场, 自己在那里看书.. ...
- react-native关于ios的启动图标设置
1.首先我们需要使用xcode打开项目,选择项目中的images.xcassets这个文件夹 2.点击AppIcon可以看到右边出现针对不同设备的图标尺寸 3.在左边空白处右击,选择Import... ...
- three.js为何如此奇妙
WebGL是在浏览器中实现三维效果的一套规范,而最初使用WebGL原生的API来写3D程序是一件非常痛苦的事情,在辛苦的付出下WebGL开源框架出现了,其中three.js就是非常优秀的一个,它掩盖了 ...
- arch安装完成之后不能使用笔记本自带的无线网卡
问题描述如下 我笔记本的wifi网卡识别不了,不知道为什么?? 使用ifconfig -a 只列出了有线网卡以及外接的无线网卡如下 enp4s0f1: flags=<UP,BROADCAST,M ...