2

I have the following scenario: I need to encode my string then i have to decode my value.

The encoded string should have the same length as the string itself. My input string has only = Alpha & Number values.

For example :

String="Test12345"  (lenght : 9) 

Encode =    (encode should lenght 9) 

Decode = Test12345

I tried different CharcodeAt , FromCharCode function but I'm getting longer Encoded strings. I could not find the solution.

See the output values which i write code:

1. Your Input String: 
Test12345
2. Encoded String: 
4R4A3W3V5Q5P5O5N5M
3. Result : Decoded/Actual Input String: 
Test12345

code :

String.prototype.toEncodedString = function()
{
    var ostr=this.toString().replace(/\s+/g,'');
    var x,nstr='',len=ostr.length;
    for(x=0;x<len;++x)
    {
        nstr+=(255-ostr.charCodeAt(x)).toString(36).toUpperCase();  
    };
    return nstr;
};

String.prototype.fromEncodedString = function() {

var ostr=this.toString();    
var x,nstr='',len=ostr.length;

for(x=0;x<len;x+=2) 

{

        nstr+=String.fromCharCode(255-parseInt(ostr.substr(x,2),36));

};
return nstr;

};

Could anyone help me on this.

8
  • I think you should be using fromCharCode() instead of toString(). You're converting each encoded character to the base-36 value of its char code. Commented Mar 20, 2014 at 13:55
  • hi Barmar , I tried convert my encode /Decode but I am getting more lenght of values. I need exact length of value. Commented Mar 20, 2014 at 13:56
  • I need to pass my string value from one system to another system, for security reason I need to convert my input string as “Encoded value”. My input string value always combine with Alphabetic(A to Z) & Numbers(0,1,2,3,4,5,6,7,8,9,10) There are no special characters in the Input string Input string length & Encode string length values should be maximum (96) characters only. I can able to encode my input string, but the encode value beyond the input string length. So I could not able to achieve this scenario. Anyone has any idea for simplified this scenario. ? Commented Mar 20, 2014 at 13:58
  • If you need an encoded string that has the same length as its decoded counterpart, you might want to use something like ROT13 where one character matches another one to one. Commented Mar 20, 2014 at 14:02
  • If you want the encoded string to be the same length as the input, you have to convert each character to one character. You're converting each character to two characters because you're using base 36. Commented Mar 20, 2014 at 14:02

2 Answers 2

2

At a very basic you can XOR each character - it's a reversible operation, so calling function again will decode the string. E.g.

String.prototype.encodeDecode = function()
{
   var nstr=''

   for (var i=0; i <  this.length; i++) {
       nstr += String.fromCharCode(this.charCodeAt(i) ^ 1);
   }

    return nstr;
};

Encoding:

"Test12345".encodeDecode() // will produce "Udru03254"

Decoding:

"Udru03254".encodeDecode() // will produce "Test12345"

Demo: http://jsfiddle.net/SLLzb/

You can modify this function (e.g. if instead of ^ 1 you use ^ i - identical characters in the input will be encoded as different characters in in the output)

But as Barmar pointed out - this is very basic, and if security is really paramount - you should look into real ciphering methods.

Sign up to request clarification or add additional context in comments.

2 Comments

