Question

I have done a DES Util class according to mkyong's JCE Encryption – Data Encryption Standard (DES) Tutorial

Here's my class:

import javax.crypto.Cipher;
import javax.crypto.KeyGenerator;
import javax.crypto.SecretKey;

import tw.com.januarytc.android.singularsdk.lib.JsLib;
import android.util.Log;

public class DESUtil
{
  private KeyGenerator keyGen=null;
  private SecretKey sKey=null;
  private Cipher desCip=null;

  /**
   * Init. DES utility class
   * @return boolean
   */
  public boolean init()
  {
    boolean b=false;

    try
    {
      keyGen=KeyGenerator.getInstance("DES");
      sKey=keyGen.generateKey();
      desCip=Cipher.getInstance("DES/ECB/PKCS5Padding");
      b=true;
    }
    catch(Exception e)
    {
      Log.d(JsLib.TAG, "Init DESUtil failed: "+e.toString());
      e.printStackTrace();
      b=false;
    }
    return b;
  }

  /**
   * Encrypt string with DES
   * @param str - Original string
   * @return java.lang.String DES encrypted string
   * @throws IllegalStateException
   */
  public String encryptString(String str) throws IllegalStateException
  {
    if(keyGen==null || sKey==null || desCip==null){throw new IllegalStateException("DESUtil class has not been initialized.");}
    String ret="";
    try
    {
      desCip.init(Cipher.ENCRYPT_MODE, sKey);
      ret=new String(desCip.doFinal(str.getBytes("UTF-8")));
    }
    catch(Exception e)
    {
      e.printStackTrace();
      ret="";
    }
    return ret;
  }

  /**
   * Decrypt string which encrypted by DES
   * @param str - DES encrypted string
   * @return java.lang.String Original string
   * @throws IllegalStateException
   */
  public String decryptString(String strDes) throws IllegalStateException
  {
    if(keyGen==null || sKey==null || desCip==null){throw new IllegalStateException("DESUtil class has not been initialized.");}
    String ret="";
    try
    {
      desCip.init(Cipher.DECRYPT_MODE, sKey);
      ret=new String(desCip.doFinal(strDes.getBytes("UTF-8")));
    }
    catch(Exception e)
    {
      e.printStackTrace();
      ret="";
    }
    return ret;
  }
}

And as WiKi said:

In cryptography, Triple DES is the common name for the Triple Data Encryption Algorithm (TDEA or Triple DEA) block cipher, which applies the Data Encryption Standard (DES) cipher algorithm three times to each data block.

I'm just wondering what if I encrypt a string with DES for 3 times...Will it equals 3DES ?

Thanks for suggestions, and sorry for my poor english~

Was it helpful?

Solution

DES uses a short 56 bit key, which was vulnerable to brute force attacks. 3DES uses 168 bit keys (56x3), and performs the encryption like so:

  1. Encrypt clear text using the 1st 56 bits of the key, produce output1
  2. Decrypt output1 using the 2nd 56 bits of the key, produce output2
  3. Encrypt output2 using the 3rd 56 bits of they, produce encrypted text.

Her's a reference: http://en.wikipedia.org/wiki/Triple_DES

OTHER TIPS

3DES does perform DES 3 times, but with three different keys. This algorithm was designed to get around DES's inherent lack of security associated with the original key size.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top