3DES = DES会做3次吗?

时间:2013-11-06 03:21:55

标签: java android des 3des

我根据mkyong's JCE Encryption – Data Encryption Standard (DES) Tutorial

完成了DES Util课程

这是我的班级:

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;
  }
}

正如WiKi所说:

在密码学中,Triple DES是三重数据加密算法(TDEA或三重DEA)分组密码的通用名称,对每个数据块应用数据加密标准(DES)密码算法三次

我只是想知道如果我用DES加密一个字符串3次...它会等于3DES吗?

感谢您的建议,对不起我的英语很差〜

2 个答案:

答案 0 :(得分:4)

DES使用短56位密钥,容易受到强力攻击。 3DES使用168位密钥(56x3),并执行加密,如下所示:

  1. 使用密钥的前56位加密明文,生成output1
  2. 使用密钥的第二个56位
  3. 解密 output1,生成output2
  4. 使用它们的第3个56位加密output2,生成加密文本。
  5. 她是一个参考: http://en.wikipedia.org/wiki/Triple_DES

答案 1 :(得分:3)

3DES执行DES 3次,但有三个不同的密钥。该算法旨在解决DES与原始密钥大小相关的固有缺乏安全性的问题。

相关问题