protected PasswordAuthentication getPasswordAuthentication() { String decrypted; try { Base64PasswordService ps = new Base64PasswordService(); String pass = service.getEmailConfig().getPassword(); if ( pass.startsWith( "ENC:" ) ) { decrypted = ps.decrypt( service.getEmailConfig().getPassword().substring( 4, pass.length() ) ); } else { decrypted = ps.decrypt( service.getEmailConfig().getPassword() ); } } catch ( Exception e ) { decrypted = service.getEmailConfig().getPassword(); } return new PasswordAuthentication( service.getEmailConfig().getUserId(), decrypted ); } } );
@Override public String decrypt( String encryptedPassword ) throws PasswordServiceException { if ( StringUtil.isEmpty( encryptedPassword ) ) { return encryptedPassword; } else { if ( encryptedPassword.startsWith( prefix ) ) { // If it starts with "Encrypted " then decode using KettleTwoWayPasswordEncoder return encoder.decode( encryptedPassword ); } else { // If not, likely Base64 encoded so twy that return super.decrypt( encryptedPassword ); } } }
@Before public void setUp( ) { service = new Base64PasswordService(); }
@Test public void testEncryptWhiteSpacePassword( ) throws Exception{ res = service.encrypt( " " ); assertEquals( res, "ICAg" ); }
@Test public void testDecryptValidPassword( ) throws Exception { res = service.decrypt( "aGVsbG93b3JsZDEyMzEyIyMkJEAoKCko" ); assertEquals( res, "helloworld12312##$$@(()(" ); } }
@Test public void testEncryptNullPassword( ) throws Exception { res = service.encrypt( null ); assertNull( res ); }
@Test public void testDecryptEmptyPassword( ) throws Exception { res = service.decrypt( "" ); assertEquals( res, "" ); }
@Test public void testEncryptEmptyPassword( ) throws Exception{ res = service.encrypt( "" ); assertEquals( res, "" ); }
@Test public void testDecryptWhiteSpacePassword( ) throws Exception { res = service.decrypt( "ICAg" ); assertEquals( res, " " ); }
@Test public void testDecryptNullPassword( ) throws Exception { res = service.decrypt( null ); assertNull( res ); }