[Android development experience] a safer algorithm than DES encryption-3DES encryption algorithm, android3des

Source: Internet
Author: User

[Android development experience] a safer algorithm than DES encryption-3DES encryption algorithm, android3des

Reprinted please indicate the source: http://blog.csdn.net/zhaokaiqiang1992

In the previous article, we discussed the DES algorithm and learned how to ensure the consistency of encryption and decryption results on different platforms. However, DES, as an encryption algorithm that has been around for a long time, is prone to brute-force cracking as the computer's computing power is enhanced, and its security becomes a little low. Therefore, to enhance data security, the 3DES algorithm came into being.

3DES, as its name implies, is an improvement to the DES encryption algorithm. 3DES encrypts each data three times to reduce the possibility of cracking.

To use 3DES encryption, follow these steps:

① Input the agreed key (keyBytes) and Algorithm (Algorithm) to build the SecretKey object

SecretKey secret ey = new SecretKeySpec (keyBytes, Algorithm );

② Instantiate the Cipher object based on the algorithm. It is responsible for encryption/Decryption

Cipher c1 = Cipher. getInstance (Algorithm );

③ Pass in the encryption/Decryption mode and SecretKey key object, and instantiate the Cipher object

C1.init (Cipher. ENCRYPT_MODE, Cipher ey );

④ Pass in the byte array, call the Cipher. doFinal () method, implement encryption/decryption, and return a 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 encryption and decryption tool class * @ author zhaokaiqiang * @ date 11:28:14 **/public class DES3Utils {// defines the encryption algorithm, DESede: 3 DESprivate 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 secret key secret ey = new SecretKeySpec (build3cipher ey (PASSWORD_CRYPT_KEY), Algorithm); // instantiate CipherCipher cipher = Cipher. getInstance (Algorithm); cipher. init (Cipher. ENCRYPT_MODE, cipher ey); 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 secret ey = new SecretKeySpec (build3cipher ey (PASSWORD_CRYPT_KEY), Algorithm); Cipher c1 = Cipher. getInstance (Algorithm); c1.init (Cipher. DECRYPT_MODE, cipher ey); 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 a 24-bit byte array of keys based on strings ** @ param keyStr * @ return * @ throws UnsupportedEncodingException */public static byte [] build3DesKey (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 = "use 3DES to encrypt data"; System. out. println ("[before encryption]:" + msg); // encrypt byte [] secretArr = DES3Utils. encryptMode (msg. getBytes (); System. out. println ("[encrypted]:" + new String (secretArr); // decrypt byte [] myMsgArr = DES3Utils. decryptMode (secretArr); System. out. println ("[decrypted]:" + new String (myMsgArr ));}}


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.