Java encryption technology (II) -- symmetric encryption algorithm des & AES

Source: Internet
Author: User

From: http://snowolf.iteye.com/blog/380034

Next, we will introduce symmetric encryption algorithms. The most common is des data encryption algorithms.
Des
Des-Data Encryption Standard, which is the data encryption algorithm. It was published by IBM in 1975. The DES algorithm has three entry parameters: Key, data, and mode. Among them, the key is 8 bytes in 64-bit, which is the key of the DES algorithm; data is also 8 bytes in 64-bit, which is the data to be encrypted or decrypted; the mode is des, which can be encrypted or decrypted.
The DES algorithm converts a 64-bit plaintext input block to a 64-bit ciphertext output block. The key used by the DES algorithm is also 64-bit.

The Java code is as follows:For the coder class, see
Java encryption technology (I)

Java code
 
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;/*** des security coding component ** <PRE> * supports des, desede (tripledes, 3DES), AES, blowfish, RC2, and RC4 (arcfour) * des key size must be equal to 56 * desede (tripledes) key size must be equal to 112 or 1 68 * AES key size must be equal to 128,192 or 256, but 192 and 256 bits may not be available * blowfish key size must be multiple of 8, and can only range from 32 to 448 (random SIVE) * RC2 key size must be between 40 and 1024 bits * RC4 (arcfour) key size must be between 40 and 1024 bits * pay attention to JDK document http ://... /docs/technotes/guides/security/sunproviders.html * </PRE> ** @ author liangdong * @ v Ersion 1.0 * @ since 1.0 */public abstract class descoder extends coder {/*** algorithm <br> * can be replaced by any of the following algorithms, at the same time, the size of the key value changes accordingly. ** <PRE> * des key size must be equal to 56 * desede (tripledes) key size must be equal to 112 or 168 * AES key size must be equal to 128,192 or 256, but 192 and 256 bits may not be available * blowfish key size must be multiple of 8, and can only range from 32 to 448 (random SIVE) * RC2 key size must be between 40 and 1024 bits * RC4 (arcfour) key size must be between 40 and 1024 bits * </PRE> ** in Use the following code in the key Tokey (byte [] Key) Method * <code> secretkey = new secretkeyspec (Key, algorithm ); </code> replace * <code> * deskeyspec DKS = new deskeyspec (key); * secretkeyfactory keyfactory = secretkeyfactory. getinstance (algorithm); * secretkey = keyfactory. generatesecret (DKS); * </code> */public static final string algorithm = "des "; /*** convert the key <br> ** @ Param key * @ return * @ throws exception */Private Static key Tokey (byte [] Key) throws exception {deskeyspec DKS = new deskeyspec (key); secretkeyfactory keyfactory = secretkeyfactory. getinstance (algorithm); secretkey = keyfactory. generatesecret (DKS); // when using other symmetric encryption algorithms, such as AES and blowfish, replace the preceding three lines of code with the following code: // secretkey = new secretkeyspec (key, algorithm); Return secretkey;}/*** decrypt ** @ Param data * @ Param key * @ return * @ throws Exception */public static byte [] decrypt (byte [] data, string key) throws exception {key K = Tokey (decryptbase64 (key); cipher = cipher. getinstance (algorithm); cipher. init (cipher. decrypt_mode, k); Return cipher. dofinal (data );} /*** encrypt ** @ Param data * @ Param key * @ return * @ throws exception */public static byte [] encrypt (byte [] data, string key) throws exception {key K = Tokey (decryptbase64 (K ); Cipher = cipher. getinstance (algorithm); cipher. init (cipher. encrypt_mode, k); Return cipher. dofinal (data);}/*** generate key ** @ return * @ throws exception */public static string initkey () throws exception {return initkey (null );} /*** generate the key ** @ Param seed * @ return * @ throws exception */public static string initkey (string seed) throws exception {securerandom = NULL; If (seed! = NULL) {securerandom = new securerandom (decryptbase64 (SEED);} else {securerandom = new securerandom ();} keygenerator kg = keygenerator. getinstance (algorithm); kg. init (securerandom); secretkey = kg. generatekey (); Return encryptbase64 (secretkey. getencoded ());}}

Continuing with the implementation of the previous class, we use MD5 and Sha to encrypt the string to generate a key. This is a common key generation method.
A test class: Java code
 

Import static Org. JUnit. assert. *; import Org. JUnit. test;/***** @ author liangdong * @ version 1.0 * @ since 1.0 */public class descodertest {@ testpublic void test () throws exception {string inputstr = "des "; string key = descoder. initkey (); system. err. println ("Original: \ t" + inputstr); system. err. println ("key: \ t" + key); byte [] inputdata = inputstr. getbytes (); inputdata = descoder. encrypt (inputdata, key); system. err. println ("encrypted: \ t" + descoder. encryptbase64 (inputdata); byte [] outputdata = descoder. decrypt (inputdata, key); string outputstr = new string (outputdata); system. err. println ("decrypted: \ t" + outputstr); assertequals (inputstr, outputstr );}}

The output is as follows: Console code
 

Original article: des key: f3wetrrv6q0 = encrypted: c6qe9onizry = decrypted: des

The output from the console is consistent with the encryption and decryption results. This is a simple encryption and decryption method with only one key.
In fact, Des has many siblings, such as desede (tripledes), AES, blowfish, RC2, and RC4 (arcfour ). Here I will not elaborate much about it. The same is true. You only need to replace algorithm with the corresponding value and make a code replacement.Secretkey = new secretkeyspec (Key, algorithm );The key length is different.

Java code
 
/*** Des key size must be equal to 56 * desede (tripledes) key size must be equal to 112 or 168 * AES key size must be equal to 128,192 or 256, but 192 and 256 bits may not be available * blowfish key size must be multiple of 8, and can only range from 32 to 448 (random SIVE) * RC2 key size must be between 40 and 1024 bits * RC4 (arcfour) key size must be between 40 and 1024 bits **/

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.