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/1862645

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.