如何使用专用密钥 - PHP创建双向编码/解码方法? [英] How to create two way encode/decode methods using use-specific key - PHP?

查看:129
本文介绍了如何使用专用密钥 - PHP创建双向编码/解码方法?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要两个函数/方法,一个编码,一个解码。 这不适用于存储密码 。每个用户都有一个特定的键/盐来编码数据。



这是我想要的工作原理:


$ b $ ($ str,$ key){
// something fancy
}

函数decode($ str,$ key) $ key){
// something fancy
}

$ key = $ logged_in_user-> get_key();
$ plain ='abc abc 123 123';
$ encoded_data = encode($ plain,$ key);
// some_fancy_encrypted_data_that_is_really_cooooool
$ decoded_data = decode($ encoded_data,$ key);
// abc abc 123 123

另一件事是,每次使用这个功能每次使用具有相同用户密钥的 encode 函数时,需要返回相同的事情。



这样做?

解决方案

  $ myVarIWantToEncodeAndDecode 

定义键(salt,broth etc ..): $ key =#& $ sdfdfs789fs7d;



要编码:

  $ encoded = base64_encode (mcrypt_encrypt(MCRYPT_RIJNDAEL_256,md5($ key)),$ myVarIWantToEncodeAndDecode,MCRYPT_MODE_CBC,md5(md5($ key)))); 

要解码:

  $ decoding = rtrim(mcrypt_decrypt(MCRYPT_RIJNDAEL_256,md5($ key)),base64_decode($ encoded),MCRYPT_MODE_CBC,md5(md5($ key))),\0); 


I need two functions/methods, one to encode, one to decode. This is not for storing passwords. Each user will have a specific key/salt to encode the data.

This is how I would like it to work:

function encode($str, $key) {
    // something fancy
}

function decode($str, $key) {
    // something fancy
}

$key = $logged_in_user->get_key();
$plain = 'abc abc 123 123';
$encoded_data = encode($plain, $key);
// some_fancy_encrypted_data_that_is_really_cooooool
$decoded_data = decode($encoded_data, $key);
// abc abc 123 123

Another thing is that every time I use this function it needs to return the same thing every time I use the encode function with the same user key.

How would I do this??

解决方案

$myVarIWantToEncodeAndDecode

Define key (salt, broth etc..): $key = "#&$sdfdfs789fs7d";

To encode:

$encoded = base64_encode(mcrypt_encrypt(MCRYPT_RIJNDAEL_256, md5($key), $myVarIWantToEncodeAndDecode, MCRYPT_MODE_CBC, md5(md5($key))));

To decode:

$decoded = rtrim(mcrypt_decrypt(MCRYPT_RIJNDAEL_256, md5($key), base64_decode($encoded), MCRYPT_MODE_CBC, md5(md5($key))), "\0");

这篇关于如何使用专用密钥 - PHP创建双向编码/解码方法?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