Common encryption algorithms-symmetric encryption

Source: Internet
Author: User
Tags base64

650) this.width=650; "src=" Http://s5.51cto.com/wyfs02/M01/89/00/wKiom1gEbU7Rk8J9AAAgh7lHOtQ358.png "title=" Symmetric encryption. png "alt=" Wkiom1gebu7rk8j9aaagh7lhotq358.png "/>

Symmetric encryption is the fastest and simplest way to encrypt encryption and decryption with the same key. Symmetric encryption has many algorithms, and because of its high efficiency, it is widely used in the core of many cryptographic protocols.

Symmetric encryption typically uses a relatively small key, typically less than the size of a bit. The greater the key, the stronger the encryption, but the slower the encryption and decryption process.

Common symmetric encryption algorithms: DES algorithm, 3DES algorithm, AES algorithm

Features: The algorithm is open, the computational amount is small, the encryption speed is fast, and the encryption efficiency is high. Its security mainly depends on the security of the secret key. There is only one key used when encrypting.


Des algorithm

Symmetric encryption algorithm, the plaintext is grouped by 64 bits, the key is 64 bits long, but in fact only 56 bits participate in the DES operation, 8th, 16, 24, 32, 40, 48, 56, 64 bits are check bits, so that each key has an odd digit. The ciphertext is formed by a bitwise substitution or Exchange method after grouping the plaintext and the 56-bit key.

   /**     * DES  Generate key      * @ return     *  @throws  Exception     */     public static string genkeydes ()  throws Exception {         keygenerator keygen = keygenerator.getinstance ("DES");         keygen.init ( //) generates a key of 56 bits          secretkey key = keygen.generatekey ();         string base64str = byte2base64 (key.getencoded ());         return base64Str;    }    /**      *  DES  convert string keys to Secretkey objects      *  @param   Base64key     *  @return      *  @throws  exception     */     public static secretkey loadkeydes (String base64key)  throws  Exception {        byte[] bytes =  Base642byte (Base64key);         secretkey key = new  secretkeyspec (bytes,  "DES");        return key;     }    /**     *DES  symmetric encryption using a generated key       *  @param  source  encrypted byte array      *  @param  key   keys      *  @return      * @ Throws exception     */    public static byte[]  encryptdes (BYTE[] SOURCE,SECRETKEY&NBSp;key)  throws exception {        cipher cipher  = cipher.getinstance ("DES");         cipher.init ( Cipher.encrypt_mode, key);        byte[] bytes =  Cipher.dofinal (source);        return bytes;     }    /**     * des decrypts it using a generated key       *  @param  source     *  @param  key      *  @return      *  @throws  exception     * /    public static byte[] decryptdes (Byte[] source,secretkey key)  throws Exception{        Cipher cipher =  Cipher.getinstance ("DES");  &nBsp;      cipher.init (Cipher.decrypt_mode, key);         byte[] bytes = cipher.dofinal (source);         return bytes;    }    /**      * base64  encode      *  @param  base64      *  @return      *  @throws  IOException      */    private static byte[] base642byte (STRING BASE64)  throws IOException {         BASE64Decoder  Bs = new base64decoder ();         return  Bs.decodebuffer (base64);     }    /**      * base64  decoding   &NBsp;  *  @param  bytes     *  @return       */    private static string byte2base64 (byte[] bytes)  {         base64encoder bse = new base64encoder ();         return bse.encode (bytes);     }

AES algorithm

one of the most popular algorithms in symmetric encryption algorithms used worldwide. The design has three key lengths (128,192,256 bits), which is more secure than the DES encryption algorithm .

/**     * aes  get public key      *  @return       *  @throws  exception     */    public  static string genkeyaes ()  throws Exception {         keygenerator keygenerator= keygenerator.getinstance ("AES");         keygenerator.init (        secretkey);  key = keygenerator.generatekey ();        string  Base64str = byte2base64 (key.getencoded ());         return  base64str;    }    /**     * aes   Generate keys for Secretkey objects      *  @param  base64Key      *  @return   &NBsp;  *  @throws  exception     */    public  static secretkey loadkeyaes (String base64key)  throws Exception {         byte[] bytes = base642byte (Base64Key);         secretkey key = new secretkeyspec (bytes,  "AES");         return key;    }     /**     * AES   Data Encryption      *  @param  source     *  @param  key     *  @return      *  @throws  Exception     */     public static byte[] encryptaes (Byte[] source,secretkey key)  throws  Exception{   &nbSp;    cipher cipher = cipher.getinstance ("AES");         cipher.init (Cipher.encrypt_mode, key);         byte[] bytes = cipher.dofinal (source);         Return bytes;    }    /**     * aes   Data decryption      *  @param  source     *  @param  key     *  @return      *  @throws   exception     */    public static byte[]  Decryptaes (Byte[] source,secretkey key)  throws Exception{         cipher cipher = cipher.getinstance ("AES");         cipher.init (cipher.decryPt_mode, key);        byte[] bytes =  Cipher.dofinal (source);        return bytes;     }


This article is from the "in the Eyes of the Sun" blog, please be sure to keep this source http://wang963825.blog.51cto.com/8695943/1862692

Common encryption algorithms-symmetric encryption

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.