/** * Encodes a String into a base 64 String. The resulting encoding is chunked at 76 bytes. * <p> * * @param s String to encode. * * @return encoded string. */ public static String encode( String s ) { byte[] sBytes = s.getBytes(); sBytes = encode( sBytes ); s = new String( sBytes ); return s; }
public static void main( String[] args ) throws Exception { String s = "\nPGRpdiB4bWxucz0iaHR0cDovL3d3dy53My5vcmcvMTk5OS94aHRtbCI+V2UncmUgcHJvcG9zaW5nIDxhIGhy\n" + "ZWY9Imh0dHA6Ly93d3cuZ29vZ2xlLmNvbS9jb3Jwb3JhdGUvc29mdHdhcmVfcHJpbmNpcGxlcy5odG1sIj5z\n" + "b21lIGd1aWRlbGluZXMgPC9hPnRvIGhlbHAgY3VyYiB0aGUgcHJvYmxlbSBvZiBJbnRlcm5ldCBzb2Z0d2Fy\n" + "ZSB0aGF0IGluc3RhbGxzIGl0c2VsZiB3aXRob3V0IHRlbGxpbmcgeW91LCBvciBiZWhhdmVzIGJhZGx5IG9u\n" + "Y2UgaXQgZ2V0cyBvbiB5b3VyIGNvbXB1dGVyLiBXZSd2ZSBiZWVuIGhlYXJpbmcgYSBsb3Qgb2YgY29tcGxh\n" + "aW50cyBhYm91dCB0aGlzIGxhdGVseSBhbmQgaXQgc2VlbXMgdG8gYmUgZ2V0dGluZyB3b3JzZS4gV2UgdGhp\n" + "bmsgaXQncyBpbXBvcnRhbnQgdGhhdCB5b3UgcmV0YWluIGNvbnRyb2wgb2YgeW91ciBjb21wdXRlciBhbmQg\n" + "dGhhdCB0aGVyZSBiZSBzb21lIGNsZWFyIHN0YW5kYXJkcyBpbiBvdXIgaW5kdXN0cnkuIExldCB1cyBrbm93\n" + "IGlmIHlvdSB0aGluayB0aGVzZSBndWlkZWxpbmVzIGFyZSB1c2VmdWwgb3IgaWYgeW91IGhhdmUgc3VnZ2Vz\n" + "dGlvbnMgdG8gaW1wcm92ZSB0aGVtLgo8YnIgLz4KPGJyIC8+Sm9uYXRoYW4gUm9zZW5iZXJnCjxiciAvPgo8\n" + "L2Rpdj4K\n"; System.out.println( decode( s ) ); } }
/** * Encrypts and returns the encrypted string for the given message. * * @param message the pass phrase that is to be encrypted. * @return the encrypted string for the given message. */ static String encrypt(String message) { java.security.MessageDigest d =null; try { d = java.security.MessageDigest.getInstance("SHA-1"); } catch (NoSuchAlgorithmException e) { return null; } d.reset(); d.update(message.getBytes()); return new String(Base64Encoder.encode(d.digest())); }
/** * Decodes a base 64 String into a String. * <p> * * @param s String to decode. * * @return encoded string. * * @throws java.lang.IllegalArgumentException * thrown if the given byte array was not valid com.sun.syndication.io.impl.Base64 encoding. */ public static String decode( String s ) throws IllegalArgumentException { s = s.replaceAll( "\n", "" ); s = s.replaceAll( "\r", "" ); byte[] sBytes = s.getBytes(); sBytes = decode( sBytes ); s = new String( sBytes ); return s; }
/** * Prepares an url connection for authentication if necessary. * * @param connection the connection to be prepared * * @return the prepared conection */ public static URLConnection prepareForAuthentication( final URLConnection connection ) { NullArgumentException.validateNotNull( connection, "url connection cannot be null" ); if( connection.getURL().getUserInfo() != null ) { // Need to decode username/password because it may contain encoded characters (http://www.w3schools.com/tags/ref_urlencode.asp) // A common encoding is to provide a username as an email address like user%40domain.org String decodedUserInfo = decode( connection.getURL().getUserInfo() ); String base64Encoded = Base64Encoder.encode( decodedUserInfo ); // sun bug 6459815: Long passwords cause Basic Auth to fail with a java.net.Authenticator base64Encoded = base64Encoded.replaceAll( "\n", "" ); connection.setRequestProperty( "Authorization", "Basic " + base64Encoded ); } return connection; }