08/07/10 00:58:15
続きです
暗号化/復号化はこんな感じです。
this.passwordはコンストラクタで初期化されています。
public String encrypt(String plain) {
byte[] bytes = plain.getBytes();
SecretKey secretKey = generateSecretKey(this.password); // XXX 毎回作るの?
Cipher cipher = Cipher.getInstance("DES");
cipher.init(Cipher.ENCRYPT_MODE, secretKey);
return encode(cipher.doFinal(bytes)); // Cookieに保存するためBase64でエンコードして戻す
}
public String decrypt(String crypted) {
byte[] bytes = decode(crypted); // さっきのBase64をデコードする
SecretKey secretKey = generateSecretKey(this.password); // XXX synchronizedにすれば毎回作る必要はないけどね…
Cipher cipher = Cipher.getInstance("DES");
chiper.init(Cipher.DECRYPT_MODE, secretKey);
return new String(chiper.doFinal(bytes));
}
Cipherはともかく、変更されない鍵までリクエストのたびに作るor同期化するっていうのは
イマイチ感が漂うのですが…
そもそもインスタンスが不変であれば同期は要らない気もしています。
どちらにせよ言及している文書が探しきれていないので、「ここ読め」とかでも結構です。
よろしくお願いします。