在PHP中安全地加密/解密字符串 [英] Secure password encryption/decryption of strings in PHP

查看:123
本文介绍了在PHP中安全地加密/解密字符串的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想知道在PHP中是否存在一种简单但安全的方法来加密字符串(而不是密码),密码不存储在服务器上。

I wanted to know if there exists a somewhat simple, but secure, method to encrypt strings(not passwords), with a password which is not stored on the server, in PHP.

我已经检查过 PHP的可逆密码加密程序,但我不确定如果入侵者可以访问服务器和源,它是否足够安全。

I've checked A reversible password encryption routine for PHP, but I'm unsure if it is secure enough if intruders have access to the server and source.

我们在谈论一个自动系统,其中计算机向服务器发送请求,该服务器存储日志中的信息。所以我想我可以在请求头中发送加密密码,最好是加密,但是在服务器上不存在密码的情况下难以进行解密。等等,我想我可能会使事情变得太复杂了,但是我希望你得到这个想法...这意味着保护信息安全,即使黑客完全可以控制服务器。

We're talking about a automatic system where a computer sends a request to a server, which stores information in a log. So I'm thinking I could send the encryption password in the request header, preferably encrypted, but then it would be difficult to decrypt without storing the password somehow on the server. Wait, I think i might be complicating things a bit too much, but I hope you get the idea... It's meant to keep the information safe, even if hackers have full control over the server.

推荐答案

如果我正确理解你的意图,你的目标是由服务器加密的日志。这些请求是以简单的方式发送的,但是您希望记录像每个用户访问统计信息等等,并且您认为这些数据是机密的,因此应由服务器进行加密,并由服务器进行解密,如果必要的。

If I understand you correctly, you aim for a log that is encrypted by the server. The requests are sent in plain, but you'd like to log something like per-user access statistics or the like and you deem this data to be confidential, so it should be encrypted by the server and also be decrypted by the server, if necessary.

如果是这种情况,实际上并不是太复杂。

If this is the case, it is actually not all too complicated.


  • 生成要由服务器使用的加密密钥(AES将是一个不错的选择)。

  • 将此密钥存储在文件中。

  • 确保应用程序和只有几个选定的人员可以访问该位置。最糟糕的情况是它会在您的公共文件中提供,任何人都可以从网络下载。所以把它放在远离你的公共资源的文件夹中:)

  • 使用基于密码的加密加密该文件 eg RFC 2898中的PBKDF2

  • Generate an encryption key (AES would be a good choice) that is to be used by the server.
  • You store this key in a file.
  • Make sure that the application and only a few selected people have access to that location. Worst case would be it's served in your public files and anyone could download it from the web. So put it in a folder far away from your public resources :)
  • Encrypt that file using password-based encryption e.g. PBKDF2 in RFC 2898.

然后你会发现你创建了一个鸡蛋问题 - 该文件需要一个服务器的密码可以访问存储在其中的密钥。但是这里的技巧 - 手动启动服务器启动时必须输入密钥,这就是您需要的短暂组件。该文件的密码应为带外信息(例如放置在物理保管库中),而不在计算机本身。

Then you will realize that you created a hen-egg problem - the file again needs a password for the server to have access to the key stored inside. But here's the trick - you will have to enter the key upon server startup manually, and that's the ephemeral component you need. The password for the file should be out-of-band information (e.g. placed in a physical vault) and nowhere on the computer itself.

一种替代方案(但可能不太安全,因为密码将以某种物理形式存在)是依赖于特定于操作系统的密码库,例如Windows隔离存储

An alternative (but potentially less secure because the password would be present in some physical form) is to rely on OS-specific "password vaults" such as Windows' Isolated Storage.

这篇关于在PHP中安全地加密/解密字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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