AES暗号化して、Base64で文字列化する場合の話
キー
public static final String ENCRYPT_KEY = "1234567890123456";
public static final String ENCRYPT_IV = "abcdefghijklmnop";
暗号
/**
* 暗号化メソッド
*
* @param text 暗号化する文字列
* @return 暗号化文字列
*/
public static String encrypt(String text) {
// 変数初期化
String strResult = null;
try {
// 文字列をバイト配列へ変換
byte[] byteText = text.getBytes("UTF-8");
// 暗号化キーと初期化ベクトルをバイト配列へ変換
byte[] byteKey = ENCRYPT_KEY.getBytes("UTF-8");
byte[] byteIv = ENCRYPT_IV.getBytes("UTF-8");
// 暗号化キーと初期化ベクトルのオブジェクト生成
SecretKeySpec key = new SecretKeySpec(byteKey, "AES");
IvParameterSpec iv = new IvParameterSpec(byteIv);
// Cipherオブジェクト生成
Cipher cipher = Cipher.getInstance("AES/CBC/PKCS7Padding");
// Cipherオブジェクトの初期化
cipher.init(Cipher.ENCRYPT_MODE, key, iv);
// 暗号化の結果格納
byte[] byteResult = cipher.doFinal(byteText);
// Base64へエンコード
strResult = Base64.encodeBase64String(byteResult);
} catch (Exception e) {
e.printStackTrace();
}
// 暗号化文字列を返却
return strResult;
}
復号
/**
* 復号化メソッド
*
* @param text 復号化する文字列
* @return 復号化文字列
*/
public static String decrypt(String text) {
// 変数初期化
String strResult = null;
try {
// Base64をデコード
byte[] byteText = Base64.decodeBase64(text);
// 暗号化キーと初期化ベクトルをバイト配列へ変換
byte[] byteKey = ENCRYPT_KEY.getBytes("UTF-8");
byte[] byteIv = ENCRYPT_IV.getBytes("UTF-8");
// 復号化キーと初期化ベクトルのオブジェクト生成
SecretKeySpec key = new SecretKeySpec(byteKey, "AES");
IvParameterSpec iv = new IvParameterSpec(byteIv);
// Cipherオブジェクト生成
Cipher cipher = Cipher.getInstance("AES/CBC/PKCS5Padding");
// Cipherオブジェクトの初期化
cipher.init(Cipher.DECRYPT_MODE, key, iv);
// 復号化の結果格納
byte[] byteResult = cipher.doFinal(byteText);
// バイト配列を文字列へ変換
strResult = new String(byteResult, "UTF-8");
} catch (Exception e) {
e.printStackTrace();
}
// 復号化文字列を返却
return strResult;
}
最終更新:2014年07月22日 17:05