@Test(expected = CryptoException.class) public void testDecryptInvalidTextLength() throws CryptoException { cryptoService.decrypt("123", KEY); }
@Override public String decrypt(String encryptedText, String key) throws CryptoException { checkParameters("encryptedText", encryptedText); checkParameters("key", key); try { ByteBuffer buffer = ByteBuffer.wrap(Base64.decodeBase64(encryptedText)); int minLength = SALT_SIZE + 1; if (buffer.capacity() < minLength) { throw new CryptoException(logMessage.getMessage(INVALID_ENCRYPTED_TXT, encryptedText), logMessage.getMessage(INVALID_ENCRYPTED_TXT_SOLUTION, String.valueOf(buffer.capacity()), String.valueOf(minLength))); } byte[] saltBytes = new byte[SALT_SIZE]; buffer.get(saltBytes, 0, saltBytes.length); byte[] encryptedTextBytes = new byte[buffer.capacity() - saltBytes.length]; buffer.get(encryptedTextBytes); byte[] pbKey = PBKDF.PBKDF2_SHA256(key.getBytes(CHARSET), saltBytes, NUMBER_OF_ITERATIONS); byte[] decryptedTextBytes = clientCryptoHandler.decryptMsg(pbKey, encryptedTextBytes); if (decryptedTextBytes != null) { return new String(decryptedTextBytes, CHARSET); } return null; } catch (SymphonyEncryptionException | InvalidDataException e) { throw new CryptoException(logMessage.getMessage(GENERAL_CRYPTO_ERROR), e, logMessage.getMessage(GENERAL_CRYPTO_ERROR_SOLUTION, e.getMessage())); } catch (SymphonyInputException e) { throw new CryptoException(logMessage.getMessage(INVALID_INPUT_TXT), e, logMessage.getMessage(INVALID_INPUT_TXT_SOLUTION, e.getMessage())); } catch (CiphertextTransportVersionException | CiphertextTransportIsEmptyException e) { throw new CryptoException(logMessage.getMessage(INVALID_CIPHER_TXT), e, logMessage.getMessage(INVALID_CIPHER_TXT_SOLUTION, e.getMessage())); } catch (UnsupportedEncodingException e) { throw new CryptoException(logMessage.getMessage(UNSUPPORTED_ENCODING), e, logMessage.getMessage(UNSUPPORTED_ENCODING_SOLUTION, CHARSET)); } }
CryptoServiceImpl implements CryptoService { @Override public String decrypt(String encryptedText, String key) throws CryptoException { checkParameters("encryptedText", encryptedText); checkParameters("key", key); try { ByteBuffer buffer = ByteBuffer.wrap(Base64.decodeBase64(encryptedText)); int minLength = SALT_SIZE + 1; if (buffer.capacity() < minLength) { throw new CryptoException(logMessage.getMessage(INVALID_ENCRYPTED_TXT, encryptedText), logMessage.getMessage(INVALID_ENCRYPTED_TXT_SOLUTION, String.valueOf(buffer.capacity()), String.valueOf(minLength))); } byte[] saltBytes = new byte[SALT_SIZE]; buffer.get(saltBytes, 0, saltBytes.length); byte[] encryptedTextBytes = new byte[buffer.capacity() - saltBytes.length]; buffer.get(encryptedTextBytes); byte[] pbKey = PBKDF.PBKDF2_SHA256(key.getBytes(CHARSET), saltBytes, NUMBER_OF_ITERATIONS); byte[] decryptedTextBytes = clientCryptoHandler.decryptMsg(pbKey, encryptedTextBytes); if (decryptedTextBytes != null) { return new String(decryptedTextBytes, CHARSET); } return null; } catch (SymphonyEncryptionException | InvalidDataException e) { throw new CryptoException(logMessage.getMessage(GENERAL_CRYPTO_ERROR), e, logMessage.getMessage(GENERAL_CRYPTO_ERROR_SOLUTION, e.getMessage())); } catch (SymphonyInputException e) { throw new CryptoException(logMessage.getMessage(INVALID_INPUT_TXT), e, logMessage.getMessage(INVALID_INPUT_TXT_SOLUTION, e.getMessage())); } catch (CiphertextTransportVersionException | CiphertextTransportIsEmptyException e) { throw new CryptoException(logMessage.getMessage(INVALID_CIPHER_TXT), e, logMessage.getMessage(INVALID_CIPHER_TXT_SOLUTION, e.getMessage())); } catch (UnsupportedEncodingException e) { throw new CryptoException(logMessage.getMessage(UNSUPPORTED_ENCODING), e, logMessage.getMessage(UNSUPPORTED_ENCODING_SOLUTION, CHARSET)); } } }
CryptoServiceImpl implements CryptoService { @Override public String decrypt(String encryptedText, String key) throws CryptoException { checkParameters("encryptedText", encryptedText); checkParameters("key", key); try { ByteBuffer buffer = ByteBuffer.wrap(Base64.decodeBase64(encryptedText)); int minLength = SALT_SIZE + 1; if (buffer.capacity() < minLength) { throw new CryptoException(logMessage.getMessage(INVALID_ENCRYPTED_TXT, encryptedText), logMessage.getMessage(INVALID_ENCRYPTED_TXT_SOLUTION, String.valueOf(buffer.capacity()), String.valueOf(minLength))); } byte[] saltBytes = new byte[SALT_SIZE]; buffer.get(saltBytes, 0, saltBytes.length); byte[] encryptedTextBytes = new byte[buffer.capacity() - saltBytes.length]; buffer.get(encryptedTextBytes); byte[] pbKey = PBKDF.PBKDF2_SHA256(key.getBytes(CHARSET), saltBytes, NUMBER_OF_ITERATIONS); byte[] decryptedTextBytes = clientCryptoHandler.decryptMsg(pbKey, encryptedTextBytes); if (decryptedTextBytes != null) { return new String(decryptedTextBytes, CHARSET); } return null; } catch (SymphonyEncryptionException | InvalidDataException e) { throw new CryptoException(logMessage.getMessage(GENERAL_CRYPTO_ERROR), e, logMessage.getMessage(GENERAL_CRYPTO_ERROR_SOLUTION, e.getMessage())); } catch (SymphonyInputException e) { throw new CryptoException(logMessage.getMessage(INVALID_INPUT_TXT), e, logMessage.getMessage(INVALID_INPUT_TXT_SOLUTION, e.getMessage())); } catch (CiphertextTransportVersionException | CiphertextTransportIsEmptyException e) { throw new CryptoException(logMessage.getMessage(INVALID_CIPHER_TXT), e, logMessage.getMessage(INVALID_CIPHER_TXT_SOLUTION, e.getMessage())); } catch (UnsupportedEncodingException e) { throw new CryptoException(logMessage.getMessage(UNSUPPORTED_ENCODING), e, logMessage.getMessage(UNSUPPORTED_ENCODING_SOLUTION, CHARSET)); } }  }
CryptoServiceImpl implements CryptoService { @Override public String decrypt(String encryptedText, String key) throws CryptoException { checkParameters("encryptedText", encryptedText); checkParameters("key", key); try { ByteBuffer buffer = ByteBuffer.wrap(Base64.decodeBase64(encryptedText)); int minLength = SALT_SIZE + 1; if (buffer.capacity() < minLength) { throw new CryptoException(logMessage.getMessage(INVALID_ENCRYPTED_TXT, encryptedText), logMessage.getMessage(INVALID_ENCRYPTED_TXT_SOLUTION, String.valueOf(buffer.capacity()), String.valueOf(minLength))); } byte[] saltBytes = new byte[SALT_SIZE]; buffer.get(saltBytes, 0, saltBytes.length); byte[] encryptedTextBytes = new byte[buffer.capacity() - saltBytes.length]; buffer.get(encryptedTextBytes); byte[] pbKey = PBKDF.PBKDF2_SHA256(key.getBytes(CHARSET), saltBytes, NUMBER_OF_ITERATIONS); byte[] decryptedTextBytes = clientCryptoHandler.decryptMsg(pbKey, encryptedTextBytes); if (decryptedTextBytes != null) { return new String(decryptedTextBytes, CHARSET); } return null; } catch (SymphonyEncryptionException | InvalidDataException e) { throw new CryptoException(logMessage.getMessage(GENERAL_CRYPTO_ERROR), e, logMessage.getMessage(GENERAL_CRYPTO_ERROR_SOLUTION, e.getMessage())); } catch (SymphonyInputException e) { throw new CryptoException(logMessage.getMessage(INVALID_INPUT_TXT), e, logMessage.getMessage(INVALID_INPUT_TXT_SOLUTION, e.getMessage())); } catch (CiphertextTransportVersionException | CiphertextTransportIsEmptyException e) { throw new CryptoException(logMessage.getMessage(INVALID_CIPHER_TXT), e, logMessage.getMessage(INVALID_CIPHER_TXT_SOLUTION, e.getMessage())); } catch (UnsupportedEncodingException e) { throw new CryptoException(logMessage.getMessage(UNSUPPORTED_ENCODING), e, logMessage.getMessage(UNSUPPORTED_ENCODING_SOLUTION, CHARSET)); } }  @Override String encrypt(String plainText, String key); @Override String decrypt(String encryptedText, String key); }
CryptoServiceImpl implements CryptoService { @Override public String decrypt(String encryptedText, String key) throws CryptoException { checkParameters("encryptedText", encryptedText); checkParameters("key", key); try { ByteBuffer buffer = ByteBuffer.wrap(Base64.decodeBase64(encryptedText)); int minLength = SALT_SIZE + 1; if (buffer.capacity() < minLength) { throw new CryptoException(logMessage.getMessage(INVALID_ENCRYPTED_TXT, encryptedText), logMessage.getMessage(INVALID_ENCRYPTED_TXT_SOLUTION, String.valueOf(buffer.capacity()), String.valueOf(minLength))); } byte[] saltBytes = new byte[SALT_SIZE]; buffer.get(saltBytes, 0, saltBytes.length); byte[] encryptedTextBytes = new byte[buffer.capacity() - saltBytes.length]; buffer.get(encryptedTextBytes); byte[] pbKey = PBKDF.PBKDF2_SHA256(key.getBytes(CHARSET), saltBytes, NUMBER_OF_ITERATIONS); byte[] decryptedTextBytes = clientCryptoHandler.decryptMsg(pbKey, encryptedTextBytes); if (decryptedTextBytes != null) { return new String(decryptedTextBytes, CHARSET); } return null; } catch (SymphonyEncryptionException | InvalidDataException e) { throw new CryptoException(logMessage.getMessage(GENERAL_CRYPTO_ERROR), e, logMessage.getMessage(GENERAL_CRYPTO_ERROR_SOLUTION, e.getMessage())); } catch (SymphonyInputException e) { throw new CryptoException(logMessage.getMessage(INVALID_INPUT_TXT), e, logMessage.getMessage(INVALID_INPUT_TXT_SOLUTION, e.getMessage())); } catch (CiphertextTransportVersionException | CiphertextTransportIsEmptyException e) { throw new CryptoException(logMessage.getMessage(INVALID_CIPHER_TXT), e, logMessage.getMessage(INVALID_CIPHER_TXT_SOLUTION, e.getMessage())); } catch (UnsupportedEncodingException e) { throw new CryptoException(logMessage.getMessage(UNSUPPORTED_ENCODING), e, logMessage.getMessage(UNSUPPORTED_ENCODING_SOLUTION, CHARSET)); } }  @Override String encrypt(String plainText, String key); @Override String decrypt(String encryptedText, String key);  }