mkyong の 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 は Triple Data Encryption Algorithm (TDEA または Triple DEA) ブロック暗号の一般名であり、Data Encryption Standard (DES) 暗号アルゴリズムを各データ ブロックに 3 回適用します。
文字列を DES で 3 回暗号化するとどうなるか疑問に思っています... 3DES と同じでしょうか?
提案をありがとう、そして私の下手な英語でごめんなさい〜