在php中如何在特定文件夹中找到已经加密的文件

发布于 2024-08-14 08:10:48 字数 224 浏览 12 评论 0原文

我正在使用 PGP(GNU Privacy Guard)来加密文件。 加密时我删除了加密文件的“.pgp”扩展名。

现在我想知道特定文件夹中哪个文件已经加密。

注意:- 我的目标是......不要对任何文件加密两次......所以在加密任何文件之前......我想检查文件是否已经加密。

在php中我们能找出哪个文件已经加密了吗?

I am using PGP (GNU Privacy Guard) for encrypting the file.
while encrypting i removed the '.pgp' extension of encrypted file.

Now some how i want to know which file is already encrypted in the specific folder.

Note :- my goal is that ... do not encrypt any file twice ... so before encrypt any file .. i want to check is the file already encrypted.

in php can we find out which file is already encrypted ?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

踏雪无痕 2024-08-21 08:10:48

PGP 文件全部以 "-----BEGIN PGP MESSAGE-----" 开头。

所以你可以这样做:

  $content = file_get_contents($filename);
  $encrypted = strpos($content, '-----BEGIN PGP MESSAGE-----') === 0;

PGP file all starts with "-----BEGIN PGP MESSAGE-----".

So you can do something like this:

  $content = file_get_contents($filename);
  $encrypted = strpos($content, '-----BEGIN PGP MESSAGE-----') === 0;
眼睛会笑 2024-08-21 08:10:48

我真的不太了解它是如何工作的,或者如何查看文件的内容来判断它是否已正确加密,但是您可以尝试解密它们吗?如果您知道您只处理纯文本文件,您可以检查解密数据的前 500 个字节,如果有奇怪的字符(在标准 az AZ 0-9 + 标点符号之外),那么这可能是一个线索文件加密。

我知道,这确实是一个半途而废的答案,但它有点长,不适合评论。

I really don't know much about how it works, or how you could look at the contents of the file to tell if it is encrypted properly, but could you try decrypting them? If you know you're only working with plain text files, you could examine the first 500 bytes of the decrypted data and if there's strange characters (outside the standard a-z A-Z 0-9 + punctuation, etc), then that could be a clue that the file wasn't encrypted.

This really is a half-arsed answer, I know, but it was a bit long to fit into a comment.

没有伤那来痛 2024-08-21 08:10:48

除非您了解加密中使用的算法,否则您不能。一旦理解了它,您就可以应用它来检查文件是否已经加密。

还要检查以确保 PGP 中已有一个功能可用于检查某些内容是否已加密。这通常出现在加密解决方案中。

谢谢

You can't unless you understand the algorithm used in the encryption. Once you understand it, you can apply that to check whether a file is already encrypted.

Also check to make sure that there is already a function available in PGP for checking if something is already encrypted. This is usually present in encryption solutions.

Thanks

花心好男孩 2024-08-21 08:10:48

OpenPGP 数据有两种可能的格式:二进制格式和 ascii 装甲格式。

Ascii 装甲文件很容易通过查找“-----BEGIN PGP MESSAGE-----”来识别,也可以使用 unix 命令 file 来完成:

$ file encrypted
encrypted: PGP message

@ZZ_Coders 答案完全没问题如果您只处理 ascii 装甲加密文件。

如果它显示其他内容,则它不是 OpenPGP 消息 - 或二进制格式。这不太容易识别(至少我不知道您可以查找哪些魔术包),但您可以轻松地使用 gpg 命令来测试该文件:

$ gpg --list-only --list-packets encrypted
:pubkey enc packet: version 3, algo 1, keyid DEAFBEEFDEADBEEF
    data: [2048 bits]
:encrypted data packet:
    length: 73
    mdc_method: 2

如果不是加密后,响应将如下所示:

$ gpg --list-only --list-packets something_else
gpg: no valid OpenPGP data found.

在 PHP 中,您可以使用以下代码来检查文件是否经过 OpenPGP 加密:

if (strpos(`gpg --list-only --list-packets my_file.txt 2>&1`,
           'encrypted data packet'))
  echo "encrypted file";

There are two possible formats for OpenPGP data, binary and ascii armored.

Ascii-armored files are easy to recognize by looking for "-----BEGIN PGP MESSAGE-----" which can also be done using the unix command file:

$ file encrypted
encrypted: PGP message

@ZZ_Coders answer is totally fine if you're only dealing with ascii armored encrypted files.

If it shows something else, it's not an OpenPGP message - or in binary format. This isn't as easy to recognize (at least I don't know which magic packets you could look for), but you can easily use the gpg command to test the file:

$ gpg --list-only --list-packets encrypted
:pubkey enc packet: version 3, algo 1, keyid DEAFBEEFDEADBEEF
    data: [2048 bits]
:encrypted data packet:
    length: 73
    mdc_method: 2

If it isn't encrypted, response will look like this:

$ gpg --list-only --list-packets something_else
gpg: no valid OpenPGP data found.

In PHP, you could use this code to check if a file is OpenPGP-encrypted:

if (strpos(`gpg --list-only --list-packets my_file.txt 2>&1`,
           'encrypted data packet'))
  echo "encrypted file";
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文