Information Encryption and information encryption technology

Source: Internet
Author: User

Information Encryption and information encryption technology

There are various encryption methods for information. We have introduced a self-designed encryption method. If you are interested, you can enjoy it. Thank you for your advice. Today, we will introduce the symmetric encryption method. The so-called symmetric encryption refers to the symmetric format of encryption and decryption methods, that is, decryption is the inverse process of encryption. Let's take a look at it: the encryption strength of DES, 3DES, AES, and PBE increases sequentially. Well, let's talk less about the Code:

First, let's take a look at DES:

Package cn.edu. hpu. des; import java. security. key; import javax. crypto. cipher; import javax. crypto. keyGenerator; import javax. crypto. secretKey; import javax. crypto. secretKeyFactory; import javax. crypto. spec. DESKeySpec; public class jdk_DES {private static final String src = "I Love You! "; Public static void main (String [] args) {jdk_DES.jdkDES () ;}@ SuppressWarnings (" static-access ") public static void jdkDES () {try {// obtain KEY KeyGenerator keyGenerator = KeyGenerator. getInstance ("DES"); keyGenerator. init (56); // set it to the default value 56. // you can obtain the KEY object SecretKey secrekeyone = keyGenerator. generateKey (); byte [] byteskey = secrekeyone. getEncoded (); // KEY conversion DESKeySpec deskeyspec = new DESKeySpec (byteskey); SecretKeyFactory factory = SecretKeyFactory. getInstance ("DES"); Key secerkeytwo = factory. generateSecret (deskeyspec); // encrypt Cipher cipher = Cipher. getInstance ("DES/ECB/PKCS5Padding"); cipher. init (cipher. ENCRYPT_MODE, secerkeytwo); // sets the mode to encrypted byte [] result = cipher. doFinal (src. getBytes (); System. out. println ("jdkEDS:" + result. toString (); // decrypt cipher. init (cipher. DECRYPT_MODE, secerkeytwo); // sets the decryption mode to result = cipher. doFinal (result); System. out. println ("jdkEDS:" + new String (result);} catch (Exception e) {e. printStackTrace ();}}}

Running result:

The second three-weight DES:

Package cn.edu. hpu. des; import java. security. key; import java. security. secureRandom; import javax. crypto. cipher; import javax. crypto. keyGenerator; import javax. crypto. secretKey; import javax. crypto. secretKeyFactory; import javax. crypto. spec. DESKeySpec; public class jdk_3DES {private static final String src = "I Love You! "; Public static void main (String [] args) {jdk_3DES.jdk3DES () ;}@ SuppressWarnings (" static-access ") public static void jdk3DES () {try {// obtain KEY KeyGenerator keyGenerator = KeyGenerator. getInstance ("DESede"); // keyGenerator. init (112); // set the key length. The default value is 168, or 112 keyGenerator. init (new SecureRandom (); // set it to the default value // obtain the KEY object SecretKey secrekeyone = keyGenerator. generateKey (); byte [] byteskey = secrekeyone. getEncoded (); // KEY conversion DESKeySpec deskeyspec = new DESKeySpec (byteskey); SecretKeyFactory factory = SecretKeyFactory. getInstance ("DES"); Key secerkeytwo = factory. generateSecret (deskeyspec); // encrypt Cipher cipher = Cipher. getInstance ("DES/ECB/PKCS5Padding"); cipher. init (cipher. ENCRYPT_MODE, secerkeytwo); // sets the mode to encrypted byte [] result = cipher. doFinal (src. getBytes (); System. out. println ("jdkEDS:" + result. toString (); // decrypt cipher. init (cipher. DECRYPT_MODE, secerkeytwo); // sets the decryption mode to result = cipher. doFinal (result); System. out. println ("jdkEDS:" + new String (result);} catch (Exception e) {e. printStackTrace ();}}}

Running result:

Third AES:

Package cn.edu. hpu. aes; import java. security. key; import java. security. secureRandom; import javax. crypto. cipher; import javax. crypto. keyGenerator; import javax. crypto. secretKey; import javax. crypto. spec. secretKeySpec; public class jdk_AES {private static final String src = "I Love You! "; Public static void main (String [] args) {AES ();} public static void AES () {try {// obtain key KeyGenerator keyGenerator = KeyGenerator. getInstance ("AES"); keyGenerator. init (new SecureRandom (); // The default key length is: SecretKey secretKey = keyGenerator. generateKey (); byte [] keyBytes = secretKey. getEncoded (); // key to convert Key key = new SecretKeySpec (keyBytes, "AES"); // encrypt Cipher cipher = Cipher. getInstance ("AES/ECB/PKCS5Padding"); cipher. init (Cipher. ENCRYPT_MODE, key); byte [] result = cipher. doFinal (src. getBytes (); System. out. println ("AES =" + result. toString (); // decrypt cipher. init (Cipher. DECRYPT_MODE, key); result = cipher. doFinal (result); System. out. println ("AES =" + new String (result);} catch (Exception e) {e. printStackTrace ();}}}

Running result:

Last PBE:

Package cn.edu. hpu. pbe; import java. security. key; import java. security. secureRandom; import javax. crypto. cipher; import javax. crypto. secretKeyFactory; import javax. crypto. spec. PBEKeySpec; import javax. crypto. spec. PBEParameterSpec; public class jdk_PBE {public static final String src = "I Love You! "; Public static void main (String [] args) {jdkPBE ();} private static void jdkPBE () {try {// initialize salt SecureRandom random = new SecureRandom (); byte [] salt = random. generateSeed (8); // password and key String password = "imooc"; PBEKeySpec pbeKeySpec = new PBEKeySpec (password. toCharArray (); // generate the secret conversion object SecretKeyFactory factory = SecretKeyFactory. getInstance ("PBEWITHMD5andDES"); Key key = factory. generateSecret (pbeKeySpec); // encrypt PBEParameterSpec pbeParameterSpec = new PBEParameterSpec (salt, 100); // instantiate an input material of the PBE object: the parameters are "salt and iterations" Cipher cipher = Cipher. getInstance ("PBEWITHMD5andDES"); cipher. init (Cipher. ENCRYPT_MODE, key, pbeParameterSpec); byte [] result = cipher. doFinal (src. getBytes (); System. out. println ("PBE:" + result. toString (); // decrypt cipher. init (Cipher. DECRYPT_MODE, key, pbeParameterSpec); result = cipher. doFinal (result); System. out. println ("PBE:" + new String (result);} catch (Exception e) {e. printStackTrace ();}}}

Running result:

For the above four methods, PBE is a safer encryption method, which determines the encryption result by salt and password. Which of the above is a better method even if you want to introduce the symmetric encryption method.

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.