Symmetric password programming using (DES, 3DES, AES) __ Programming

Source: Internet
Author: User
Tags crypt decrypt
The concept of symmetric cryptography
Encryption key and encryption key is the same, for most symmetric cryptographic algorithms, encryption and decryption process of the mutual reverse encryption and decryption communication model


Features: The algorithm is open, small computational capacity, fast encryption, high encryption efficiency weaknesses: Both sides use the same key, security is not guaranteed

programming use of DES algorithms (short key (56 bit), short life cycle)

Package com.crypt.des;

Import Com.crypt.ByteToHexUtil;
Import Javax.crypto.Cipher;
Import Javax.crypto.KeyGenerator;
Import Javax.crypto.SecretKey;

Import Javax.crypto.spec.SecretKeySpec;
 /** * DES encryption * Created by Zhangweixiang on 4/17/2016.
    * * public class Desutil {/** * generates Desckey * @param type * @return * @throws Exception * * public static byte[] GenerateKey (String type) throws Exception {Keygenerator Keygenerator = Keygenerator.getinst
        ance (type);
        Keygenerator.init (56);
        Secretkey Secretkey = Keygenerator.generatekey ();
    return secretkey.getencoded ();
     /** * @param key key * @param type encryption with the specified Deskey encryption * @param data encryption * @return encrypted information
        * @throws Exception */public static byte[] Encrypt (byte[] data,byte[] key,string type) throws Exception {
        Secretkey Secretkey = new Secretkeyspec (key,type);
        Cipher Cipher = cipher.getinstance (type); CiphEr.init (Cipher.encrypt_mode,secretkey);
    return cipher.dofinal (data);
     /** * des decryption * @param data needs to be decrypted * @param key decryption secret key * @param type types * @return decrypted results
        * @throws Exception */public static byte[] Decrypt (byte[] data,byte[] key,string type) throws Exception {
        Secretkey Secretkey = new Secretkeyspec (key,type);
        Cipher Cipher = cipher.getinstance (type);
        Cipher.init (Cipher.decrypt_mode,secretkey);
    return cipher.dofinal (data);
        public static void Main (string[] args) throws Exception {String data = "Test desc";
        String type = "DES";//des/ecb/pkcs5padding byte[] key = Desutil.generatekey (type);
        byte[] Encdata = Desutil.encrypt (Data.getbytes (), key,type);
        String encdatastr = bytetohexutil.bytestohexstring (Encdata);

        System.out.println (data+ ">>des encrypt>>" +encdatastr);
     byte[] Decdata = Desutil.decrypt (Encdata,key,type);   System.out.println (encdatastr+ ">>des decrypt>>" +new String (Decdata));
 }
}

programming use of 3DES algorithm

Increase the key length to 112-bit or 168-bit, improving security by increasing the number of iterations

Disadvantages: Slow processing speed, long key calculation time, low encryption efficiency

Package com.crypt.des;

Import Com.crypt.ByteToHexUtil;
Import Javax.crypto.Cipher;
Import Javax.crypto.KeyGenerator;
Import Javax.crypto.SecretKey;

Import Javax.crypto.spec.SecretKeySpec;
 /** * 3DES encryption * Created by Zhangweixiang on 4/17/2016.
     /public class Tripledesutils {/** * generates 3desckey * @param type * @return * @throws Exception */public static byte[] GenerateKey (String type) throws Exception {Keygenerator Keygenerator = Keygenerator
        . getinstance (type);
        Keygenerator.init (112);//128 Secretkey Secretkey = Keygenerator.generatekey ();
    return secretkey.getencoded (); 
     /** * @param key key * @param type encryption with the specified 3deskey encryption * @param data encryption * @return encrypted information
        * @throws Exception */public static byte[] Encrypt (byte[] data,byte[] key,string type) throws Exception {
        Secretkey Secretkey = new Secretkeyspec (key,type); Cipher Cipher = cipher.getinstance(type);
        Cipher.init (Cipher.encrypt_mode,secretkey);
    return cipher.dofinal (data);
     /** * 3DES decryption * @param data needs to be decrypted * @param key decryption secret key * @param type types * @return decrypted results
        * @throws Exception */public static byte[] Decrypt (byte[] data,byte[] key,string type) throws Exception {
        Secretkey Secretkey = new Secretkeyspec (key,type);
        Cipher Cipher = cipher.getinstance (type);
        Cipher.init (Cipher.decrypt_mode,secretkey);
    return cipher.dofinal (data);
        public static void Main (string[] args) throws Exception {String data = "Test desc";
        String type = "Desede";//des/ecb/pkcs5padding byte[] key = Tripledesutils.generatekey (type);
        byte[] Encdata = Tripledesutils.encrypt (Data.getbytes (), key,type);
        String encdatastr = bytetohexutil.bytestohexstring (Encdata);

        System.out.println (data+ ">>3des encrypt>>" +encdatastr); byte[] Decdata = TRipledesutils.decrypt (Encdata,key,type);
    System.out.println (encdatastr+ ">>3des decrypt>>" +new String (Decdata));
 }
}

the programming use of AES algorithm

1.AES: Advanced data Encryption Standard, capable of effectively resisting all known attacks against the DES algorithm

2. Features: Key to establish a short time, good sensitivity, low memory demand, high security

Package com.crypt.des;

Import Com.crypt.ByteToHexUtil;
Import Javax.crypto.Cipher;
Import Javax.crypto.KeyGenerator;
Import Javax.crypto.SecretKey;

Import Javax.crypto.spec.SecretKeySpec;
 /** * AES Encryption/decryption * Created by Zhangweixiang on 4/17/2016.
    * * public class Aesutil {/** * generates Adesckey * @param type * @return * @throws Exception * * public static byte[] GenerateKey (String type) throws Exception {Keygenerator Keygenerator = Keygenerator.getins
        Tance (type);
                                Keygenerator.init (128);//default is 128 digits, if use 192 256 then need to obtain no policy file (download UnlimitedJECPolicyJDK7 from Oracle official website after decompression will
        2 of these jars are copied to the security under the JRE under Lib, Secretkey Secretkey = Keygenerator.generatekey ();
    return secretkey.getencoded ();
     /** * @param key key * @param type encryption with the specified Aeskey encryption * @param data encryption * @return encrypted information * @throws Exception */public static byte[] Encrypt (byte[] data,byte[] key,string type) throws Exception {Secretkey Secretkey = new Secretkeyspec (key,type);
        Cipher Cipher = cipher.getinstance (type);
        Cipher.init (Cipher.encrypt_mode,secretkey);
    return cipher.dofinal (data);
     /** * AES Decryption * @param data needs to be decrypted * @param key decryption secret key * @param type types * @return decrypted results
        * @throws Exception */public static byte[] Decrypt (byte[] data,byte[] key,string type) throws Exception {
        Secretkey Secretkey = new Secretkeyspec (key,type);
        Cipher Cipher = cipher.getinstance (type);
        Cipher.init (Cipher.decrypt_mode,secretkey);
    return cipher.dofinal (data);
        public static void Main (string[] args) throws Exception {String data = "Test desc";
        String type = "AES";//aes/ecb/pkcs5padding byte[] key = Aesutil.generatekey (type);
        byte[] Encdata = Aesutil.encrypt (Data.getbytes (), key,type);
String encdatastr = bytetohexutil.bytestohexstring (Encdata);        System.out.println (data+ ">>aes encrypt>>" +encdatastr);
        byte[] Decdata = Aesutil.decrypt (Encdata,key,type);
    System.out.println (encdatastr+ ">>aes decrypt>>" +new String (Decdata)); }
}

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.