Symmetric encryption algorithms: DES encryption and Desede encryption and AES and PBE

Source: Internet
Author: User

The symmetric encryption algorithm is to say that the encrypting party and the decryption party use the same key. Common symmetric encryption algorithms include 4, Des,desede (3DES), Aes,pbe.

This article discusses the cryptographic algorithm, not the message Digest, not the encoding. Here's a distinction between the three concepts.

Encryption algorithm is a one-to-one mapping, plaintext cipher correspondence. Encryption is ambiguous and obscure.

A message digest is a cipher that corresponds to multiple plaintext, which is simply a fingerprint of the whole text, a reflection, a digest.

Encoding is a one-to-one mapping, is clear, is obviously easy to see, such as base64 encoding.

DES (Data Encryption Standard) is called encryption standards. It is explicitly divided into 64-bit one-component block encryption, and the key is also 64-bit. Where the 7,15,23,31,39,47,56,63 bit of the key is to make up and check the bit ( The number of 1 in a byte is guaranteed to be an odd number, so the actual valid bit of the key is 56 bits.

Des in Java can be called as follows:

Importjava.security.InvalidKeyException;Importjava.security.NoSuchAlgorithmException;ImportJava.security.SecureRandom;Importjava.security.spec.InvalidKeySpecException;Importjavax.crypto.BadPaddingException;ImportJavax.crypto.Cipher;Importjavax.crypto.IllegalBlockSizeException;ImportJavax.crypto.KeyGenerator;Importjavax.crypto.NoSuchPaddingException;ImportJavax.crypto.SecretKey;Importjavax.crypto.SecretKeyFactory;ImportJavax.crypto.spec.DESKeySpec; Public classDES {Private Final StaticString key_des = "DES"; StaticString TOS (byte[] b) {String ans= "";  for(inti = 0; i < b.length; i++) {ans+ = String.Format ("%02x", B[i]); }        returnans; }     Public Static voidMain (string[] args)throwsnosuchalgorithmexception, InvalidKeyException, Nosuchpaddingexception, Invalidkeyspecexception, Illegalblocksizeexception, badpaddingexception {securerandom secure=NewSecureRandom ("Weidiao". GetBytes ());; Keygenerator Generator=keygenerator.getinstance (key_des);        Generator.init (secure); byte[] key =Generator.generatekey (). getencoded (); Deskeyspec DKs=NewDeskeyspec (key); Secretkeyfactory Factory=secretkeyfactory.getinstance (key_des); Secretkey Secretkey=Factory.generatesecret (DKS); //When using other symmetric encryption algorithms, such as AES, Blowfish and other algorithms, replace the above three lines of code with the following code//Secretkey = new Secretkeyspec (key, key_des);        byte[] data = "123". GetBytes (); Cipher Cipher=cipher.getinstance (key_des);        Cipher.init (Cipher.encrypt_mode, Secretkey); byte[] Encrypt =cipher.dofinal (data);        Cipher.init (Cipher.decrypt_mode, Secretkey); byte[] Decrypt =cipher.dofinal (encrypt); System.out.println ("Des key:" +tos (key)); System.out.println ("Raw data:" +tos (data)); System.out.println ("After encryption:" +tos (encrypt)); System.out.println ("After decryption:" +tos (decrypt)); }}

3DES is not a new encryption algorithm, but on the basis of DES more operations several times, so that the encryption effect is better, it takes full advantage of Des reversibility, not like Md5,sha ciphertext to clear text for a one-to-many mapping, des vmcontext for one-to-one mapping.

The 3DES algorithm is called the Desede algorithm, E for encryption, d for decryption, in fact, the 3DES algorithm is shown below, E for the cryptographic function, d for the decryption function, a total of 3 pairs of encryption key:

Encrypt=e3 (D2 (E1 (data)))

DECRYPT=D1 (E2 (D3 (encrypt)))

Change the Deskeyspec in the above code to DESEDEKEYSPEC, and change the key_des to "Desede" to run the Desede encryption algorithm.

Symmetric encryption algorithms: DES encryption and Desede encryption and AES and PBE

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.