DESede , AES と試したので、こんどはパスワードによる SHA1 and トリプルDES 暗号化
import java.util.Arrays;
import javax.crypto.Cipher;
import javax.crypto.SecretKey;
import javax.crypto.SecretKeyFactory;
import javax.crypto.spec.PBEKeySpec;
import javax.crypto.spec.PBEParameterSpec;
/**
* パスワードによる SHA1 and トリプルDES 暗号化
*/
public abstract class AbstractPBECipher{
/** ソルト値を決定 8byte 必要 */
public abstract byte getSaltValue();
/** 暗号化パスワード */
public abstract String getPassword();
private int iteratCount = 1024;
private String algorithm = "PBEWithSHA1AndDESede";
/**
* 暗号化
* @param text クリアテキスト
* @return 暗号化された文字列 byte
*/
public final byte encrypt(String text){
PBEParameterSpec pbeParamSpec = new PBEParameterSpec(this.getSaltValue(),this.iteratCount);
char password = this.getPassword().toCharArray();
PBEKeySpec pbeKeySpec = new PBEKeySpec(password);
Arrays.fill(password, (char)0x00);
try{
SecretKeyFactory keyFac = SecretKeyFactory.getInstance(this.algorithm);
SecretKey pbeKey = keyFac.generateSecret(pbeKeySpec);
Cipher pbeCipher = Cipher.getInstance(this.algorithm);
pbeCipher.init(Cipher.ENCRYPT_MODE, pbeKey, pbeParamSpec);
return pbeCipher.doFinal(text.getBytes());
}catch (Exception e){
throw new RuntimeException("AbstractPBECipher.encrypto Exception : "+e.getMessage(),e);
}
}
/**
* 複合化
* @param data 暗号化されたデータ
* @return 複合した文字列
*/
public final String decrypt(byte data){
PBEParameterSpec pbeParamSpecDec = new PBEParameterSpec(this.getSaltValue(),this.iteratCount);
char password = this.getPassword().toCharArray();
PBEKeySpec pbeKeySpec = new PBEKeySpec(password);
Arrays.fill(password, (char)0x00);
try{
SecretKeyFactory keyFac = SecretKeyFactory.getInstance(this.algorithm);
SecretKey pbeKey = keyFac.generateSecret(pbeKeySpec);
Cipher pbeCipher = Cipher.getInstance(this.algorithm);
pbeCipher.init(Cipher.DECRYPT_MODE,pbeKey,pbeParamSpecDec);
return new String(pbeCipher.doFinal(data));
}catch (Exception e){
throw new RuntimeException("AbstractPBECipher.decrypto Exception : "+e.getMessage(),e);
}
}
/**
* @return イテレーションカウント値
*/
public int getIteratCount(){
return this.iteratCount;
}
/**
* @return 暗号アルゴリズム名
*/
public String getAlgorithm(){
return this.algorithm;
}
}
これを継承して、
public class MyPBECipher extends AbstractPBECipher{
private String password;
@Override
public String getPassword(){
return this.password;
}
@Override
public byte getSaltValue(){
return new byte{(byte)0x12,(byte)0x53,(byte)0x2a
,(byte)0xfc,(byte)0x2e,(byte)0xc8,(byte)0x76,(byte)0x6d};
}
public void setPassword(String password){
this.password = password;
}
}
テストプログラム、
MyPBECipher cipher = new MyPBECipher();
String target = "abcd 1234 _!@/#$% ~あいうABC";
System.out.println(target);
cipher.setPassword("uranus!7z");
byte[] encdata = cipher.encrypt(target);
 // Base64 は、 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(Base64.decodeBase64(encstr.getBytes()));
System.out.println("\n複合化!!");
System.out.println(decString);
System.out.println(target.equals(decString) ? "\nOK" : "\nNG");
実行結果は、
abcd 1234 _!@/#$% ~あいうABC
暗号化!!
TvAd5ZCv3X0743Sp0bdbIfRUzHTNs83f5jD+3PAULygCFjVjpWr2uQ==
length = 40
複合化!!
abcd 1234 _!@/#$% ~あいうABC
OK