"Android Development experience" is more secure than DES encryption algorithm--3des encryption algorithm

Source: Internet
Author: User

Reprint Please specify source: http://blog.csdn.net/zhaokaiqiang1992

In the previous article, we discussed the DES algorithm, but also understand how to ensure the consistency of the encryption and decryption results under different platforms. But Des, as a kind of encryption algorithm that has appeared for a long time, with the enhancement of computer computing ability, des encryption is prone to brute force, and its security becomes a bit low. Therefore, in order to enhance the security of data, 3DES algorithm has emerged.

3DES, as the name implies, is the improvement of DES encryption algorithm, 3DES by each data 3 times DES encryption, thereby reducing the likelihood of being cracked.

If we're going to use 3DES encryption, here are a few steps

① Pass in the common contract key (keybytes) and the algorithm (algorithm) to build the Secretkey Key Object

Secretkey Deskey = new Secretkeyspec (keybytes, algorithm);

② instantiates Cipher objects based on the algorithm. It is responsible for encrypting / decrypting

Cipher C1 = cipher.getinstance (algorithm);

③ incoming encryption / decryption mode and Secretkey Key object, instantiating Cipher Object

C1.init (Cipher.encrypt_mode, Deskey);

④ Incoming byte array, calls the cipher.dofinal () method, implements encryption / decryption, and returns a byte byte array

C1.dofinal (SRC);

The specific code implementation process is as follows

Package Com.qust;import Java.io.unsupportedencodingexception;import Javax.crypto.cipher;import Javax.crypto.secretkey;import javax.crypto.spec.secretkeyspec;/** * * @ClassName: Com.qust.SecretUtils * @Description : 3DES Cryptographic Decryption Tool class * @author Zhaokaiqiang * @date 2014-11-13 PM 11:28:14 * */public class Des3utils {//define encryption algorithm, Desede is 3DESpriva  Te static final string algorithm = "Desede";//encryption Key private static final String Password_crypt_key = "zhaokaiqiang1992";/** * Encryption Method * * @param src * byte array of source data * @return */public static byte[] Encryptmode (byte[] src) {try {//Generate key Secret Key Deskey = new Secretkeyspec (Build3deskey (Password_crypt_key), algorithm);//instantiation Ciphercipher cipher = Cipher.getinstance (algorithm); Cipher.init (Cipher.encrypt_mode, Deskey); return cipher.dofinal (SRC);} catch (Java.security.NoSuchAlgorithmException E1) {e1.printstacktrace ();} catch ( Javax.crypto.NoSuchPaddingException E2) {e2.printstacktrace ();} catch (Java.lang.Exception E3) {e3.printstacktrace () ;} return null;} /** * Decryption function * * @param src * Ciphertext byte array * @return */public static byte[] Decryptmode (byte[] src) {try {Secretkey Deskey = New Secretkeyspec (Build3deskey (Password_crypt_key), algorithm); Cipher C1 = cipher.getinstance (algorithm); C1.init (Cipher.decrypt_mode, Deskey); return c1.dofinal (SRC);} catch (Java.security.NoSuchAlgorithmException E1) {e1.printstacktrace ();} catch ( Javax.crypto.NoSuchPaddingException E2) {e2.printstacktrace ();} catch (Java.lang.Exception E3) {e3.printstacktrace () ;} return null;} /** * Generate key 24-bit byte array based on String * * @param keystr * @return * @throws unsupportedencodingexception */public static byte[] Build3d Eskey (String keystr) throws unsupportedencodingexception {byte[] key = new byte[24];byte[] temp = keystr.getbytes ("UTF-8" if (Key.length > Temp.length) {system.arraycopy (temp, 0, key, 0, temp.length);} else {system.arraycopy (temp, 0, key, 0, key.length);} Return key;}}

The code for the test class is as follows

Package Com.qust;public class Main {public static void main (string[] args) {String msg = "Encrypt data using 3DES"; System.out.println ("Pre-Encryption": "+ msg);//encryption byte[] Secretarr = Des3utils.encryptmode (Msg.getbytes ()); System.out.println ("" After Encryption ":" + new String (Secretarr));//decryption byte[] Mymsgarr = Des3utils.decryptmode (Secretarr); System.out.println ("After decryption": "+ new String (Mymsgarr));}}


"Android Development experience" is more secure than DES encryption algorithm--3des encryption algorithm

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.