private static String decode_password ( String encoded_string , String crypto_algorithm ) { /* * decoding process: * * -- check for empty algorithm tag * -- convert input String to byte[] using base64 decoding * -- decipher byte[] * -- convert byte[] to String using UTF8 conversion code */ StringBuilder buffer = new StringBuilder ( ) ; if ( crypto_algorithm . length ( ) == 0 ) { // crypto algorithm is empty ... don't decode password buffer . append ( encoded_string ) ; } else { // decode password with specified crypto algorithm String decoded_string = null ; if ( encoded_string . length ( ) > 0 ) { // convert viewable string to encrypted password byte[] byte [ ] encrypted_bytes = convert_viewable_to_bytes ( encoded_string ) ; logger . logp ( Level . FINEST , PasswordUtil . class . getName ( ) , "decode_password" , "byte array before decoding\n" + PasswordHashGenerator . hexDump ( encrypted_bytes ) ) ; if ( encrypted_bytes == null ) { // base64 decoding failed logger . logp ( Level . SEVERE , PasswordUtil . class . getName ( ) , "decode_password" , "PASSWORDUTIL_INVALID_BASE64_STRING" ) ; return null ; } if ( encrypted_bytes . length > 0 ) { // decrypt encrypted password byte[] with specified crypto algorithm byte [ ] decrypted_bytes = null ; try { decrypted_bytes = PasswordCipherUtil . decipher ( encrypted_bytes , crypto_algorithm ) ; } catch ( InvalidPasswordCipherException e ) { logger . logp ( Level . SEVERE , PasswordUtil . class . getName ( ) , "decode_password" , "PASSWORDUTIL_CYPHER_EXCEPTION" , e ) ; return null ; } catch ( UnsupportedCryptoAlgorithmException e ) { logger . logp ( Level . SEVERE , PasswordUtil . class . getName ( ) , "decode_password" , "PASSWORDUTIL_UNKNOWN_ALGORITHM_EXCEPTION" , e ) ; return null ; } if ( ( decrypted_bytes != null ) && ( decrypted_bytes . length > 0 ) ) { // convert decrypted password byte[] to string decoded_string = convert_to_string ( decrypted_bytes ) ; } } } if ( ( decoded_string != null ) && ( decoded_string . length ( ) > 0 ) ) { // append decoded string buffer . append ( decoded_string ) ; } } return buffer . toString ( ) ; }
Decode the provided string with the specified algorithm .