Symmetric encryption detail, and Java simple implementation

Source: Internet
Author: User
Tags decrypt md5 encryption asymmetric encryption

Original

There are 3 common types of encryption

1, positive encryption, such as MD5, encrypted ciphertext fixed, there is no way to crack, but can be able to have a certain probability of database crash to find, but now generally used in this way encryption will add salt value.

2, symmetric encryption, through a fixed symmetric key, the need to transmit data encryption and decryption, fast, but not high security, mainly used in enterprise-level internal system data transmission.

3, asymmetric encryption, n the public key, a private key, the private key stored in the server side of custody, the public key can be placed on any client, the client request to the server ciphertext only the server to get the private key can be decrypted.

Here's a concept that explains,

Random salt value (Solt):

ABCD-MD5 Encryption-e2fc714c4727ee9395f324cd2e7f331f

And then through the library, this simple character can be found very quickly.

If I define a string that only I know, put it anywhere I want to encrypt the text, for example, I add: helloword123123 before the second character of ABCD, then the character to be encrypted becomes: Ahelloword123123world.

We encrypt this again:

Ahelloword123123world, MD5 encryption, 978E1014EEFF5E0A708314DB2E7D6DA1

Again, we can decrypt it by crashing the vault:

Found this time again through the crash of the way to decrypt the failure. The salt value here is equivalent to disturbing the characters that are normally encrypted, and this rule is known only to the developer, the server.

^ (XOR) function of the operation.

Its operational rules are binary operations, the same 0, different 1, such as: 2 binary is 10,3 binary is 11, the final result is 01, turn into decimal is 1.

There is a special nature of the XOR operation, that is, inverse, take just the example, 2^3=1, regardless of 2, 3, 1 which two number do XOR, the result will always be equal to another number.

Like what:

2^3=1

2^1=3

3^1=2, it is a good choice to use it to make symmetric encryption by using XOR or this special Operation property.

Here is a simple implementation of Java:

Encrypt 1234 with the key ABC.

Ideas:

1, first convert 1234 into binary, that is, ASCII code.

Encryption:

1, 2, 3, 4 respectively corresponds to 49, 50, 51, 52

The encryption process is: 49 ^ Key ^ Salt Value-M1

The encryption process is: 50 ^ key ^ M1-m2

The encryption process is: 51 ^ key ^ m2-m3

The encryption process is: 52 ^ Key ^ m3-M4

After the above operation, ciphertext becomes M1, M2, M3, M4.

Decrypt:

Decryption is the inverse of the above encryption: Known conditions are M1, M2, M3, M4

M4, decryption process: M4 ^ M3 ^ key--52

M3, decryption process: m3^ m2 ^ key-51

M2, decryption process: M2 ^ M1 ^ key--50

M1, decryption process: M1 ^ Salt value ^ key--49

The Java implementation code is given below:

Package Com.lee;public class Encryption {public static void main (string[] args) {String content = "1234";//Required The character to encrypt String key = "abc";        Key byte[] result = encryption (content, key);        SYSTEM.OUT.PRINTLN ("1234 Value after encryption:" + new String (result));        System.out.println ("---------------");    SYSTEM.OUT.PRINTLN ("1234 decrypted value:" +new string (Decipher (new string (result), key));        } public static byte[] encryption (String content,string key) {byte[] contentbytes = Content.getbytes ();        byte[] keybytes = Key.getbytes ();        byte Dkey = 0;        for (byte b:keybytes) {Dkey ^= B;  } byte salt = 0;        Random salt value byte[] result = new Byte[contentbytes.length];            for (int i = 0; i < contentbytes.length; i++) {salt = (byte) (contentbytes[i] ^ Dkey ^ salt);        Result[i] = salt;    } return result; } public static byte[] Decipher (String content,string key) {byte[] contentbytes = Content.getbytes ();        byte[] keybytes = Key.getbytes ();        byte Dkey = 0;    for (byte b:keybytes) {Dkey ^= B;  } byte salt = 0;        Random salt value byte[] result = new Byte[contentbytes.length];            for (int i = contentbytes.length-1; I >= 0; i--) {if (i = = 0) {salt = 0;            }else{salt = contentbytes[i-1];        } Result[i] = (byte) (contentbytes[i] ^ Dkey ^ salt);    } return result; }}

The output is:

Symmetric encryption detail, and Java simple implementation

Related Article

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.