@Test(expected = CryptoException.class) public void testEncryptInvalidKey() throws CryptoException { cryptoService.encrypt(TEXT, null); }
@Override public String encrypt(String plainText, String key) throws CryptoException { checkParameters("plainText", plainText); checkParameters("key", key); try { byte saltBytes[] = new byte[SALT_SIZE]; Bytes.random(saltBytes); byte[] pbKey = PBKDF.PBKDF2_SHA256(key.getBytes(CHARSET), saltBytes, NUMBER_OF_ITERATIONS); KeyIdentifier keyId = new KeyIdentifier(new byte[STREAM_ID_SIZE], 0L, 0L); byte[] encryptedTextBytes = clientCryptoHandler.encryptMsg(pbKey, keyId, plainText.getBytes(CHARSET)); byte[] buffer = new byte[saltBytes.length + encryptedTextBytes.length]; System.arraycopy(saltBytes, 0, buffer, 0, saltBytes.length); System.arraycopy(encryptedTextBytes, 0, buffer, saltBytes.length, encryptedTextBytes.length); return Base64.encodeBase64String(buffer); } catch (SymphonyEncryptionException 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 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 encrypt(String plainText, String key) throws CryptoException { checkParameters("plainText", plainText); checkParameters("key", key); try { byte saltBytes[] = new byte[SALT_SIZE]; Bytes.random(saltBytes); byte[] pbKey = PBKDF.PBKDF2_SHA256(key.getBytes(CHARSET), saltBytes, NUMBER_OF_ITERATIONS); KeyIdentifier keyId = new KeyIdentifier(new byte[STREAM_ID_SIZE], 0L, 0L); byte[] encryptedTextBytes = clientCryptoHandler.encryptMsg(pbKey, keyId, plainText.getBytes(CHARSET)); byte[] buffer = new byte[saltBytes.length + encryptedTextBytes.length]; System.arraycopy(saltBytes, 0, buffer, 0, saltBytes.length); System.arraycopy(encryptedTextBytes, 0, buffer, saltBytes.length, encryptedTextBytes.length); return Base64.encodeBase64String(buffer); } catch (SymphonyEncryptionException 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 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 encrypt(String plainText, String key) throws CryptoException { checkParameters("plainText", plainText); checkParameters("key", key); try { byte saltBytes[] = new byte[SALT_SIZE]; Bytes.random(saltBytes); byte[] pbKey = PBKDF.PBKDF2_SHA256(key.getBytes(CHARSET), saltBytes, NUMBER_OF_ITERATIONS); KeyIdentifier keyId = new KeyIdentifier(new byte[STREAM_ID_SIZE], 0L, 0L); byte[] encryptedTextBytes = clientCryptoHandler.encryptMsg(pbKey, keyId, plainText.getBytes(CHARSET)); byte[] buffer = new byte[saltBytes.length + encryptedTextBytes.length]; System.arraycopy(saltBytes, 0, buffer, 0, saltBytes.length); System.arraycopy(encryptedTextBytes, 0, buffer, saltBytes.length, encryptedTextBytes.length); return Base64.encodeBase64String(buffer); } catch (SymphonyEncryptionException 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 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 encrypt(String plainText, String key) throws CryptoException { checkParameters("plainText", plainText); checkParameters("key", key); try { byte saltBytes[] = new byte[SALT_SIZE]; Bytes.random(saltBytes); byte[] pbKey = PBKDF.PBKDF2_SHA256(key.getBytes(CHARSET), saltBytes, NUMBER_OF_ITERATIONS); KeyIdentifier keyId = new KeyIdentifier(new byte[STREAM_ID_SIZE], 0L, 0L); byte[] encryptedTextBytes = clientCryptoHandler.encryptMsg(pbKey, keyId, plainText.getBytes(CHARSET)); byte[] buffer = new byte[saltBytes.length + encryptedTextBytes.length]; System.arraycopy(saltBytes, 0, buffer, 0, saltBytes.length); System.arraycopy(encryptedTextBytes, 0, buffer, saltBytes.length, encryptedTextBytes.length); return Base64.encodeBase64String(buffer); } catch (SymphonyEncryptionException 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 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 encrypt(String plainText, String key) throws CryptoException { checkParameters("plainText", plainText); checkParameters("key", key); try { byte saltBytes[] = new byte[SALT_SIZE]; Bytes.random(saltBytes); byte[] pbKey = PBKDF.PBKDF2_SHA256(key.getBytes(CHARSET), saltBytes, NUMBER_OF_ITERATIONS); KeyIdentifier keyId = new KeyIdentifier(new byte[STREAM_ID_SIZE], 0L, 0L); byte[] encryptedTextBytes = clientCryptoHandler.encryptMsg(pbKey, keyId, plainText.getBytes(CHARSET)); byte[] buffer = new byte[saltBytes.length + encryptedTextBytes.length]; System.arraycopy(saltBytes, 0, buffer, 0, saltBytes.length); System.arraycopy(encryptedTextBytes, 0, buffer, saltBytes.length, encryptedTextBytes.length); return Base64.encodeBase64String(buffer); } catch (SymphonyEncryptionException 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 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);  }