encrypt with keyfile, decrypt with password

时间:2017-10-12 10:21:49

标签: php linux encryption passwords

I'm fairly sure this should be simple, but somehow I've come up short from Googling.

I am writing a php script on linux to encrypt files. I want anyone to be able to encrypt files using this script, but to require a password when decrypting the files.

I looked at GnuPG and openssl but they seem to require keyfiles when decrypting too, or password both when encrypting and decrypting - unless I missed something.

Basically I am working on a repository and the project's configuration files contain sensitive information that I don't want on the repo unencrypted, but I want to have a script all developers can use to easily encrypt sensitive files before they commit them to the repo.

2 个答案:

答案 0 :(得分:0)

这应该可以同时使用GnuPGopenssl

您需要拥有加密(即受密码保护)的私钥文件和未加密的公钥文件。任何人都可以使用公钥加密,但解密需要提供受密码保护的私钥。

唯一的技术性是您需要将密钥文件分发给每个人。

答案 1 :(得分:-1)

在martinstoeckli问了一个显而易见的问题"为什么你不能使用密钥来解密文件?",我以为我曾经想过一个黑客,但显然它是#s谷歌搜索后实际上非常广泛使用 - 我的解决方案涉及使用密钥对和密码。

注意:我在临时文件夹中生成密钥对,否则此密钥对将存在于生成它们的PC上的密钥链中,我不希望这样,因此我添加了--homedir /tmp/gnupg

我使用GPG生成无密码密钥对 - 这是我键入的内容:
mkdir /tmp/gnupg
gpg --homedir /tmp/gnupg --full-gen-key
(1) RSA and RSA (default)
4096
0
y
mark
marklahn@domain.com
this key is used for protecting config files
o
然后导出私钥,私钥和ownertrust
gpg --homedir /tmp/gnupg --armor --export marklahn@domain.com > gpg_keyfile.pub
gpg --homedir /tmp/gnupg --armor --export-secret-keys marklahn@domain.com > gpg_keyfile.priv
gpg --homedir /tmp/gnupg --export-ownertrust > gpg_ownertrust.txt
rm -rf /tmp/gnupg
将密钥导入GPG的钥匙串后,这足以在没有密码的情况下加密和解密文件。

接下来是密码保护私钥,所以没有密码就没有人可以使用它。 gpg -c --batch --passphrase password1234 gpg_keyfile.priv
请注意:GPG不会删除原始文件,因此在加密时,请记住在必要时删除原始文件 然后我可以将所有3个文件添加到存储库(gpg_keyfile.pub gpg_keyfile.priv.gpg gpg_ownertrust.txt - NOT gpg_keyfile.priv!) 加密另一台机器上的文件:
1:导入公共文件和ownertrust
gpg --import gpg_keyfile.pub
gpg --import-ownertrust gpg_ownertrust.txt
2:用公钥加密文件
gpg -e -r marklahn@domain.com configfile.ini
configfile.ini.gpg现在应该存在,可以提交到repo

现在,当想要再次解密文件时,需要进行一些额外的操作以确保系统不会保存私钥。 1:首先,私钥受密码保护,因此解密私钥:
gpg --batch --passphrase password1234 gpg_keyfile.priv.gpg
2:然后创建一个临时的gpg目录
mkdir /tmp/gnupg
3:将私钥导入临时钥匙串
gpg --homedir /tmp/gnupg --import gpg_keyfile.priv
4:使用现在持有私钥的临时密钥链来解密文件
gpg --homedir /tmp/gnupg configfile.ini.gpg
5:确保立即删除临时钥匙串和私钥
rm -rf /tmp/gnupg gpg_keyfile.priv

这对我有用,现在我可以将一个分支克隆到一个新系统上,轻松设置我的应用程序并解密我的配置文件只有一个密码。

相关问题