Android 上的 PBEWITHSHA256AND256BITAES-CBC-BC:密钥生成和密码的迭代次数?
PBEWITHSHA256AND256BITAES-CBC-BC on Android : number of iterations for keygeneration and cipher?
我将 PBEWITHSHA256AND256BITAES-CBC-BC 与 BouncyCastle 一起使用。
public static String algorithm = "PBEWITHSHA256AND256BITAES-CBC-BC";
我用这个方法生成秘钥:
private void generateSK(char[] passPhrase, byte[] salt) throws NoSuchAlgorithmException, InvalidKeySpecException, NoSuchPaddingException, InvalidAlgorithmParameterException, InvalidKeyException {
pbeParamSpecKey = new PBEParameterSpec(salt,1000);
PBEKeySpec pbeKeySpec = new PBEKeySpec(passPhrase);
SecretKeyFactory secretKeyFactory;
secretKeyFactory = SecretKeyFactory.getInstance(algorithm);
secretKey = secretKeyFactory.generateSecret(pbeKeySpec);
}
然后生成密码对象(用于加密或解密):
protected Cipher getCipher(int mode) {
try {
Cipher cipher = Cipher.getInstance(algorithm);
cipher.init(mode, secretKey, pbeParamSpecKey);
return cipher;
}catch (Exception e) {
e.printStackTrace();
return null;
}
}
pbeParamSpecKey 需要相同吗? "important"(在安全方面)生成密钥的迭代次数和生成 Cipher 对象的迭代次数是多少?它们可以不同吗?
The pbeParamSpecKey need to be the same? How much is "important" (in terms of security) the number of iterations generating the key and the one generating the Cipher object?
这是一个需要您自己回答的难题。一般的指导是,尽可能大而不打扰用户太多,但现在 1000 次迭代有点低。
接下来您需要做的是尝试在您的用户组可能拥有的不同设备上对您的应用程序进行基准测试。然后你可以相应地微调。
也许您可以稍微重新设计您的应用程序,以便在空闲阶段在后台进行密钥派生,因此可能需要更长的时间。但是,这可能会引入其他问题,例如保持派生密钥安全且不可泄漏。
Can they be different?
AES 是一种对称分组密码,因此需要 相同的 密钥进行加密和解密。要生成same密钥,需要使用same密码、salt和PBKDF2的迭代次数。
我将 PBEWITHSHA256AND256BITAES-CBC-BC 与 BouncyCastle 一起使用。
public static String algorithm = "PBEWITHSHA256AND256BITAES-CBC-BC";
我用这个方法生成秘钥:
private void generateSK(char[] passPhrase, byte[] salt) throws NoSuchAlgorithmException, InvalidKeySpecException, NoSuchPaddingException, InvalidAlgorithmParameterException, InvalidKeyException {
pbeParamSpecKey = new PBEParameterSpec(salt,1000);
PBEKeySpec pbeKeySpec = new PBEKeySpec(passPhrase);
SecretKeyFactory secretKeyFactory;
secretKeyFactory = SecretKeyFactory.getInstance(algorithm);
secretKey = secretKeyFactory.generateSecret(pbeKeySpec);
}
然后生成密码对象(用于加密或解密):
protected Cipher getCipher(int mode) {
try {
Cipher cipher = Cipher.getInstance(algorithm);
cipher.init(mode, secretKey, pbeParamSpecKey);
return cipher;
}catch (Exception e) {
e.printStackTrace();
return null;
}
}
pbeParamSpecKey 需要相同吗? "important"(在安全方面)生成密钥的迭代次数和生成 Cipher 对象的迭代次数是多少?它们可以不同吗?
The pbeParamSpecKey need to be the same? How much is "important" (in terms of security) the number of iterations generating the key and the one generating the Cipher object?
这是一个需要您自己回答的难题。一般的指导是,尽可能大而不打扰用户太多,但现在 1000 次迭代有点低。
接下来您需要做的是尝试在您的用户组可能拥有的不同设备上对您的应用程序进行基准测试。然后你可以相应地微调。
也许您可以稍微重新设计您的应用程序,以便在空闲阶段在后台进行密钥派生,因此可能需要更长的时间。但是,这可能会引入其他问题,例如保持派生密钥安全且不可泄漏。
Can they be different?
AES 是一种对称分组密码,因此需要 相同的 密钥进行加密和解密。要生成same密钥,需要使用same密码、salt和PBKDF2的迭代次数。