简体   繁体   中英

encoding a message using apache commons

I am trying to do a base 64 encoding of some message and then URL encode the message and pass the whole encoded content as a parameter/value to a querystring. http://www.xxxxxx.com/xxxxx?query=base64urlencodedmessage

So i want to use base64 encoding and then URL encoding. I see apache provides good library for it.

So, in the below apache methods:

1) how to see which method i want to use 1), 2) and 3) 2) In method 1 below, encoding with a specific char set means encoding with only selected char set? what is the difference between 1 and 3 ? Which is more secure??? 2) I am using base64 first, so does it mean that i will be getting only byte array as output and so I should use only method 2?

URLCodec url=new URLCodec();

1) url.encode(str, charset);

2) url.encode(bytes);

3) url.encode(str);

it should be clear for this test

    String s1 = "test";
    System.out.println(s1);
    Base64 base64 = new Base64();
    String s2 = base64.encodeAsString(s1.getBytes());
    System.out.println(s2);
    URLCodec url = new URLCodec();
    String s3 = url.encode(s2);
    System.out.println(s3);
    s2 = url.decode(s3);
    System.out.println(s2);
    s1 = new String(base64.decode(s2));
    System.out.println(s1);

output

test
dGVzdA==
dGVzdA%3D%3D
dGVzdA==
test

BTW you can do the same with standard java.net.URLEncoder / java.net.URLDecoder and javax.bind.DatatypeConverted classes

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM