我根据mkyong的JCE加密-数据加密标准(DES)教程做了一个DES类
这是我的课:
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或Triple )分组密码的通用名称,对每个数据块三次应用数据加密标准(DES)密码算法。
我只是在想,如果我用DES为3 times...Will加密一个字符串,它等于3 DES呢?
谢谢你的建议,也很抱歉我的英语不好~
发布于 2013-11-06 03:35:40
DES使用一个短的56位密钥,这是很容易受到暴力攻击。3 3DES使用168位密钥(56x3),并执行如下加密:
她是个推荐人:DES
发布于 2013-11-06 03:22:52
3DES确实执行了3次DES,但使用了三个不同的键。该算法旨在解决DES固有的与原始密钥大小相关的安全性不足问题。
https://stackoverflow.com/questions/19803505
复制相似问题