Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Similar Java function like atob in javascript

I have Query parameter that was encoded by btoa javascript function. when url clicked, request function called inside java controller where i wanted to decode Query parameter (which is encoded from javascript btoa). i have tried BASE64Decoder and Base64.getDecoder() but not able to get proper value. is there any other way to do so?

Java Controller

@RequestMapping(value = "decode/{email}", method = RequestMethod.GET)
    public String decodeEmail(Model model, @PathVariable String email){
        Decode decode = new Decode();
        decode.setEmail(email);
        decodeService.save(decode);
        return "decode/List";
    }

JavaScript

var email = document.getElementById("email").value;
var encodedEmail = btoa(email);

Example

String to encode : [email protected]

Encoded String : ZGVtb0BkZW1vLmNvbQ==

like image 902
Krupesh Kotecha Avatar asked Jun 07 '16 08:06

Krupesh Kotecha


1 Answers

Java 8 has a new Base64 package:

public void test() {
    String s = "[email protected]";
    String encoded = new String(Base64.getEncoder().encode(s.getBytes()));
    String decoded = new String(Base64.getDecoder().decode(encoded));
    System.out.println("S: " + s + " -> " + encoded + " -> " + decoded);
}

prints

S: [email protected] -> ZGVtb0BkZW1vLmNvbQ== -> [email protected]

There are also other encoder/decoder pairs - you may find the mime encoder appropriate to your needs.

like image 121
OldCurmudgeon Avatar answered Oct 17 '22 13:10

OldCurmudgeon