2017-04-05 9 views
1

まず、PBEWithHmacSHA512AndAES_128を使用する暗号化と復号化にJava(JDK 8)を使用してコードを書きました。PBEWithHmacSHA512AndAES_128とAESモード(GCMなど)

しかし、それがAESならば、GCMのようなモードを使って整合性をチェックするにはどうすればいいですか?

一方、AES/GCM/NoPaddingPBKDF2WithHmacSHA256を併用することができます。手段鍵はPBKDF2WithHmacSHA256を使用して生成され、AES/GCMで使用されます。

  1. しかし、私はPBEWithHmacSHA512AndAES_128を使用してキーを生成するソースを見つけて、AES/GCM を使うのに苦労AMまたはそれが可能だ場合でも、または、それは理にかなっていますか?

  2. 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

おかげ一方みんな

+0

ようこそstackoverflow。バックライン文字を使用してインラインコードをフォーマットできることをご存知でしたか? '\' code \ '' < - these –

答えて

2

で答えてしまった - 私はと一緒にAES/GCM/NoPaddingを使用することができています3210。

パーフェクト。あなたはAES-128にデフォルト設定したいと思うようです。正しく実装されていれば、上記には間違いがほとんどなく、SHA-512に変更しても、セキュリティがあれば(もしあれば)助けにならないでしょう。

しかし、私はそれが理にかなっている場合PBEWithHmacSHA512AndAES_128を使用してキーを生成するソースを見つけて、それが可能だ場合でも、AES/GCMを使用するか、またはのに苦労していますか?

AES_128は、モードが完全性を使用していないことを既に示しています。これは、CBCにデフォルト設定されているオール・オア・ナッシング・スキームのようなものです。私は上記のように、あなたが持っているものを保つだけです。

第二にPBEWithHmacSHA512AndAES_128を使用して生成した鍵は常に9バイトである - それはケースだならば、私は16のバイトサイズのAES 128人のニーズキーを疑問に思って、どのキーが9つのバイトとして生成なっていますか?

これは正しくありません。間違いなく、キーは128ビット/ 16バイトですが、間違った情報を取得しているだけです。下にあるバイト配列を最初に16進数に変換するのではなく、直接出力しようとします。

+0

2番目の質問についてもっと正確な発言をする前に、コードを表示する必要があります。 –

+0

@ maarten-bodewesにお返事ありがとうございます...私はコードブロックを追加して2つの質問を言い換えました...第二に、Javaで 'PBEWithHmacSHA512AndAES_128'を使用する場合、GCMを使用するメカニズムはありません。もしそうなら、私は 'PBEWithHmacSHA512AndAES_128'を使ったとき、IV +塩のitegrityチェックのためにHMACに落ちなければなりません...意味がありますか? –

関連する問題