【问题标题】:How to encode and decode using Javascript (Given string length should match Encode string length)如何使用 Javascript 进行编码和解码(给定的字符串长度应与编码字符串长度匹配)
【发布时间】:2014-04-27 09:56:27
【问题描述】:

我有以下情况:我需要编码我的字符串,然后我必须解码我的值。

编码后的字符串应与字符串本身具有相同的长度。我的输入字符串只有 = Alpha & Number 值。

例如:

String="Test12345"  (lenght : 9) 

Encode =    (encode should lenght 9) 

Decode = Test12345

我尝试了不同的 CharcodeAt , FromCharCode 函数,但我得到了更长的编码字符串。我找不到解决方案。

查看我编写代码的输出值:

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

代码:

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;

};

谁能帮我解决这个问题。

【问题讨论】:

  • 我认为您应该使用fromCharCode() 而不是toString()。您正在将每个编码字符转换为其字符代码的 base-36 值。
  • 嗨 Barmar,我尝试转换我的编码/解码,但我得到了更多长度的值。我需要精确的值长度。
  • 我需要将我的字符串值从一个系统传递到另一个系统,出于安全原因,我需要将我的输入字符串转换为“编码值”。我的输入字符串值总是与 Alphabetic(A to Z) & Numbers(0,1,2,3,4,5,6,7,8,9,10) 结合 输入字符串中没有特殊字符 输入字符串长度& 编码字符串长度值只能是最大 (96) 个字符。我可以对我的输入字符串进行编码,但编码值超出了输入字符串的长度。所以我无法实现这种情况。任何人都对简化这种情况有任何想法。 ?
  • 如果您需要一个与解码后的字符串长度相同的编码字符串,您可能需要使用类似ROT13 的字符串,其中一个字符与另一个字符一一匹配。
  • 如果您希望编码后的字符串与输入的长度相同,则必须将每个字符转换为一个字符。您将每个字符转换为两个字符,因为您使用的是基数 36。

标签: javascript string decode encode


【解决方案1】:

在一个非常基本的情况下,您可以对每个字符进行异或运算 - 这是一个可逆操作,因此再次调用函数将对字符串进行解码。例如

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

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

    return nstr;
};

编码:

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

解码:

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

演示:http://jsfiddle.net/SLLzb/

您可以修改此函数(例如,如果您使用 ^ i 而不是 ^ 1 - 输入中的相同字符将在输出中编码为不同的字符)

但正如 Barmar 所指出的 - 这是非常基本的,如果安全性真的很重要 - 你应该研究真正的加密方法。

【讨论】:

【解决方案2】:
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();
    }
}

}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-09-14
    • 2018-01-16
    相关资源
    最近更新 更多