百科狗-知识改变命运!
--

mdecrypt_generic() - mcrypt库(加密)

乐乐12个月前 (11-21)阅读数 22#技术干货
文章标签数据

mdecrypt_generic()

(PHP 4 >= 4.0.2, PHP 5, PHP 7 = 1.0.0)

解密数据

Warning

This function has beenDEPRECATEDas of PHP 7.1.0. Relying on this functionis highly discouraged.

说明

mdecrypt_generic(resource $td,string $data): string

解密数据。请注意,由于存在数据补齐的情况,返回字符串的长度可能和明文的长度不相等。

参数

$td

由mcrypt_module_open()函数创建的加密描述符。

$data

密文。

范例

mdecrypt_generic()例程

mdecrypt_generic() - mcrypt库(加密)

上例中演示了如何检测解密后的数据是否和原始明文长度一致。需要着重提醒的是,在对数据进行机密之前,必须使用mcrypt_generic_init()函数来重新初始化缓冲区。

调用本函数之前,必须使用密钥和初始向量来调用mcrypt_generic_init()函数对解密句柄进行初始化。加解密工作完成之后,需要调用mcrypt_generic_deinit()来释放加解密缓冲区。例程请参见mcrypt_module_open()。

参见

  • mcrypt_generic()加密数据
  • mcrypt_generic_init()初始化加密所需的缓冲区
  • mcrypt_generic_deinit()对加密模块进行清理工作
For all those of you which are "surprised" by the padding (even though it is mentioned in the description of the method) please do not just "strip away everything which I do not understand".
The approach with rtrim, rtrim "\0", removing all charcodes 
We found that sometimes the resulting padding is not null characters "\0" but rather one of several control characters.
If you know your data is not supposed to have any trailing control characters "as we did" you can strip them like so. 
Whenever you need to decrypt files encrypted with dot.net (and others?) you can use the following settings:
Encryption: 'rijndael-256'
Mode: 'cbc'
Padding-Mode: Zeros
iv: "yes"
encode the data with base64 to send them to your php script
I have noticed that sometimes when the binary ciphertext is longer than the plaintext, the decrypted plaintext can have some little boxes/squares next to it as 'padding'. I also noticed that you can't cut and paste them to be able to edit them out, but i did find a solution.
Just call rtrim() around the string and it removes them.
It is generally not recommended to just use rtrim to remove the padding.
Use rtrim($str, "\0") for strings that do not end in "\0" or store the data length during encryption. 
(Although data containing "\0" sometimes gets corrupted during encryption so these types of data actually should be packed.)
For example: 
Just confirming the .DLL issues. With 4.3.4 you need the older .DLL. I'm guessing any version of PHP4 needs the older .DLL. With PHP5 you need the newer one.
On Win32 systems with PHP 5, you must use the newer libmcrypt.dll file, otherwise mdecrypt_generic will not work.
Here's a bit of encrypt/decrypt code.
If you're using this on the win32 platform, BEWARE! The latest DLL (19-Jan-2004) contains a bug that keeps mdecrypt_generic from functioning. Nearly drove me over the edge... The 30-Dec-2002 version seems to work with no trouble.

鹏仔微信 15129739599 鹏仔QQ344225443 鹏仔前端 pjxi.com 共享博客 sharedbk.com

免责声明:我们致力于保护作者版权,注重分享,当前被刊用文章因无法核实真实出处,未能及时与作者取得联系,或有版权异议的,请联系管理员,我们会立即处理! 部分文章是来自自研大数据AI进行生成,内容摘自(百度百科,百度知道,头条百科,中国民法典,刑法,牛津词典,新华词典,汉语词典,国家院校,科普平台)等数据,内容仅供学习参考,不准确地方联系删除处理!邮箱:344225443@qq.com)

图片声明:本站部分配图来自网络。本站只作为美观性配图使用,无任何非法侵犯第三方意图,一切解释权归图片著作权方,本站不承担任何责任。如有恶意碰瓷者,必当奉陪到底严惩不贷!

内容声明:本文中引用的各种信息及资料(包括但不限于文字、数据、图表及超链接等)均来源于该信息及资料的相关主体(包括但不限于公司、媒体、协会等机构)的官方网站或公开发表的信息。部分内容参考包括:(百度百科,百度知道,头条百科,中国民法典,刑法,牛津词典,新华词典,汉语词典,国家院校,科普平台)等数据,内容仅供参考使用,不准确地方联系删除处理!本站为非盈利性质站点,本着为中国教育事业出一份力,发布内容不收取任何费用也不接任何广告!)