In the previous section, we introduced the use of some encryption and decryption classes. Let’s put them together to do a simple test. The code is as follows:
MainActivity:
Copy the code code as follows:
package com.home.testdes;
import android.os.Bundle;
import android.util.Log;
import android.app.Activity;
public class MainActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
DESUtil u = new DESUtil();
String mi = u.getEnc("I love you");
Log.i("After encryption", mi);
String ming = u.getDec(mi);
Log.i("After decryption", ming);
}
}
Encryption and decryption tools:
Copy the code code as follows:
package com.home.testdes;
import java.security.Key;
import java.security.spec.AlgorithmParameterSpec;
import javax.crypto.Cipher;
import javax.crypto.SecretKeyFactory;
import javax.crypto.spec.DESKeySpec;
import javax.crypto.spec.IvParameterSpec;
import android.util.Base64;
/**
* Use DES encryption and decryption tools
*
* @author Administrator
*
*/
public class DESUtil {
private Key key;// key value of the key
private byte[] DESkey;
private byte[] DESIV = { 0x12, 0x34, 0x56, 0x78, (byte) 0x90, (byte) 0xAB,
(byte) 0xCD, (byte) 0xEF };
private AlgorithmParameterSpec iv = null;//Parameter interface of encryption algorithm
public DESUtil() {
try {
this.DESkey = "abcdefghijk".getBytes("UTF-8");//Set the key
DESKeySpec keySpec = new DESKeySpec(DESkey);//Set key parameters
iv = new IvParameterSpec(DESIV); // Set vector
SecretKeyFactory keyFactory = SecretKeyFactory.getInstance("DES");//Get the key factory
key = keyFactory.generateSecret(keySpec);//Get the key object
} catch (Exception e) {
e.printStackTrace();
}
}
/**
* Encrypted String, plain text input, cipher text output
*
* @param inputString
* Plain text to be encrypted
* @return encrypted string
*/
public String getEnc(String inputString) {
byte[] byteMi = null;
byte[] byteMing = null;
String outputString = "";
try {
byteMing = inputString.getBytes("UTF-8");
byteMi = this.getEncCode(byteMing);
byte[] temp = Base64.encode(byteMi, Base64.DEFAULT);
outputString = new String(temp);
} catch (Exception e) {
} finally {
byteMing = null;
byteMi = null;
}
return outputString;
}
/**
* Decrypt String and input plaintext as ciphertext and output
*
* @param inputString
* The string that needs to be decrypted
* @return decrypted string
*/
public String getDec(String inputString) {
byte[] byteMing = null;
byte[] byteMi = null;
String strMing = "";
try {
byteMi = Base64.decode(inputString.getBytes(), Base64.DEFAULT);
byteMing = this.getDesCode(byteMi);
strMing = new String(byteMing, "UTF8");
} catch (Exception e) {
} finally {
byteMing = null;
byteMi = null;
}
return strMing;
}
/**
* Encryption uses byte[] plain text input and byte[] cipher text output
*
* @param bt
* Bytecode to be encrypted
* @return encrypted bytecode
*/
private byte[] getEncCode(byte[] bt) {
byte[] byteFina = null;
Cipher cipher;
try {
// Get Cipher instance
cipher = Cipher.getInstance("DES/CBC/PKCS5Padding");
cipher.init(Cipher.ENCRYPT_MODE, key, iv);
byteFina = cipher.doFinal(bt);
} catch (Exception e) {
e.printStackTrace();
} finally {
cipher = null;
}
return byteFina;
}
/**
* Decryption uses byte[] ciphertext input and byte[] plaintext output
*
* @param bt
* Bytecode to be decrypted
* @return decrypted bytecode
*/
private byte[] getDesCode(byte[] bt) {
Cipher cipher;
byte[] byteFina = null;
try {
// Get Cipher instance
cipher = Cipher.getInstance("DES/CBC/PKCS5Padding");
cipher.init(Cipher.DECRYPT_MODE, key, iv);
byteFina = cipher.doFinal(bt);
} catch (Exception e) {
e.printStackTrace();
} finally {
cipher = null;
}
return byteFina;
}
}