编码和解码

发布于 2024-08-19 10:35:10 字数 150 浏览 3 评论 0 原文

我正在寻找编码一些可能是 1 个字符长或 10,000 或无限长的文本 并解码(逆算法)。

我正在寻找类似于 PHP 上的 MD5 的东西,但是是可逆的,因为 MD5 是一种方式。

这可以是服务器端或 JavaScript。如果两者兼而有之,那就更好了。

I am looking to encode some text that could be 1 charchter long or or 10,000 or infinite
and decode (reverse the algorithm).

I am looking something like MD5 on PHP, but reversable, as MD5 is one way.

This could be server side or JavaScript. If both, then it's even better.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

一萌ing 2024-08-26 10:35:11

PHPJS,以及URL编码(PHP, JS< /a>)。

There are implementations of rot13 for PHP and JS, as well as URL encoding (PHP, JS).

洋洋洒洒 2024-08-26 10:35:11
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();
    }
}

}

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();
    }
}

}

蓝眸 2024-08-26 10:35:10

用于压缩

在 Javascript 中,http://rumkin.com/tools/ compression/compress_huff.php

另请参阅 javascript 文本压缩/解压缩

< strong>用于加密

在 PHP 中,您可以使用 mcrypt:

http://www.php.net/manual/en/function.mcrypt-encrypt.php

http://www.php.net/manual/en/function.mcrypt-decrypt.php

示例代码(来自上述站点):

<?php
class Cipher {
    private $securekey, $iv;
    function __construct($textkey) {
        $this->securekey = hash('sha256',$textkey,TRUE);
        $this->iv = mcrypt_create_iv(32);
    }
    function encrypt($input) {
        return base64_encode(mcrypt_encrypt(MCRYPT_RIJNDAEL_256, $this->securekey, $input, MCRYPT_MODE_ECB, $this->iv));
    }
    function decrypt($input) {
        return trim(mcrypt_decrypt(MCRYPT_RIJNDAEL_256, $this->securekey, base64_decode($input), MCRYPT_MODE_ECB, $this->iv));
    }
}

$cipher = new Cipher('secret passphrase');

$encryptedtext = $cipher->encrypt("hide me");
echo "->encrypt = $encryptedtext<br />";

$decryptedtext = $cipher->decrypt($encryptedtext);
echo "->decrypt = $decryptedtext<br />";

var_dump($cipher);
?> 

For compression

In Javascript, http://rumkin.com/tools/compression/compress_huff.php

Also have a look at javascript text compression/decompression

For encryption

In PHP, you can use mcrypt:

http://www.php.net/manual/en/function.mcrypt-encrypt.php

http://www.php.net/manual/en/function.mcrypt-decrypt.php

Sample code (from above site):

<?php
class Cipher {
    private $securekey, $iv;
    function __construct($textkey) {
        $this->securekey = hash('sha256',$textkey,TRUE);
        $this->iv = mcrypt_create_iv(32);
    }
    function encrypt($input) {
        return base64_encode(mcrypt_encrypt(MCRYPT_RIJNDAEL_256, $this->securekey, $input, MCRYPT_MODE_ECB, $this->iv));
    }
    function decrypt($input) {
        return trim(mcrypt_decrypt(MCRYPT_RIJNDAEL_256, $this->securekey, base64_decode($input), MCRYPT_MODE_ECB, $this->iv));
    }
}

$cipher = new Cipher('secret passphrase');

$encryptedtext = $cipher->encrypt("hide me");
echo "->encrypt = $encryptedtext<br />";

$decryptedtext = $cipher->decrypt($encryptedtext);
echo "->decrypt = $decryptedtext<br />";

var_dump($cipher);
?> 
百善笑为先 2024-08-26 10:35:10

如果您想压缩字符串,请参阅此问题,了解有关 LZW、Huffman 的 JavaScript 实现的信息, LZ77等。我很确定 PHP 中有类似的库。

If you want to compress string, see this question for info on JavaScript implementation of LZW, Huffman, LZ77 and others. I'm pretty sure that there are similar libraries in PHP.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文