java - 是3DES = DES 做3次吗?

标签 java android des 3des

我根据mkyong's JCE Encryption – Data Encryption Standard (DES) Tutorial做了一个DES Util类

这是我的类(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;
  }
}

正如 WiKi 所说:

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

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

感谢您的建议,抱歉我的英语不好~

最佳答案

DES 使用短的 56 位 key ,容易受到暴力攻击。 3DES 使用 168 位 key (56x3),并像这样执行加密:

  1. 使用 key 的前 56 位加密明文,产生输出 1
  2. 解密使用 key 的第2个56位输出1,产生输出2
  3. 使用它们的第 3 个 56 位对 output2 进行加密,生成加密文本。

她是一个引用: http://en.wikipedia.org/wiki/Triple_DES

关于java - 是3DES = DES 做3次吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19803505/

相关文章:

java - 使用 Spring Data Embedded Mongo 在 Mongo 数据库中导入 JSON 文件

java - PostgreSQL + Hibernate + C3P0 = FATAL : sorry, 已经有太多客户端

java - 选择线程执行屏障操作 - Java CyclicBarrier

java - 切换主题时如何考虑变化?

Android:调用相机 Intent 后 Activity 被破坏

java - 规范是否保证对顺序 Java 流的操作必须保留在当前线程中?

java - 使用php将base64图像从android上传到服务器时出错

java - C/C++ 与 Java 中的 DES 加密

python - python 中的异或位

php - Python 相当于 PHP Mcrypt