A reversible string encryption algorithm

Source: Internet
Author: User
A reversible string encryption algorithm must support Chinese characters (non-hard requirements), letters, numbers, decimal points, etc. the encrypted string consists of numbers and characters, it is recommended that the length after encryption be the same as that before encryption.
For example, the encrypted "www.qq.com" string


Reply to discussion (solution)

In the same case, you can consider the replacement algorithm, which is the simplest. the replacement rules can be defined by yourself,

In the same case, you can consider the replacement algorithm, which is the simplest. the replacement rules can be defined by yourself. Is there any code?

If you do not consider Chinese characters, you only need to encrypt them with Caesar.

$ S = 'www .qq.com ~ '; $ K = 13; // encrypted for ($ I = 0; $ I
 
  
126) $ n = ($ n + 32) & 0x7f; $ s {$ I} = chr ($ n);} echo $ s, PHP_EOL; // $; ~~; P | z + // decrypt for ($ I = 0; $ I
   
   

If you do not consider Chinese characters, you only need to encrypt them with Caesar.

$ S = 'www .qq.com ~ '; $ K = 13; // encrypted for ($ I = 0; $ I
    
     
126) $ n = ($ n + 32) & 0x7f; $ s {$ I} = chr ($ n);} echo $ s, PHP_EOL; // $; ~~; P | z + // decrypt for ($ I = 0; $ I
       
      

This method is good, but does not meet my requirements, that is, the encrypted string can only have numbers, letters

If you do not consider Chinese characters, you only need to encrypt them with Caesar.

$ S = 'www .qq.com ~ '; $ K = 13; // encrypted for ($ I = 0; $ I
       
        
126) $ n = ($ n + 32) & 0x7f; $ s {$ I} = chr ($ n);} echo $ s, PHP_EOL; // $; ~~; P | z + // decrypt for ($ I = 0; $ I
          
         

For example, after the string 'abcdefghijklmnopqrstuvwxyz1234567890 _-. 'is encrypted, it can only consist of strings and numbers,-and _, and the decimal point is converted to another one.

If I'm not mistaken, your request is "the encrypted string consists of numbers and characters"
I don't know how to explain this character.

The length of a character beyond the range of letters and numbers must be represented by letters and numbers.
"Put the elephant in the refrigerator" is just a joke. do you take it seriously?

If a string contains only letters and numbers, it is impossible to meet the requirements of blue words based on the conditions of your red letter.

It must support Chinese characters (not hard required), letters and numbers, decimal points, etc. the encrypted string consists of numbers and characters. it is best to have the same length as before encryption.
For example, the encrypted "www.qq.com" string

The length of a character beyond the range of letters and numbers must be represented by letters and numbers.
"Put the elephant in the refrigerator" is just a joke. do you take it seriously?

Base64 is it possible to convert 'abcdefghijklmnopqrstuvwxyz1234567890 _-. 'to pure numbers and letters? Even if the length is a little longer, you can feel that base64 is too long.

If a string contains only letters and numbers, it is impossible to meet the requirements of blue words based on the conditions of your red letter.


It must support Chinese characters (not hard required), letters and numbers, decimal points, etc. the encrypted string consists of numbers and characters. it is best to have the same length as before encryption.
For example, the encrypted "www.qq.com" string

The length can be slightly longer.

After learning about the bit of a byte, you can see that base64 is the limit of visualized non-compressed encryption.
We can only use the compression algorithm to shorten it.


The length of a character beyond the range of letters and numbers must be represented by letters and numbers.
"Put the elephant in the refrigerator" is just a joke. do you take it seriously?

Base64 is it possible to convert 'abcdefghijklmnopqrstuvwxyz1234567890 _-. 'to pure numbers and letters? Even if the length is a little longer, you can feel that base64 is too long.

One byte can only represent 256 states
All compression algorithms store the same consecutive segments as: Count + segments.
Therefore, compression does not always reduce the size.

Base64 conversion also includes the/And =
Replace -_
However, base64 can be used to convert any data, such as Chinese characters, to 4 characters for every 3 characters, up to 1/3 characters in length. It is estimated that the landlord thinks it is too wasteful.

It should be changed to convert only the specified characters.
At least 65 valid characters must be converted to a-zA-Z0-9.
However, the output cannot contain 64 valid characters including vertices. In theory, the length is at least 1/64.

Base64 conversion also includes the/And =
Replace -_
However, base64 can be used to convert any data, such as Chinese characters, to 4 characters for every 3 characters, up to 1/3 characters in length. It is estimated that the landlord thinks it is too wasteful.

It should be changed to convert only the specified characters.
At least 65 valid characters must be converted to a-zA-Z0-9.
However, the output cannot contain 64 valid characters including vertices. In theory, the length is at least 1/64. An algorithm can also be obtained for a theoretical length of 1/64

Shift algorithm can be implemented

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.