Nowadays, mobile phone apps are colorful. Ensuring the data security of mobile phone users is a skill that developers must master. The following is an example to introduce the use of DES in Android, ios and Java platforms. DES encryption is the most commonly used symmetric encryption method. It has better performance than asymmetric encryption (RSA) and is preferred for data encryption requested by mobile apps.

DES is a block algorithm that encrypts Data with a Key. The entry parameters of the algorithm are Key, Data, and Mode. Key: contains 7 bytes and 56 bits, which is the working Key of the DES algorithm. Data: 8 bytes of 64-bit Data to be encrypted or decrypted. Mode: indicates the working Mode of DES, including encryption and decryption. Introduction: 3DES (or Triple DES) is the general name of Triple Data Encryption Algorithm (TDEA) block cipher. It is equivalent to applying the DES encryption algorithm three times to each block of data. Due to the enhancement of computer computing power, the key length of the original DES password becomes easy to be cracked by violence; 3DES is designed to provide a relatively simple way to avoid such attacks by increasing the key length of DES, rather than designing a new block cipher algorithm. Therefore, 3DES is more secure than the original DES.

Encryption example: Java version ` package com. V1. Linxun. Portal. Utils;

import java.security.Key;

import javax.crypto.Cipher; import javax.crypto.SecretKeyFactory; import javax.crypto.spec.DESedeKeySpec; import javax.crypto.spec.IvParameterSpec;

/ * *

  • 3DES Encryption tool class */ public class Des3Util {private final static String secretKey = “123456789012345678901234”; private Final static String secretKey = “123456789012345678901234”; Private final static String iv = “01234567”; private final static String iv = “01234567”; Private final static String Encoding = “UTF-8 “; private final static String Encoding =” UTF-8 “;

    /** * 3DES ** @plainText * @return * @throws Exception */ public static String encode(String plainText) throws Exception { Key deskey = null; DESedeKeySpec spec = new DESedeKeySpec(secretKey .getBytes()); SecretKeyFactory keyfactory = SecretKeyFactory.getInstance( "desede"); deskey = keyfactory.generateSecret( spec); Cipher cipher = Cipher.getInstance( "desede/CBC/PKCS5Padding"); IvParameterSpec ips = new IvParameterSpec( iv.getBytes()); cipher.init(Cipher. ENCRYPT_MODE, deskey, ips); byte[] encryptData = cipher.doFinal( plainText.getBytes( encoding)); return Base64. encode( encryptData);Copy the code

    }

    /** * 3DES encryptText ** @param encryptText * @return * @throws Exception */ public static String decode(String) encryptText) throws Exception { Key deskey = null; DESedeKeySpec spec = new DESedeKeySpec( secretKey.getBytes()); SecretKeyFactory keyfactory = SecretKeyFactory.getInstance( "desede"); deskey = keyfactory. generateSecret( spec); Cipher cipher = Cipher.getInstance( "desede/CBC/PKCS5Padding" ); IvParameterSpec ips = new IvParameterSpec( iv.getBytes()); cipher. init(Cipher. DECRYPT_MODE, deskey, ips); byte[] decryptData = cipher. doFinal(Base64. decode(encryptText )); return new String( decryptData, encoding);Copy the code

    }

    Public static void main(String args[]) throws Exception{String STR = "Hello"; System.out. println("---- before encryption ----- : "+ STR); String encodeStr = Des3Util. encode( str); System. The out. Println (" -- -- -- -- -- -- after encryption: "+ encodeStr); System.out. println("---- after decryption ----- : "+ des3util.decode (encodeStr));Copy the code

    }} `