简体   繁体   English

如何最好地在 SQL Server 中存储密码以便可以检索密码

[英]How best to store a password in SQL Server so that the password can be retrieved

So I'm creating an application that logs into a long list of FTP sites, checks for new files and downloads any new files it finds.所以我正在创建一个应用程序,它登录到一长串 FTP 站点,检查新文件并下载它找到的任何新文件。

I'm using SQL Server 2016 SP1 to store my table of files that I've already downloaded, so when I connect to the FTP and check against that table, I can tell if a file is "new" or not.我正在使用 SQL Server 2016 SP1 来存储我已经下载的文件表,因此当我连接到 FTP 并检查该表时,我可以判断文件是否为“新”文件。

I'm also using SQL Server to store my list of FTP sites, usernames and... PASSWORDS!我还使用 SQL Server 来存储我的 FTP 站点列表、用户名和...密码! My application loops through every FTP site in my FTP table one by one and uses the credentials in the table to connect to them.我的应用程序一个一个循环遍历我的 FTP 表中的每个 FTP 站点,并使用表中的凭据连接到它们。

Right now I have the passwords stored as plan varchars, since I need to pass them into the FTP application I'm using.现在我将密码存储为计划 varchars,因为我需要将它们传递到我正在使用的 FTP 应用程序中。

What I'd like to do is store them as encrypted data, so that I only ever decrypt the password when I'm about to use it.我想要做的是将它们存储为加密数据,以便我只在我即将使用密码时解密密码。 Ideally I'll pass the decrypted password to my FTP client in memory only and never actually write it to disk as a varchar.理想情况下,我只会将解密的密码传递给内存中的 FTP 客户端,而不会将其作为 varchar 实际写入磁盘。

Every guide or webpage I come across talks about encrypting passwords so you only get the hash, effectively giving me a way to check if I have the correct password, which is useful for running a website with a password, or checking if a user has entered a password correctly, or whatever.我遇到的每个指南或网页都在谈论加密密码,因此您只能获得哈希值,有效地让我有一种方法来检查我是否拥有正确的密码,这对于使用密码运行网站或检查用户是否已输入非常有用密码正确,或其他。

What I need is a way to store the password encrypted, so that it can be DE-crypted, by me, so I can use it.我需要的是一种存储加密密码的方法,以便我可以对其进行解密,以便我可以使用它。 Whatever I search for along these lines, I always keep coming back to answers that give me a hash check, not a way to decrypt it.无论我沿着这些方向搜索什么,我总是不断地回到给我哈希检查的答案,而不是解密它的方法。

Is what I want to do possible?我想做的可能吗? Is there even any point?甚至有任何意义吗? Since if I can decrypt the passwords, then surely anyone with access to the database could?因为如果可以解密密码,那么任何有权访问数据库的人都可以吗? Or is there some way of effectively locking them down to only be decrypted by certain UserIDs?或者是否有某种方法可以有效地将它们锁定为只能由某些用户 ID 解密?

Advice welcomed.欢迎咨询。

Not sure what language you are using for your app, but in PHP you could use the openssl and base64 methods to encrypt and decrypt server side.不确定您的应用程序使用的是什么语言,但在 PHP 中,您可以使用 openssl 和 base64 方法来加密和解密服务器端。 Here's a quick PoC:这是一个快速的 PoC:

<?php

$plaintext = 'plain text string to encrypt';
$password = 'Ir43234Ldsase445wsd4f'; 

$method = 'aes-256-cbc';

echo "Password:" . $password . "\n";

$key = openssl_random_pseudo_bytes(openssl_cipher_iv_length($method));

$ivlen = openssl_cipher_iv_length($method);
$iv = openssl_random_pseudo_bytes($ivlen);

$encrypted = base64_encode(openssl_encrypt($plaintext, $method, $key, OPENSSL_RAW_DATA, $iv));

$decrypted = openssl_decrypt(base64_decode($encrypted), $method, $key, OPENSSL_RAW_DATA, $iv);

echo "plaintext=" . $plaintext . "\n";
echo "cipher=" . $method . "\n";
echo "encrypted to: " . $encrypted . "\n";
echo "decrypted to: " . $decrypted . "\n";

You can store your passwords encrypted and be able to decrypt them by using many different public/private or private key encryption methods out there (asymmetrical or symmetric) such as RSA, AES, and 3DES.您可以存储加密的密码,并能够使用许多不同的公钥/私钥或私钥加密方法(非对称或对称)解密它们,例如 RSA、AES 和 3DES。 However, as you stated at the end of your post this creates a security risk as you will need to protect the ability to decrypt the passwords using either strong database permission access to the data and private key.但是,正如您在帖子末尾所述,这会产生安全风险,因为您需要使用对数据和私钥的强数据库权限访问来保护解密密码的能力。 Ideally you should only ever use one-way hashes when storing passwords, and I'd suggest you search for an alternative solution that avoids storing them otherwise (do you need to log in to every FTP site, for example? It depends on what you're trying to achieve with the application).理想情况下,您应该在存储密码时只使用单向哈希,我建议您寻找一种替代解决方案,避免以其他方式存储它们(例如,您是否需要登录到每个 FTP 站点?这取决于您'正在尝试通过应用程序实现)。

If you need to be able to decrypt the passwords with an automatic application, be absolutely sure that the decrypted password is never written anywhere and only exists in temporary memory, and make it such that the private key required to decrypt is protected with only the least necessary permissions.如果您需要能够使用自动应用程序解密密码,请绝对确保解密后的密码永远不会被写入任何地方而只存在于临时内存中,并使其解密所需的私钥只受到最少的保护必要的权限。 For example, YOU shouldn't have access to the production private key, only the application that runs it which should probably run under its own Active Directory account.例如,您不应该访问生产私钥,而只能访问运行它的应用程序,该应用程序可能应该在其自己的 Active Directory 帐户下运行。

It turns out this was a massive waste of time.事实证明,这是对时间的巨大浪费。 I can't encrypt and decrypt on the fly easily enough for it to be worth my while.我无法轻松地即时加密和解密,值得我花时间。 It's possible, but not worth the effort imo.这是可能的,但不值得付出努力。

In the end I just installed a password manager and scripted password requests to it.最后,我只是安装了一个密码管理器并向它编写了密码请求脚本。 By far the simpler and safer solution than creating my own encryption and decryption routines.到目前为止,比创建自己的加密和解密例程更简单、更安全的解决方案。

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

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