@Test public void verifyTextSizeLimit() throws Exception { crypto1.encryptSecret(Strings.repeat(".", 16 * 1024)); expectedException.expect(IllegalArgumentException.class); crypto1.encryptSecret(Strings.repeat(".", 16 * 1024 + 1)); }
@Override public String encryptSecret(String plainText) { if (plainText.length() > MAX_PLAINTEXT_LENGTH) { throw new IllegalArgumentException("Too long text"); } byte[] nonce = generateNonce(); Cipher cipher = cipher(ENCRYPT_MODE, sharedSecret, nonce); byte[] plainTextBytes = plainText.getBytes(UTF_8); if (plainTextBytes.length > MAX_PLAINTEXT_LENGTH) { throw new IllegalArgumentException("Too long text"); } int recordContentsLength = LENGTH_SIZE + plainTextBytes.length; int recordLength = RECORD_SIZE_ALIGNMENT * ((recordContentsLength + RECORD_SIZE_ALIGNMENT - 1) / RECORD_SIZE_ALIGNMENT); byte[] recordBytes = new byte[recordLength]; ByteBuffer recordBuffer = ByteBuffer.wrap(recordBytes); recordBuffer.putInt(plainTextBytes.length); recordBuffer.put(plainTextBytes); byte[] cipherText; try { cipherText = cipher.doFinal(recordBytes); } catch (IllegalBlockSizeException | BadPaddingException e) { throw Throwables.propagate(e); } byte[] opaque = new byte[WRAPPING_SIZE + cipherText.length]; ByteBuffer output = ByteBuffer.wrap(opaque); output.put(NAME_BYTES); output.putInt(VERSION_2); output.putInt(TERM); output.put(nonce); output.put(cipherText); assert output.remaining() == 0; return Base64.getEncoder().encodeToString(opaque); }
AESGCMSecretCrypto implements SecretCrypto { @Override public String encryptSecret(String plainText) { if (plainText.length() > MAX_PLAINTEXT_LENGTH) { throw new IllegalArgumentException("Too long text"); } byte[] nonce = generateNonce(); Cipher cipher = cipher(ENCRYPT_MODE, sharedSecret, nonce); byte[] plainTextBytes = plainText.getBytes(UTF_8); if (plainTextBytes.length > MAX_PLAINTEXT_LENGTH) { throw new IllegalArgumentException("Too long text"); } int recordContentsLength = LENGTH_SIZE + plainTextBytes.length; int recordLength = RECORD_SIZE_ALIGNMENT * ((recordContentsLength + RECORD_SIZE_ALIGNMENT - 1) / RECORD_SIZE_ALIGNMENT); byte[] recordBytes = new byte[recordLength]; ByteBuffer recordBuffer = ByteBuffer.wrap(recordBytes); recordBuffer.putInt(plainTextBytes.length); recordBuffer.put(plainTextBytes); byte[] cipherText; try { cipherText = cipher.doFinal(recordBytes); } catch (IllegalBlockSizeException | BadPaddingException e) { throw Throwables.propagate(e); } byte[] opaque = new byte[WRAPPING_SIZE + cipherText.length]; ByteBuffer output = ByteBuffer.wrap(opaque); output.put(NAME_BYTES); output.putInt(VERSION_2); output.putInt(TERM); output.put(nonce); output.put(cipherText); assert output.remaining() == 0; return Base64.getEncoder().encodeToString(opaque); } }
AESGCMSecretCrypto implements SecretCrypto { @Override public String encryptSecret(String plainText) { if (plainText.length() > MAX_PLAINTEXT_LENGTH) { throw new IllegalArgumentException("Too long text"); } byte[] nonce = generateNonce(); Cipher cipher = cipher(ENCRYPT_MODE, sharedSecret, nonce); byte[] plainTextBytes = plainText.getBytes(UTF_8); if (plainTextBytes.length > MAX_PLAINTEXT_LENGTH) { throw new IllegalArgumentException("Too long text"); } int recordContentsLength = LENGTH_SIZE + plainTextBytes.length; int recordLength = RECORD_SIZE_ALIGNMENT * ((recordContentsLength + RECORD_SIZE_ALIGNMENT - 1) / RECORD_SIZE_ALIGNMENT); byte[] recordBytes = new byte[recordLength]; ByteBuffer recordBuffer = ByteBuffer.wrap(recordBytes); recordBuffer.putInt(plainTextBytes.length); recordBuffer.put(plainTextBytes); byte[] cipherText; try { cipherText = cipher.doFinal(recordBytes); } catch (IllegalBlockSizeException | BadPaddingException e) { throw Throwables.propagate(e); } byte[] opaque = new byte[WRAPPING_SIZE + cipherText.length]; ByteBuffer output = ByteBuffer.wrap(opaque); output.put(NAME_BYTES); output.putInt(VERSION_2); output.putInt(TERM); output.put(nonce); output.put(cipherText); assert output.remaining() == 0; return Base64.getEncoder().encodeToString(opaque); } AESGCMSecretCrypto(String sharedSecretBase64); }
AESGCMSecretCrypto implements SecretCrypto { @Override public String encryptSecret(String plainText) { if (plainText.length() > MAX_PLAINTEXT_LENGTH) { throw new IllegalArgumentException("Too long text"); } byte[] nonce = generateNonce(); Cipher cipher = cipher(ENCRYPT_MODE, sharedSecret, nonce); byte[] plainTextBytes = plainText.getBytes(UTF_8); if (plainTextBytes.length > MAX_PLAINTEXT_LENGTH) { throw new IllegalArgumentException("Too long text"); } int recordContentsLength = LENGTH_SIZE + plainTextBytes.length; int recordLength = RECORD_SIZE_ALIGNMENT * ((recordContentsLength + RECORD_SIZE_ALIGNMENT - 1) / RECORD_SIZE_ALIGNMENT); byte[] recordBytes = new byte[recordLength]; ByteBuffer recordBuffer = ByteBuffer.wrap(recordBytes); recordBuffer.putInt(plainTextBytes.length); recordBuffer.put(plainTextBytes); byte[] cipherText; try { cipherText = cipher.doFinal(recordBytes); } catch (IllegalBlockSizeException | BadPaddingException e) { throw Throwables.propagate(e); } byte[] opaque = new byte[WRAPPING_SIZE + cipherText.length]; ByteBuffer output = ByteBuffer.wrap(opaque); output.put(NAME_BYTES); output.putInt(VERSION_2); output.putInt(TERM); output.put(nonce); output.put(cipherText); assert output.remaining() == 0; return Base64.getEncoder().encodeToString(opaque); } AESGCMSecretCrypto(String sharedSecretBase64); @Override String encryptSecret(String plainText); @Override String decryptSecret(String encryptedBase64); @Override String getName(); }
AESGCMSecretCrypto implements SecretCrypto { @Override public String encryptSecret(String plainText) { if (plainText.length() > MAX_PLAINTEXT_LENGTH) { throw new IllegalArgumentException("Too long text"); } byte[] nonce = generateNonce(); Cipher cipher = cipher(ENCRYPT_MODE, sharedSecret, nonce); byte[] plainTextBytes = plainText.getBytes(UTF_8); if (plainTextBytes.length > MAX_PLAINTEXT_LENGTH) { throw new IllegalArgumentException("Too long text"); } int recordContentsLength = LENGTH_SIZE + plainTextBytes.length; int recordLength = RECORD_SIZE_ALIGNMENT * ((recordContentsLength + RECORD_SIZE_ALIGNMENT - 1) / RECORD_SIZE_ALIGNMENT); byte[] recordBytes = new byte[recordLength]; ByteBuffer recordBuffer = ByteBuffer.wrap(recordBytes); recordBuffer.putInt(plainTextBytes.length); recordBuffer.put(plainTextBytes); byte[] cipherText; try { cipherText = cipher.doFinal(recordBytes); } catch (IllegalBlockSizeException | BadPaddingException e) { throw Throwables.propagate(e); } byte[] opaque = new byte[WRAPPING_SIZE + cipherText.length]; ByteBuffer output = ByteBuffer.wrap(opaque); output.put(NAME_BYTES); output.putInt(VERSION_2); output.putInt(TERM); output.put(nonce); output.put(cipherText); assert output.remaining() == 0; return Base64.getEncoder().encodeToString(opaque); } AESGCMSecretCrypto(String sharedSecretBase64); @Override String encryptSecret(String plainText); @Override String decryptSecret(String encryptedBase64); @Override String getName();  }