使用密钥对 php5 中的 utf-8 字符串进行简单加密/解密

发布于 2024-10-21 12:56:02 字数 256 浏览 2 评论 0原文

有人可以就“使用密钥对 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

愁杀 2024-10-28 12:56:02

使用 mcrypt 扩展: http://us.php.net/manual/en/ book.mcrypt.php
像下面这样的东西可能是可行的:

$pass      = 'password_string_to_be_encrypted';
$string    = 'password_string_to_be_decrypted';
$salt      = 'your_salt';
$pepper    = 'your_extra_salt';
$encrypted = base64_encode( mcrypt_encrypt( MCRYPT_RIJNDAEL_256, md5( $salt . $pass . $pepper ), $string, MCRYPT_MODE_CBC, md5( md5( $salt . $pass . $pepper ))));
$decrypted = rtrim( mcrypt_decrypt( MCRYPT_RIJNDAEL_256, md5($salt . $pass . $pepper), base64_decode( $string ), MCRYPT_MODE_CBC, md5( md5( $salt . $pass . $pepper))), "\0");

编辑: 事实证明是错误的,我建议删除加密的 md5 部分。至于什么更好,我还不知道,但当我有足够的时间调查时,我会尝试更新我的帖子..抱歉! :)

Using the mcrypt extension: http://us.php.net/manual/en/book.mcrypt.php
Something like the following could be feasible:

$pass      = 'password_string_to_be_encrypted';
$string    = 'password_string_to_be_decrypted';
$salt      = 'your_salt';
$pepper    = 'your_extra_salt';
$encrypted = base64_encode( mcrypt_encrypt( MCRYPT_RIJNDAEL_256, md5( $salt . $pass . $pepper ), $string, MCRYPT_MODE_CBC, md5( md5( $salt . $pass . $pepper ))));
$decrypted = rtrim( mcrypt_decrypt( MCRYPT_RIJNDAEL_256, md5($salt . $pass . $pepper), base64_decode( $string ), MCRYPT_MODE_CBC, md5( md5( $salt . $pass . $pepper))), "\0");

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! :)

违心° 2024-10-28 12:56:02

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文