トリプルDES 暗号化・複合化のために Abstract クラスを作ってみた。
import java.security.Key;
import java.security.spec.AlgorithmParameterSpec;
import javax.crypto.Cipher;
import javax.crypto.spec.IvParameterSpec;
import javax.crypto.spec.SecretKeySpec;
/**
* DESede/CBC/PKCS5Padding 暗号化、プロバイダは、SunJCE
*/
public abstract class AbstractDESedeCipher{
/** 24byte の鍵 */
public abstract byte getKey();
/** 初期化ベクトル 8byte */
public abstract byte getInitVector();
private Key skey;
private AlgorithmParameterSpec ivParamSpec;
public AbstractDESedeCipher(){
byte keybyte = this.getKey();
if (keybyte.length != 24){
throw new RuntimeException("getKey() return length must be 24byte.");
}
this.skey = new SecretKeySpec(keybyte,"DESede");
byte vector = this.getInitVector();
if (vector.length != 8){
throw new RuntimeException("getInitVector() return length must be 8byte.");
}
this.ivParamSpec = new IvParameterSpec(vector);
}
/**
* 暗号化
* @param text クリアテキスト
* @return 暗号化された文字列 byte
*/
public final byte encrypt(String text){
try{
Cipher cipher = Cipher.getInstance("DESede/CBC/PKCS5Padding","SunJCE");
cipher.init(Cipher.ENCRYPT_MODE,this.skey,this.ivParamSpec);
byte iv = cipher.getIV();
byte enc = cipher.doFinal(text.getBytes());
byte bs = new byte[iv.length + enc.length];
System.arraycopy(iv,0,bs,0,iv.length);
System.arraycopy(enc,0,bs,iv.length,enc.length);
return bs;
}catch (Exception e){
throw new RuntimeException(e);
}
}
/**
* 複合化
* @param data 暗号化されたデータ
* @return 複合した文字列
*/
public final String decrypt(byte data){
try {
Cipher cipher = Cipher.getInstance("DESede/CBC/PKCS5Padding","SunJCE");
cipher.init(Cipher.DECRYPT_MODE,this.skey,this.ivParamSpec);
int blocksize = cipher.getBlockSize();
return new String(cipher.doFinal(data,blocksize,data.length - blocksize));
}catch(Exception e){
throw new RuntimeException(e);
}
}
/**
* アルゴリズム javax.crypto.spec.SecretKeySpec キー
* @return java.security.Key
*/
public Key getSecretKeySpec(){
return this.skey;
}
/**
* @return AlgorithmParameterSpec
*/
public AlgorithmParameterSpec getAlgorithmParameterSpec(){
return this.ivParamSpec;
}
}
これを継承して、
public class MyDESedeCiper extends AbstractDESedeCipher{
@Override
public byte getInitVector(){
return new byte{
(byte)0x12,(byte)0x53,(byte)0x2a,(byte)0xfc
,(byte)0x2e,(byte)0xc8,(byte)0x76,(byte)0x6d
};
}
@Override
public byte getKey(){
return new byte{
(byte)0xF8,(byte)0x75,(byte)0x09,(byte)0x13
,(byte)0x86,(byte)0xA4,(byte)0xC1,(byte)0x26
,(byte)0x50,
省略!!24 バイト用意する。<
};
}
}
テストプログラム、
MyDESedeCiper cipher = new MyDESedeCiper();
String target = "abcd 1234 _!@/#$% ~あいうABC";
System.out.println(target);
byte[] encdata = cipher.encrypt(target);
// Base64 は、import org.apache.commons.codec.binary.Base64;
String encstr = new String(Base64.encodeBase64(encdata));
System.out.println("\n暗号化!!");
System.out.println(encstr);
System.out.println("length = "+encdata.length);
String decString = cipher.decrypt(encdata);
System.out.println("\n複合化!!");
String decString = cipher.decrypt(Base64.decodeBase64(encstr.getBytes()));
System.out.println(target.equals(decString) ? "\nOK" : "\nNG");
実行結果は、
abcd 1234 _!@/#$% ~あいうABC
暗号化!!
ElMq/C7Idm00ntgCmDT9nbBOQFpvXhrtIkFHdzAImqXRWesmiBqIaOZjBu+F1eob
length = 48
複合化!!
abcd 1234 _!@/#$% ~あいうABC
OK