Skip to main content

Encode from JavaScript and Decode at Java.


if you have a senerio like you want to send encoded parameter values from your web page 
and decode them  at server side then this blog may be help you.

JavaScript:
Before submit the form I am encoding all the value entered by user so that it can not be read by humanly, for encoding the values I have written a method named encodeBase64

<script>

function encodeBase64(input){
        var keyStr = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789";

        input = escape(input);
        var output = "";
        var chr1, chr2, chr3 = "";
        var enc1, enc2, enc3, enc4 = "";
        var i = 0;
    
        do {
            chr1 = input.charCodeAt(i++);
            chr2 = input.charCodeAt(i++);
            chr3 = input.charCodeAt(i++);
        
            enc1 = chr1 >> 2;
            enc2 = ((chr1 & 3) << 4) | (chr2 >> 4);
            enc3 = ((chr2 & 15) << 2) | (chr3 >> 6);
            enc4 = chr3 & 63;
        
            if (isNaN(chr2)) {
                enc3 = enc4 = 64;
            } else if (isNaN(chr3)) {
                enc4 = 64;
            }
        
            output = output + keyStr.charAt(enc1) + keyStr.charAt(enc2) + keyStr.charAt(enc3) + keyStr.charAt(enc4);
            chr1 = chr2 = chr3 = "";
            enc1 = enc2 = enc3 = enc4 = "";
        } while (i < input.length);
    
        return output;

 };

 function submit(){

             var userName = document.getElementsByName("userName")[0];
             userName.value = encodeBase64(userName.value);
             
            var password = document.getElementsByName("password")[0];
            password.value = encodeBase64(password.value);
            
            document.getElementById("loginForm").submit();
 }

</script>


HTML:
Simple form in which I am calling submit method on button click which encode the user input value and submit the form.

<form action="/login" id="loginForm" method="POST">
             User Name : <input type="text" name="userName" />
             Password : <input type="password" name="password" />
            <input type="button" onclick="submit()" />
 </form>


JAVA:
There is one method I have written named decodeBase64 which convert the encoded value sent from the client  to the original value that user has input.

NOTE: Make sure the keyStr used in javascript and Java code must be same.

import java.io.*;
import java.net.URLDecoder;
import javax.servlet.*;
import javax.servlet.http.*;

public class login extends HttpServlet {

public static String keyStr = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789";

 public void doPost(HttpServletRequest request,HttpServletResponse response)
           throws ServletException, IOException{
         PrintWriter out = response.getWriter();
                  out.println("User Name = "+decodeBase64(request.getParameter("userName")));
                  out.println("Password = "+decodeBase64(request.getParameter("password")));

 }


  public String decodeBase64(String input) {

           String output = "";
           if (input != null) {
               int chr1, chr2, chr3;
               int enc1 = 0, enc2 = 0, enc3 = 0, enc4 = 0;
               int i = 0;

               while (i <= input.length()) {
                   try {
                       enc1 = keyStr.indexOf(input.charAt(i++));
                       enc2 = keyStr.indexOf(input.charAt(i++));
                       chr1 = (enc1 << 2) | (enc2 >> 4);
                       output = output + String.valueOf((char) chr1);

                       enc3 = keyStr.indexOf(input.charAt(i++));
                       chr2 = ((enc2 & 15) << 4) | (enc3 >> 2);
                       if (enc3 != 64) {
                           output = output + String.valueOf((char) chr2);
                       }

                       enc4 = keyStr.indexOf(input.charAt(i++));
                       chr3 = ((enc3 & 3) << 6) | enc4;
                       if (enc4 != 64) {
                           output = output + String.valueOf((char) chr3);
                       }
                   } catch (Exception e) {
                   }

                   chr1 = chr2 = chr3 = 0;
                   enc1 = enc2 = enc3 = enc4 = 0;
               }
           }
           try {
               return URLDecoder.decode(output, "utf-8");
           } catch (UnsupportedEncodingException ex) {
           }
     return "";
 }
}

Comments