Using DES encryption to decrypt instances in Java _java

Source: Internet
Author: User
Tags base64 decrypt ming

In the previous introduction of the use of some encryption and decryption classes, here combined to do a simple test, the code is as follows:

Mainactivity:

Copy 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 ("Encrypted", MI);
String ming = U.getdec (mi);
LOG.I ("decrypted", Ming);
}
}

Cryptographic decryption Tool Class:

Copy 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 tool classes
*
* @author Administrator
*
*/
public class Desutil {

Key value for private key key;//
Private byte[] Deskey;
Private byte[] Desiv = {0x12, 0x34, 0x56, 0x78, (Byte) 0x90, (byte) 0xAB,
(byte) 0xCD, (byte) 0xEF};
Private Algorithmparameterspec IV = parameter interface of null;//encryption algorithm

 public Desutil () {
  try {
   this. Deskey = "Abcdefghijk". GetBytes ("UTF-8");/Set key
   deskeyspec keyspec = new Deskeyspec (Deskey); Set key parameter
   iv = new Ivparameterspec (DESIV);//Set vector
   secretkeyfactory Keyfactory = Secretkeyfactory.getinstance ("DES");//Get key factory
   key = Keyfactory.generatesecret ( KEYSPEC);//Get Key Object
  } catch (Exception e) {
   e.printstacktrace ();
 &NBSP}
 }

 /**
  * Encrypt string clear text input ciphertext output
  *
  * @param inputstring
  *   & nbsp;        to encrypt the plaintext
  * @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 with ciphertext input plaintext output
  *
  * @param inputstring
  *   & nbsp;        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;
 }

 /**
  * Encrypt with byte[] plaintext input, byte[] redaction output
  *
  * @param bt
  *             byte code 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;
 &NBSP}
  return Bytefina;
 .}

/**
* Decrypt with byte[] ciphertext input, in byte[] Clear text output
*
* @param BT
* Bytecode to be decrypted
* @return The decrypted byte code
*/
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;
}
}

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.