まず、PBEWithHmacSHA512AndAES_128
を使用する暗号化と復号化にJava(JDK 8)を使用してコードを書きました。PBEWithHmacSHA512AndAES_128とAESモード(GCMなど)
しかし、それがAESならば、GCMのようなモードを使って整合性をチェックするにはどうすればいいですか?
一方、AES/GCM/NoPadding
とPBKDF2WithHmacSHA256
を併用することができます。手段鍵はPBKDF2WithHmacSHA256
を使用して生成され、AES/GCMで使用されます。
しかし、私は
PBEWithHmacSHA512AndAES_128
を使用してキーを生成するソースを見つけて、AES/GCM を使うのに苦労AMまたはそれが可能だ場合でも、または、それは理にかなっていますか?PBEWithHmacSHA512AndAES_128
を使用して生成されたキーは、常に9バイトです。その場合、AES 128には16バイトのサイズのキーが必要で、キーが9バイトとして生成される方法がありますか? 2つの質問があり、あなたが見ることができるようにPBEWithHmacSHA512AndAES_128
private byte[] getRandomNumber(final int size) throws NoSuchAlgorithmException { SecureRandom secureRandom = SecureRandom.getInstanceStrong(); byte[] randomBytes = new byte[size]; secureRandom.nextBytes(randomBytes); return randomBytes; } private SecretKey getPBE_AES_Key(final String password, final byte[] salt) { try { char[] passwdData = password.toCharArray(); PBEKeySpec pbeKeySpec = new PBEKeySpec(passwdData, salt, 4096, 128); SecretKeyFactory keyFactory = SecretKeyFactory.getInstance("PBEWithHmacSHA256AndAES_128"); SecretKey pbeKey = keyFactory.generateSecret(pbeKeySpec); return pbeKey; // <-- size of this byte array is 9 - I thought it should be 16 since its AES } catch (NoSuchAlgorithmException | InvalidKeySpecException ex) { throw new OperationFailedException(ex.getMessage(), ex); } } public String encrypt_PBE_AES(final String plaintext, final String password) { try { byte[] ivBytes = getRandomNumber(16); byte[] saltBytes = getRandomNumber(16); byte[] dataToEncrypt = plaintext.getBytes("UTF-8"); Cipher cipher = Cipher.getInstance("PBEWithHmacSHA256AndAES_128"); IvParameterSpec ivParameterSpec = new IvParameterSpec(ivBytes); PBEParameterSpec pbeParameterSpec = new PBEParameterSpec(saltBytes, 4096, ivParameterSpec); cipher.init(Cipher.ENCRYPT_MODE, getPBE_AES_Key(password, saltBytes), pbeParameterSpec); byte[] encryptedData = cipher.doFinal(dataToEncrypt); byte[] ivWithSalt = ArrayUtils.addAll(ivBytes, saltBytes); byte[] encryptedDataWithIVAndSalt = ArrayUtils.addAll(ivWithSalt, encryptedData); String encodedData = Base64.getUrlEncoder().encodeToString(encryptedDataWithIVAndSalt); return encodedData; } catch (NoSuchAlgorithmException | NoSuchPaddingException | InvalidKeyException | IllegalBlockSizeException | BadPaddingException | IOException | InvalidAlgorithmParameterException ex) { throw new OperationFailedException(ex.getMessage(), ex); } } public String decrypt_PBE_AES(final String ciphertext, final String password) { try { byte[] encryptedDataWithIVAndSalt = Base64.getUrlDecoder().decode(ciphertext); byte[] ivBytes = ArrayUtils.subarray(encryptedDataWithIVAndSalt, 0, 16); byte[] saltBytes = ArrayUtils.subarray(encryptedDataWithIVAndSalt, 16, 16 + 16); byte[] dataToDecrypt = ArrayUtils.subarray(encryptedDataWithIVAndSalt, 16 + 16, encryptedDataWithIVAndSalt.length); Cipher cipher = Cipher.getInstance("PBEWithHmacSHA256AndAES_128"); IvParameterSpec ivParameterSpec = new IvParameterSpec(ivBytes); PBEParameterSpec pbeParameterSpec = new PBEParameterSpec(saltBytes, 4096, ivParameterSpec); cipher.init(Cipher.DECRYPT_MODE, getPBE_AES_Key(password, saltBytes), pbeParameterSpec); byte[] decryptedData = cipher.doFinal(dataToDecrypt); return new String(decryptedData, "UTF-8"); } catch (NoSuchAlgorithmException | NoSuchPaddingException | InvalidKeyException | UnsupportedEncodingException | IllegalBlockSizeException | BadPaddingException | InvalidAlgorithmParameterException ex) { throw new OperationFailedException(ex.getMessage(), ex); } }
使用して、この中に任意のヘルプ/明確化が高く評価さよろしく
...
コード...
a)私のコードでは、IVと暗号文を暗号文で保持しています。私はAES/GCMを使ってIV +塩全体の完全性をチェックしたかったのです。
b)キーのバイト[]が9バイトなのはなぜですか?私は[email protected]
として入力を与えた場合(生成されたキーは9バイトです - 最初の質問は、下記答えているしかし2番目の質問 - 。私はpbeKey.getEncoded()
の長さとその9
多くのおかげ
更新を確認してい!https://crypto.stackexchange.com/questions/46849/pbewithhmacsha512andaes-128-and-aes-modes-like-gcm
おかげ一方みんな
ようこそstackoverflow。バックライン文字を使用してインラインコードをフォーマットできることをご存知でしたか? '\' code \ '' < - these –