简体   繁体   English

PHP:mycrypt密钥?

[英]PHP: mycrypt key?

please don't mind me asking this but I'm new to php and I need to encrypt and decrypt a password. 请不要介意我问这个问题,但是我是php的新手,我需要加密和解密密码。 I want to send the password over a URL so I heard it's the safest way to use mycrypt. 我想通过URL发送密码,因此听说这是使用mycrypt的最安全方法。

I don't get the thing with the KEY in the mycrypt function? 我无法通过mycrypt函数中的KEY得到东西吗? Shouldn't that be as secret as the password itself. 那不应该像密码本身一样秘密。 eg I'm using this function from the PHP manual: 例如,我正在使用PHP手册中的以下功能:

    <?php
    $iv_size = mcrypt_get_iv_size(MCRYPT_RIJNDAEL_256, MCRYPT_MODE_ECB);
    $iv = mcrypt_create_iv($iv_size, MCRYPT_RAND);
    $key = "this is my personal decryption key";
    $text = "Meet me at 11 o'clock behind the monument.";
    echo strlen($text) . "\n";

    $crypttext = mcrypt_encrypt(MCRYPT_RIJNDAEL_256, $key, $text, MCRYPT_MODE_ECB, $iv);
    echo strlen($crypttext) . "\n";
    ?>

can i set the $key to whatever i want? 我可以将$ key设置为我想要的吗? what if someone downloads the source of my document where i set up this $key. 如果有人在我设置此$ key的位置下载我的文档源怎么办? he is able to easily decrypt the $text again. 他能够轻松地再次解密$ text。 isn't he? 是不是 Or do i get something wrong with this function? 还是我使用此功能出错?

It would probably be simpler and more secure to use TLS (HTTPS). 使用TLS(HTTPS)可能更简单,更安全。 This can use the same technologies (eg AES/Rijndael), but handles many of the details, including key distribution, for you. 这可以使用相同的技术(例如AES / Rijndael),但是可以为您处理许多细节,包括密钥分发。 If you use mcrypt, you need to figure out a safe way to exchange both key and IV (initialization vector). 如果使用mcrypt,则需要找出一种安全的方式来交换密钥和IV(初始化向量)。

You clearly also need to protect the key and IV. 您显然还需要保护密钥和IV。 So if it's embedded in your PHP file, you must take care to secure that file. 因此,如果它嵌入在您的PHP文件中,则必须注意保护该文件的安全。 It's important to remember the distinction between server and client side, though. 不过,记住服务器端和客户端之间的区别很重要。 The key won't leak into the generated HTML file unless you have a bug in your script. 除非脚本中有错误,否则密钥不会泄漏到生成的HTML文件中。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM