对 Android 加密的工作原理感到困惑

发布于 2024-11-07 07:04:24 字数 352 浏览 0 评论 0原文

好的,我正在开发一个应用程序,我想在用户的 SD 卡上存储一个文件,但我希望该文件被加密。我研究了几个使用 DES 加密来加密文件和数据的网站,但我对某些事情感到困惑。我看到的所有示例都使用这样的行:

SecretKey key = KeyGenerator.getInstance("DES").generateKey();

我遇到的问题是我得到了要加密的密钥,显然我需要相同的密钥来解密。但这似乎是一个悖论,因为如果我将密钥存储在文件或数据库中,那么其他人就无法获得密钥并解密我的文件吗?也许我遗漏了一些东西,但是有没有办法使用提供的密码短语生成密钥?如果有人必须将生成的密钥存储在其他地方,为什么他们不想使用密钥呢?

Okay, I am working on an application and I want to store a file on the user's SD Card, but I want the file encrypted. I've researched several sites that use the DES encryption to encrypt files and data, but I am confused about something. All the examples I'm seen use a line like this:

SecretKey key = KeyGenerator.getInstance("DES").generateKey();

The problem I am having is that I get my key to encrypt and obviously I need the same key to decrypt. But its seems like a paradox because if I store my key in a file or in a database, can't someone get the key and decrypt my file? Maybe I am missing something, but is there a way to generate a key using a supplied pass phrase? And why would someone not want to use a passkey if they must store the generated key somewhere else?

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

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

发布评论

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

评论(3

小姐丶请自重 2024-11-14 07:04:24

我认为有两种情况:

  1. 您信任用户 - 让密钥依赖于某些输入(密码/密码短语)。并用它加密/解密数据。

  2. 你不信任用户——那么你就有麻烦了,你可能会混淆加密/解密算法,但只要机制+密钥存储在设备上,你就会遇到这个问题。

这适用于对称和非对称加密。

I think there are two cases:

  1. You trust the user - let the key be dependent on some input (password / passphrase). and encrypt / decrypt the data with it.

  2. You don't trust the user - then you're in trouble, you might obfuscate the encryption / decryption algorithm, but as long as the mechanism + key is stored on the device, you will have this problem.

This applies to both symmetric and asymmetric encryption.

彡翼 2024-11-14 07:04:24

首先,请不要使用DES。多年来它已经被打破了。请改用 AES

我遇到的问题是我得到
我的加密密钥,显然我需要
解密的密钥相同。

如果您使用对称加密技术,就是这样。否则,请查看 asimmetric 加密

但这似乎是一个悖论,因为
如果我将密钥存储在文件或
数据库,别人无法获取密钥
并解密我的文件?

是的,有人可以做到。

Maybe I am missing something, but is there a way to generate a key using a supplied pass phrase?

您不使用密码短语来使用密钥。通常您会执行以下操作:

  1. 密钥生成
  2. 使用从密码短语派生的对称密钥对生成的密钥进行加密

为什么有人不想使用
密钥(如果他们必须存储)
在其他地方生成密钥?

可能有几个原因。例如,您可以将密钥存储在可移动设备中,并且只需将其连接到计算机即可检索密钥,而无需输入密码。
拥有密码也有其缺点:密码必须记住,可以猜测,如果密码太长,您可能会将其写下来(这与将其存储在文件中几乎是一样的事情)

编辑:

从密码查看 PBKDF2 (相关帖子)。

First of all, please don't use DES. It has been broken from many years. Use AES instead.

The problem I am having is that I get
my key to encrypt and obviously I need
the same key to decrypt.

If you use symmetric cryptography techniques this is it. Otherwise have a look to asimmetric encryption.

But its seems like a paradox because
if I store my key in a file or in a
database, can't someone get the key
and decrypt my file?

Yes, someone could do it.

Maybe I am missing something, but is there a way to generate a key using a supplied pass phrase?

You don't use a the key using a passphrase. Usually you do the following things:

  1. key generation
  2. encrypt the key generated with a symmetric key derived from a passphrase

And why would someone not want to use
a passkey if they must store the
generated key somewhere else?

There could be several reasons. For example you can store the key in a removable device, and you want simply connect it to your computer for retrieving the key, without entering the passphrase.
Having a passphrase has its disadvantage too: passphrase must be remembered, can be guessed, if it's too long probably you'll write it down (and that's pretty the same thing then storing it in a file )

EDIT:

to generate a key from a password have a look at PBKDF2 (related post).

清君侧 2024-11-14 07:04:24

是的,可以使用密码进行加密。

但首先,转储 DES。使用 AES-128。

接受用户的密码并使用 SHA-256 或 SHA-512 生成哈希值。将 AES-128 的哈希值修剪为 128 位。参考这篇文章。

Java AES 和使用我自己的密钥

尽可能使用盐。

关于密码部分的存储。存储哈希值而不是密码。这样就可以防止攻击者生成密钥。要求用户输入强密码。并且不要忘记你的盐也必须很浓。

因此,最后,您只存储密码的哈希值。不存储密码,也不会存储解密密钥(它将在运行时生成)

希望有帮助。

Yeah, it is possible to use a pass-phrase to encrypt.

But first, dump DES. Use AES-128.

Accept the pass-phrase from the user and and generate the hash using SHA-256 or SHA-512. Trim the hash to 128 bits for AES-128. Refer this post.

Java AES and using my own Key

Use a salt when you can.

Regarding the storage of password part. Store the hash and not the password. This way you can prevent the attacker from generating the key. Ask the user to enter strong password. And don't forget that your salt must be very strong too.

So, in the end, you store only the hash of the password. The password is not stored and the key to decrypt will not be stored(It will be generated at run-time)

Hope it helps.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文