使用 AES 和密码对数据进行正确/安全的加密

发布于 2025-01-08 18:16:59 字数 761 浏览 0 评论 0原文

现在,这就是我正在做的事情: 1. SHA-1密码,如“pass123”,使用十六进制解码的前32个字符作为密钥 2. 使用 AES-256 进行加密,无论默认参数是什么 ^这样足够安全吗?

我需要我的应用程序使用密码安全地加密数据。当我用谷歌搜索这个时,会出现太多不同的事情,还有一些我不明白的事情。我问这个问题是一个一般性问题,而不是任何特定的编码语言(尽管我计划将其与 Java 和 iOS 一起使用)。

因此,现在我正在尝试更正确地执行此操作,请按照我的想法进行操作:

  1. 输入是密码,例如“pass123”,数据是 我想要加密的内容,例如“银行帐户是038414838,密码是5931”

  2. 使用 PBKDF2 从密码中派生密钥。参数: 1000 次迭代 长度为256位 盐——这个让我很困惑,因为我不知道从哪里得到盐,我只是做一个吗?例如,我的所有加密都将始终使用盐“F”(因为显然盐是 8 位,只是一个字符)

  3. 现在我拿了这个密钥,我要散列它吗?我应该使用 SHA-256 之类的东西吗?这样安全吗?什么是 HMAC?我应该用那个吗? 注意:我是否需要同时执行步骤 2 和 3,还是只执行其中一个即可?

  4. 现在我有了 256 位密钥来进行加密。因此,我使用 AES 执行加密,但这里还有另一个令人困惑的部分(参数)。 我不太确定要使用哪些不同的“模式”,显然有 CBC 和 EBC 以及其他一些模式 我也不确定“初始化向量”,我是否只是编造一个并始终使用该向量? 那么其他选项呢?什么是 PKCS7Padding?

Right now, this is what I am doing:
1. SHA-1 a password like "pass123", use the first 32 characters of the hexadecimal decoding for the key
2. Encrypt with AES-256 with just whatever the default parameters are
^Is that secure enough?

I need my application to encrypt data with a password, and securely. There are too many different things that come up when I google this and some things that I don't understand about it too. I am asking this as a general question, not any specific coding language (though I'm planning on using this with Java and with iOS).

So now that I am trying to do this more properly, please follow what I have in mind:

  1. Input is a password such as "pass123" and the data is
    what I want to encrypt such as "The bank account is 038414838 and the pin is 5931"

  2. Use PBKDF2 to derive a key from the password. Parameters:
    1000 iterations
    length of 256bits
    Salt - this one confuses me because I am not sure where to get the salt from, do I just make one up? As in, all my encryptions would always use the salt "F" for example (since apparently salts are 8bits which is just one character)

  3. Now I take this key, and do I hash it?? Should I use something like SHA-256? Is that secure? And what is HMAC? Should I use that?
    Note: Do I need to perform both steps 2 and 3 or is just one or the other okay?

  4. Okay now I have the 256-bit key to do the encryption with. So I perform the encryption using AES, but here's yet another confusing part (the parameters).
    I'm not really sure what are the different "modes" to use, apparently there's like CBC and EBC and a bunch of others
    I also am not sure about the "Initialization Vector," do I just make one up and always use that one?
    And then what about other options, what is PKCS7Padding?

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

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

发布评论

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

评论(1

深海不蓝 2025-01-15 18:16:59

对于您的初始观点:

  1. 使用十六进制显然将密钥大小分成两半。基本上,您使用的是 AES-128 安全性。这并不是说这很糟糕,但您也可以选择 AES-128 并使用 16 字节。
  2. SHA-1 对于密钥派生来说相对安全,但由于彩虹表的存在/创建,不应该直接使用它。为此,您需要像 PBKDF2 这样的函数,它使用迭代计数和盐。

至于解决方案:

  1. 如果可以避免,则不应加密 PIN。请确保您的密码足够安全,允许使用密码短语。
  2. 为每个密码创建一个随机数,并将盐(16 字节)与 PBKDF2 的输出一起保存。尽管您可能希望包含系统机密以增加一些额外的安全性,但盐不必是秘密的。盐和密码经过哈希处理,因此它们可以具有与 PBKDF2 兼容的任意长度。
  3. 不,您只需保存 PBKDF2 生成的秘密,让 PBKDF2 在需要时生成更多数据。
  4. 切勿使用 ECB(而非 EBC)。使用 CBC 作为最小值。请注意,CBC 加密提供完整性检查(有人可能会更改密文,而您可能永远不知道)或真实性。为此,您可能需要添加额外的 MAC、HMAC 或使用 GCM 等加密模式。 PKCS7Padding(在大多数情况下与 PKCS5Padding 相同)是一种添加虚假数据以获得 N * [blocksize] 字节的简单方法,这是块明智加密所需的。

不要忘记在密文前面添加(随机)IV,以防您重复使用加密密钥。 IV 类似于盐,但应该恰好是 [blocksize] 字节(对于 AES 为 16)。

For your initial points:

  1. Using hexadecimals clearly splits the key size in half. Basically, you are using AES-128 security wise. Not that that is bad, but you might also go for AES-128 and use 16 bytes.
  2. SHA-1 is relatively safe for key derivation, but it shouldn't be used directly because of the existence/creation of rainbow tables. For this you need a function like PBKDF2 which uses an iteration count and salt.

As for the solution:

  1. You should not encrypt PIN's if that can be avoided. Please make sure your passwords are safe enough, allow pass phrases.
  2. Create a random number per password and save the salt (16 bytes) with the output of PBKDF2. The salt does not have to be secret, although you might want to include a system secret to add some extra security. The salt and password are hashed, so they may have any length to be compatible with PBKDF2.
  3. No, you just save the secret generated by the PBKDF2, let the PBKDF2 generate more data when required.
  4. Never use ECB (not EBC). Use CBC as minimum. Note that CBC encryption does not provide integrity checking (somebody might change the cipher text and you might never know it) or authenticity. For that, you might want to add an additional MAC, HMAC or use an encryption mode such as GCM. PKCS7Padding (identical to PKCS5Padding in most occurences) is a simple method of adding bogus data to get N * [blocksize] bytes, required by block wise encryption.

Don't forget to prepend a (random) IV to your cipher text in case you reuse your encryption keys. An IV is similar to a salt, but should be exactly [blocksize] bytes (16 for AES).

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