fromCharCode(this.charCodeAt(i) ^ 1) -> There is sympol before 1 characters. what does it mean? i couldnt understand.
0
import java.io.*;
class ed2
{

BufferedReader obj = new BufferedReader(new InputStreamReader(System.in));

void encode()throws InterruptedException,IOException
{
    String rs,ren;

    encoder_symbol();
    rs = encode_input();
    ren = encode_find(rs);
    encode_display(ren);
}

void decode()throws InterruptedException,IOException
{
    String rs,rde;

    decoder_symbol();
    rs = decode_input();
    rde = decode_find(rs);
    decode_display(rde);
}

void encoder_symbol()throws InterruptedException //just for fun
{
    System.out.println("********  ***         ***  *********  ************  ******      ********  *****");
    Thread.sleep(100);
    System.out.println("********  ****        ***  *********  ************  ********    ********  *** **");
    Thread.sleep(100);
    System.out.println("***       *****       ***  ***        ***      ***  ***   ***   ***       ***  **");
    Thread.sleep(100);
    System.out.println("***       *** **      ***  ***        ***      ***  ***    ***  ***       *** **");
    Thread.sleep(100);
    System.out.println("******    ***  **     ***  ***        ***      ***  ***    ***  ******    *****");
    Thread.sleep(100);
    System.out.println("******    ***   **    ***  ***        ***      ***  ***    ***  ******    *****");
    Thread.sleep(100);
    System.out.println("***       ***     **  ***  ***        ***      ***  ***    ***  ***       *** **");
    Thread.sleep(100);
    System.out.println("***       ***      ** ***  ***        ***      ***  ***   ***   ***       ***  **");
    Thread.sleep(100);
    System.out.println("*******   ***       *****  *********  ************  ********    ********  ***   **");
    Thread.sleep(100);
    System.out.println("*******   ***        ****  *********  ************  ******      ********  ***    **");
    Thread.sleep(2700);

    System.out.println();
    System.out.println();
}

void decoder_symbol()throws InterruptedException // just for fun
{
    System.out.println("******      ********  *********  ************  ******      ********  *****");
    Thread.sleep(100);
    System.out.println("********    ********  *********  ************  ********    ********  *** **");
    Thread.sleep(100);
    System.out.println("***   ***   ***       ***        ***      ***  ***   ***   ***       ***  **");
    Thread.sleep(100);
    System.out.println("***    ***  ***       ***        ***      ***  ***    ***  ***       *** **");
    Thread.sleep(100);
    System.out.println("***    ***  ******    ***        ***      ***  ***    ***  ******    *****");
    Thread.sleep(100);
    System.out.println("***    ***  ******    ***        ***      ***  ***    ***  ******    *****");
    Thread.sleep(100);
    System.out.println("***    ***  ***       ***        ***      ***  ***    ***  ***       *** **");
    Thread.sleep(100);
    System.out.println("***   ***   ***       ***        ***      ***  ***   ***   ***       ***  **");
    Thread.sleep(100);
    System.out.println("********    ********  *********  ************  ********    ********  ***   **");
    Thread.sleep(100);
    System.out.println("******      ********  *********  ************  ******      ********  ***    **");
    Thread.sleep(1000);

    System.out.println();
    System.out.println();
}

String encode_input()throws IOException
{
    String s;
    System.out.println("ENTER THE STRING TO BE ENCODED");
    s = obj.readLine();
    return(s);
}

String decode_input()throws IOException
{
    String s;
    System.out.println("ENTER THE CODE TO BE DECODED");
    s = obj.readLine();
    return(s);
}

String encode_find(String s)//converting the string into its binary equivalent
{
     int ac,i,j,l,chklen;
     String bc,en="";
     char ic;
     l = s.length();    
     for(i=0;i<l;i++)
     {   
         ic = s.charAt(i); //takes out every character
         bc = ""; 
         ac = (int)ic;  //ASCII value of this character
         while(ac!=0)
         {
             bc = Integer.toString((ac%2)) + bc; //converting the ASCII value into binary equivalent
             ac = ac/2;
         }
         chklen = bc.length();//length of the binary equivalent
         if(chklen<7)
         {
            for(j=1;j<=(7-chklen);j++) //increasing the length of binary equivalent so that it becomes equal to 7
            {  
                bc = "0" + bc;
            }
         }
         en = en+bc; //concatenating all the binary equivalent into one string
     }
     return (en);
}

String decode_find(String s)// converts binary(i.e. in the form of dots and space) to decimal
{
    int f;//for the index of every character of code
    long l,i,j,ac;
    char c;
    String de="";

    l = s.length();
    f = 0;//index of first caharcter
    for(i=0;i<(l/7);i++)//since the length of every binary equivalent of a character is 7 therefore there will be (length/7) characters in a code of length l
    {
        ac = 0;//intializes the decimal(ASCII) equivalent to zero
        for(j=6;j>=0;j--)//loop will work 7 times for every binary equivalent of a character
        {
            c = s.charAt(f);//takes out every dot or space
            if(c=='.')//it means that c corresponds to 'one'
            {
                ac = ac + ((int)Math.pow(2,j));//converting binary into decimal(ASCII) equivalent by adding all the powers of 2 which correspond to one('.') 
            }
            f++;//increasing the index for next character of binary equivalent
        }
        de = de + ((char)ac);//converts the ASCII equivalent into character and then concatenates it with the intitial string
    }
    return(de);
}

void encode_display(String en)//displays the code
{
    int i,l;
    char ic;
    System.out.println("YOUR ENCODED MESSAGE IS :");
    l=en.length();
    for(i=0;i<l;i++)
    {
        ic=en.charAt(i);
        if(ic=='1')//for every 'one' it will print '.'(dot)
        {
             System.out.print(".");
        }
        else if(ic=='0')//for every 'zero' it will print ' '(space)
        {
             System.out.print(" ");
        }
    }
}

void decode_display(String de)
{
    System.out.println(de);
}

public static void main(String args[])throws IOException,InterruptedException
{
    BufferedReader obj = new BufferedReader(new InputStreamReader(System.in));

    char ch;

    ed2 ed = new ed2();

    System.out.println("PRESS 'E' TO ENCODE A MESSAGE OR PRESS 'D' TO DECODE A MESSAGE");
    ch = (char)obj.read();

    if((ch=='e')||(ch=='E'))
    {
        ed.encode();
    }
    else if((ch=='d')||(ch=='D'))
    {
        ed.decode();
    }
}

}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.