使用密钥对 php5 中的 utf-8 字符串进行简单加密/解密
有人可以就“使用密钥对 php5 中的 utf-8 字符串进行简单加密/解密”主题向我提供建议吗?关于独立图书馆的一些建议? (不依赖于 32/64 位机器)。
$encryptedTextUTF8 = simpleEncrypt($originalTextUTF8, $secret);
echo simpleDecrypt($encryptedTextUTF8, $secret); // outputs $originalTextUTF8
Could somebody advice me on subject "Simple encrypt/decrypt of utf-8 strings in php5 with secret key" ? Some advice on independant libraries?
(no dependancy on 32/64bit machines).
$encryptedTextUTF8 = simpleEncrypt($originalTextUTF8, $secret);
echo simpleDecrypt($encryptedTextUTF8, $secret); // outputs $originalTextUTF8
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
使用 mcrypt 扩展: http://us.php.net/manual/en/ book.mcrypt.php
像下面这样的东西可能是可行的:
编辑: 事实证明是错误的,我建议删除加密的 md5 部分。至于什么更好,我还不知道,但当我有足够的时间调查时,我会尝试更新我的帖子..抱歉! :)
Using the mcrypt extension: http://us.php.net/manual/en/book.mcrypt.php
Something like the following could be feasible:
edit: Proven wrong, I suggest to remove the md5 part of the encryption. As to what is better, I do not know yet, but I'll try to come update my post when I've had sufficient time investigating.. sorry! :)
PHP 字符串不知道(或关心)它们的字符编码是什么。它们只是字节的集合。这让你完全不用担心这里的UTF-8。
因为您要求对称密钥加密,所以您可能需要使用 mcrypt扩展名。它有多种密码可用,包括 AES(“Rijndael”) )。 Mcrypt 本身并不关心它是在 32 位还是 64 位机器上。
我不会提供示例代码,因为使用加密需要一些不平凡的知识,我不确定我是否可以单独用代码向您正确解释这些知识,并且加密代码的复制粘贴是一个糟糕的主意。
了解分组密码操作模式的工作原理将帮助您为您正在做的事情选择正确的模式。如果您希望使用其他语言开发的程序处理您的加密数据,这种理解也是必要的,因为它们将使用不同的默认密码模式和不同的 填充方案。
PHP strings don't know (or care) what their character encoding is. They're just a collection of bytes. This allows you to entirely not worry about UTF-8 here.
Because you're asking for symmetric key encryption, you'll probably want to use the mcrypt extension. It has a wide variety of cyphers available, including AES ("Rijndael"). Mcrypt itself doesn't care whether or not it's on a 32-bit or 64-bit machine.
I'm not going to provide example code, because using encryption requires some non-trivial knowledge that I'm not sure I can properly explain to you in code alone, and copy-paste of encryption code is a bad, bad idea.
Understanding how block cypher modes of operation work will help you pick the proper mode for what you're doing. This understanding will also be necessary if you wish to have programs developed in other languages work with your encrypted data, as they'll use different default cipher modes and different padding schemes.