简体   繁体   English

如何安全地存储加密密钥?

[英]How to store a crypto key securely?

I'm looking at using a crypto lib such as pycrypto for encrypting/decrypting fields in my python webapp db. 我正在寻找使用诸如pycrypto之类的加密库来加密/解密我的python webapp数据库中的字段。 But encryption algorithms require a key. 但加密算法需要密钥。 If I have an unencrypted key in my source it seems silly to attempt encryption of db fields as on my server if someone has access to the db files they will also have access to my python sourcecode. 如果我的源代码中有一个未加密的密钥,那么在我的服务器上尝试加密db字段似乎很愚蠢,如果有人可以访问db文件,他们也可以访问我的python源代码。

Is there a best-practice method of securing the key used? 是否有保护所用密钥的最佳实践方法? Or an alternative method of encrypting the db fields (at application not db level)? 或者另一种加密db字段的方法(在应用程序而不是db级别)?

UPDATE: the fields I am trying to secure are oauth tokens. 更新:我试图保护的字段是oauth令牌。

UPDATE: I guess there is no common way to avoid this. 更新:我想没有常见的方法来避免这种情况。 I think I'll need to encrypt the fields anyway as it's likely the db files will get backed up and moved around so at least I'll reduce the issue to a single vulnerable location - viewing my source code. 我想我无论如何都需要加密字段,因为db文件可能会被备份和移动,所以至少我会将问题减少到一个易受攻击的位置 - 查看我的源代码。

UPDATE: The oauth tokens need to be used for api calls while the user is offline, therefore using their password as a key is not suitable in this case. 更新:当用户离线时,需要将oauth令牌用于api调用,因此在这种情况下使用他们的密码作为密钥是不合适的。

If you are encrypting fields that you only need to verify (not recall), then simple hash with SHA or one-way encrypt with DES, or IDEA using a salt to prevent a rainbow table to actually reveal them. 如果要加密只需要验证的字段(不记得),那么使用SHA进行简单散列或使用DES进行单向加密,或使用salt进行IDEA以防止彩虹表实际显示它们。 This is useful for passwords or other access secrets. 这对密码或其他访问机密很有用。

Python and webapps makes me think of GAE, so you may want something that is not doing an encrypt/decrypt on every DB transaction since these are already un-cheap on GAE. Python和webapps让我想到了GAE,所以你可能想要一些不对每个数据库事务进行加密/解密的东西,因为这些在GAE上已经不便宜了。

Best practice for an encrypted databased is to encrypt the fields with the users own secret, but to include an asymmetric backdoor that encrypts the users secret key so you (and not anyone who has access to the DB source files, or the tables) can unencrypt the users key with your secret key, should recovery or something else necessitate. 加密数据库的最佳实践是使用用户自己的密钥加密字段,但要包含加密用户密钥的非对称后门,以便您(而不是任何有权访问数据库源文件或表的人)可以解密用户密钥与您的密钥,应恢复或其他必要的。

In that case, the user (or you or trusted delegate) can retireve and unencrypt their own information only. 在这种情况下,用户(或您或受信任的代理人)只能退出并解密他们自己的信息。 You may want to be more stringent in validating user secrets if you are thinking you need to secure their fields by encryption. 如果您认为需要通过加密来保护其字段,则可能需要更严格地验证用户机密。

In this regards, a passphrase (as opposed to a password) of some secret words such "in the jungle the mighty Jungle" is a good practice to encourage. 在这方面,一些密码(而不是密码)的一些秘密词,如“在丛林中强大的丛林”是一个鼓励的好习惯。

EDIT: Just saw your update. 编辑:刚看到你的更新。 The best way to store OAuth is to give them a short lifespan, only request resources your need and re-request them over getting long tokens. 存储OAuth的最佳方法是为他们提供短暂的生命周期,只需要需要的资源,并通过获取长令牌重新请求它们。 It's better to design around getting authenticated, getting your access and getting out, than leaving the key under the backdoor for 10 years. 最好是设计一下进行身份验证,获取访问权限和离开,而不是将密钥留在后门10年。

Since, if you need to recall OAuth when the user comes online, you can do as above and encrypt with a user specfic secret. 因为,如果您需要在用户联机时调用OAuth,您可以执行上述操作并使用用户特定密码进行加密。 You could also keygen from an encrypted counter (encrypted with the user secret) so the actual encryption key changes at each transaction, and the counter is stored in plaintext. 您还可以从加密计数器(使用用户密钥加密)中键入密钥,以便实际加密密钥在每次事务处更改,并且计数器以明文形式存储。 But check specific crypto algo discussion of this mode before using. 但在使用之前,请检查此模式的具体加密算法。 Some algorithms may not play nice with this. 有些算法可能不会很好用。

Symmetric encryption is indeed useless, as you have noticed; 正如您所注意到的那样,对称加密确实毫无用处; however for certain fields, using asymmetric encryption or a trapdoor function may be usable: 但是对于某些字段,可以使用非对称加密或陷门功能:

  • if the web application does not need to read back the data, then use asymmetric encryption. 如果Web应用程序不需要回读数据,则使用非对称加密。 This is useful eg for credit card data: your application would encrypt the data with the public key of the order processing system, which is on a separate machine that is not publically accessible. 这对于信用卡数据非常有用:您的应用程序将使用订单处理系统的公钥加密数据,订单处理系统位于不可公开访问的单独计算机上。

  • if all you need is equality comparison, use a trapdoor function, such as a message digest, ideally with a salt value. 如果您需要的是相等比较,请使用陷门功能,例如消息摘要,理想情况下使用salt值。 This is good for passwords that should be unrecoverable on the server. 这适用于服务器上不可恢复的密码。

Before you can determine what crypto approach is the best, you have to think about what you are trying to protect and how much effort an attacker will be ready to put into getting the key/information from your system. 在确定最佳加密方法之前,您必须考虑您要保护的内容以及攻击者准备从您的系统获取密钥/信息所需的工作量。

What is the attack scenario that you are trying to remedy by using crypto? 您尝试使用加密来解决的攻击情形是什么? A stolen database file? 一个被盗的数据库文件?

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

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