package jp.co.techfirm.ssec.batch.util;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.math.BigDecimal;
import java.net.URL;
import java.sql.Timestamp;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.CellStyle;
import org.apache.poi.ss.usermodel.CreationHelper;
import org.apache.poi.ss.usermodel.RichTextString;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Workbook;
import org.apache.poi.ss.usermodel.WorkbookFactory;
/**
* xlsファイル作成編集アクセサ<br/>
* POI-SS系のメソッドをラップしてアクセスしやすい形にしてみました。
* ただし、複雑な処理は出来ないよ。Fomulaとか思いっきり無視してるし。
* @author y-kumada
*/
public class PoiAccessor {
public static enum Type{SHEET, ROW, CELL};
protected static final Log LOG = LogFactory.getLog(PoiAccessor.class);
private final String __def_format = "$name$";
// カレントワークブック
private Workbook wb;
// カレントシート
private Sheet sheet;
// 作成ヘルパ
private CreationHelper helper;
// フォーマットMap
private Map<String, Integer> format;
/**
* コンストラクタ。
* 新規にWorkbookを作成する。
*/
public PoiAccessor(){
info();
wb = null;
if(!create()){
throw new IllegalStateException();
}
}
/**
* コンストラクタ。
* 指定されたWorkbookを読み込む。
* @param in 読み込む対象ファイルのFileInputStream
*/
public PoiAccessor(FileInputStream in){
info();
wb = null;
if(!load(in)){
throw new IllegalStateException();
}
}
/**
* Cellの表示フォーマットを定義する
* @param key 以降フォーマットを参照する為のキー文字列
* @param value 定義するフォーマット文字列
*/
public void defineFormat(String key, String value){
Integer val = format.get(key);
if(val == null){
if(helper == null){
throw new IllegalStateException();
}
// format追加
short fmt = helper.createDataFormat().getFormat(value);
format.put(key, new Integer(fmt));
LOG.debug("poi.defineFormat("+key+", "+value+")");
}
}
/**
* Cellのデフォルト表示フォーマットを定義する<br/>
* 指定したクラス型の値をsetした場合に、defualtの表示フォーマットを使用する。
* @param clazz クラス型
* @param value 定義するフォーマット文字列
*/
public void defualtFormat(Class<?> clazz, String value){
String key = new String(__def_format).replace("name", clazz.getName());;
defineFormat(key, value);
}
/**
* 指定したWoorkbookを読み込む<br/>
* 通常はコンストラクタで読めば良いので使用しない
* @param in 読み込む対象ファイルのFileInputStream
* @return 読込が正常に終了した場合 true を返す。
*/
public boolean load(FileInputStream in){
try{
if(wb != null){
LOG.warn("workbook is already exist in this instance.");
}
wb = WorkbookFactory.create(in);
helper = wb.getCreationHelper();
// 初期値
sheet = wb.getSheetAt(0);
format = new HashMap<String, Integer>();
}catch (Exception e) {
return false;
}
return true;
}
/**
* Woorkbookを作成する<br/>
* 通常はコンストラクタで作成すれば良いので使用しない
* @return 作成が正常に終了した場合 true を返す。
*/
public boolean create(){
try{
if(wb != null){
LOG.warn("workbook is already exist in this instance.");
}
wb = new HSSFWorkbook();
helper = wb.getCreationHelper();
sheet = wb.createSheet();
format = new HashMap<String, Integer>();
}catch (Exception e) {
return false;
}
return true;
}
/**
* Woorkbookを保存する<br/>
* @param filename 保存するファイル名
* @return 作成が正常に終了した場合 true を返す。
*/
public boolean save(String filename) throws IllegalStateException {
checkNull(wb);
try{
FileOutputStream out = new FileOutputStream(filename);
wb.write(out);
}
catch (Exception e) {
return false;
}
return true;
}
/**
* Sheetを選択する<br/>
* @param name シート名
*/
public void sheet(String name) throws Exception{
checkNull(wb);
sheet = wb.getSheet(name);
if(sheet == null){
throw new IllegalArgumentException();
}
}
/**
* Sheetを選択する<br/>
* @param name シート番号(0~)
*/
public void sheet(int index) throws IllegalStateException{
checkNull(wb);
sheet = wb.getSheetAt(index);
}
/**
* 選択中のSheet番号を返却する<br/>
*/
public int sheet() throws IllegalStateException{
checkNull(wb);
return wb.getSheetIndex(sheet);
}
/**
* 対象のCellに値を設定する<br/>
* @param target excelのセル指定文字列(A10とか、C5とか。※$には未対応)
* @param value 設定値
*/
public void set(String target, Object value){
set(target, value, null);
}
/**
* 対象のCellに値を設定する(表示フォーマットも指定する)<br/>
* @param target excelのセル指定文字列(A10とか、C5とか。※$には未対応)
* @param value 設定値
* @param formatKey フォーマット識別子(defineFormatで指定したkey文字列<br/>
* ※ 一応、フォーマット文字列を直接指定した場合も適用されます)
*/
public void set(String target, Object value, String formatKey){
int n[] = getRowCell(target);
set(n[0], n[1], value, formatKey);
}
/**
* 対象のCellに値を設定する<br/>
* @param row 行番号(0~)
* @param col 列番号(0~)
* @param value 設定値
*/
public void set(int row, int col, Object value){
set(row, col, value, null);
}
/**
* 対象のCellに値を設定する(表示フォーマットも指定する)<br/>
* @param row 行番号(0~)
* @param col 列番号(0~)
* @param value 設定値
* @param formatKey フォーマット識別子(defineFormatで指定したkey文字列<br/>
* ※ 一応、フォーマット文字列を直接指定した場合も適用されます)
*/
public void set(int row, int col, Object value, String formatKey) throws IllegalStateException {
checkNull(sheet);
Row r = sheet.getRow(row);
Cell c;
if(r == null){
r=sheet.createRow(row);
}
c = r.getCell(col);
if (c == null) {
c = r.createCell(col);
}
if (value == null) {
c.setCellValue("");
return;
}
if (value instanceof Boolean) {
c.setCellValue((Boolean)value);
}
else if (value instanceof Calendar) {
c.setCellValue((Calendar)value);
}
else if (value instanceof Timestamp ) {
Calendar cal = Calendar.getInstance();
cal.setTimeInMillis(((Timestamp)value).getTime());
c.setCellValue(cal.getTime());
}
else if (value instanceof Date) {
c.setCellValue((Date)value);
}
else if (value instanceof Double) {
c.setCellValue((Double)value);
}
else if (value instanceof Integer) {
c.setCellValue(((Integer)value).doubleValue());
}
else if (value instanceof BigDecimal) {
c.setCellValue(((BigDecimal)value).doubleValue());
}
else if (value instanceof RichTextString) {
c.setCellValue((RichTextString)value);
}
else if (value instanceof String) {
c.setCellValue((String)value);
}
else if(value.equals(true) || value.equals(false)){
c.setCellValue((Boolean)value);
}
else{
LOG.error("unmatch type: " + value.getClass().getName());
}
if(true){
// defualt format matches
Integer fmt = format.get(new String(__def_format).replace("name", value.getClass().getName()));
if(fmt != null){
c.getCellStyle().setDataFormat(fmt.shortValue());
}
}
if(formatKey != null){
Integer fmt = format.get(formatKey);
if(fmt == null){
defineFormat(formatKey, formatKey);
fmt = format.get(formatKey);
}
c.getCellStyle().setDataFormat(fmt.shortValue());
}
}
/**
* 対象のCellの値を取得する<br/>
* @param target excelのセル指定文字列(A10とか、C5とか。※$には未対応)
* @return 対象セルの値
*/
public Object get(String target){
int n[] = getRowCell(target);
return get(n[0], n[1]);
}
/**
* 対象のCellの値を取得する<br/>
* @param row 行番号(0~)
* @param cell 列番号(0~)
* @return 対象セルの値
*/
public Object get(int row, int cell){
Object value = null;
Cell c = sheet.getRow(row).getCell(cell);
// 2011.08.24 yonaha add start
if(c == null){
c = sheet.getRow(row).createCell(cell);
}
// 2011.08.24 yonaha add end
switch(c.getCellType()){
case Cell.CELL_TYPE_BLANK:
// 2011.08.24 yonaha change start
// return null;
return new String("");
// 2011.08.24 yonaha change end
case Cell.CELL_TYPE_BOOLEAN:
return new Boolean(c.getBooleanCellValue());
case Cell.CELL_TYPE_FORMULA:
return c.getCellFormula();
case Cell.CELL_TYPE_NUMERIC:
return c.getNumericCellValue();
case Cell.CELL_TYPE_STRING:
return c.getStringCellValue();
}
return value;
}
/**
* 選択シートの最終列番号を取得(1始まり)
*
* @return 行数
*/
public int getLastRow(){
// 最後の行を取得する(0始まり)
int last = sheet.getLastRowNum();
// 0始まりなので+1して返す
return last+1;
}
/**
* 空白セルをチェックする
*
* @return チェック結果
*/
public boolean isSpaceCell(){
boolean result = true;
// データが入っている行数(空白行は含まない)
int phyRowNum = sheet.getPhysicalNumberOfRows();
// データの入っている最終列番号(空白行を含む)
int lastRowNum = getLastRow();
// 最終列番号とデータが入っている行数が一致しない場合はNG
if(phyRowNum != lastRowNum){
result = false;
}
// 行ごとの空白セルのチェック
for(int i = 0; i < lastRowNum; i++){
result = isSpaceCellOfRow(i);
if(result == false){
break;
}
}
return result;
}
/**
* 1行中の空白セルをチェック
*
* @param rowNum 行番号(0~)
* @return チェック結果
*/
public boolean isSpaceCellOfRow(int rowNum){
boolean result = true;
Row row = sheet.getRow(rowNum);
// データが入っているセル列数(空白列は含まない)
int phyCellNum = row.getPhysicalNumberOfCells();
// データが入っている最終セル列番号(空白列含む)
int lastCellNum = row.getLastCellNum();
//最終セル列数とデータが入っているセル列数が一致しない場合はNG
if(phyCellNum != lastCellNum){
result = false;
}
return result;
}
/**
* 対象行のCellの値を取得する
*
* @param rowNum 読み込み行番号
* @return エクセル1行分のデータリスト
*/
public List<Object> getRow(int rowNum){
int lastCellNum = sheet.getRow(rowNum).getLastCellNum();
return getRow(rowNum,lastCellNum);
}
/**
* 対象行のCellの値を取得する
*
* @param rowNum 読み込み行番号
* @raram readColNum 読み込み列数
* @return エクセル1行分のデータリスト
*/
public List<Object> getRow(int rowNum, int readColNum){
List<Object> valueList = new ArrayList<Object>();
// データが入っている最終列番号(空白列含む)
if(sheet.getRow(rowNum)==null){
sheet.createRow(rowNum);
}
// 1行分のデータを取得する
for(int i = 0; i < readColNum; i++ ){
Object values = get(rowNum, i);
valueList.add(values);
}
return valueList;
}
// excelセル指定文字列をrowとcellに解析する(A1~)
private int[] getRowCell(String target){
int n = 0;
for(int i = 0; i < target.length(); i++){
int c = target.charAt(i);
if('0' <= c && c <= '9'){ n = i; break; }
}
if(n == 0){
throw new IllegalArgumentException();
}
String cellString = target.substring(0, n).toUpperCase();
int row = 0;
try{
row = Integer.parseInt(target.substring(n)) - 1;
if(cellString.length() == 0 || 2 < cellString.length() || row < 0){
throw new IllegalArgumentException();
}
}catch (Exception e) {
throw new IllegalArgumentException();
}
int cell = 0;
int x = 'Z' - 'A';
for(int i = cellString.length() - 1; 0 <= i; i--){
cell += ((cellString.charAt(i)) - 'A' + i) * ((i != 0) ? x + 1 : 1);
}
int[] ret = {row, cell};
return ret;
}
// NullExceptionをIllegalStateExceptionにすり替える
private void checkNull(Object obj) throws IllegalStateException{
if(obj == null){
throw new IllegalStateException();
}
}
// POIの読込先ディレクトリを表示する(クラスパス指定不正防止)
private void info(){
ClassLoader classloader = org.apache.poi.poifs.filesystem.POIFSFileSystem.class.getClassLoader();
URL res = classloader.getResource("org/apache/poi/poifs/filesystem/POIFSFileSystem.class");
String path = res.getPath();
LOG.info("POI : " + path);
}
/**
* セルの領域が確保されていない場合に、新規に領域を確保する<br/>
* @param row 行番号(0~)
* @param cell 列番号(0~)
* @return 対象セルの値
*/
public void createCell(int row, int col){
checkNull(sheet);
Row r = sheet.getRow(row);
if(r == null){
r = sheet.createRow(row);
}
Cell c = r.getCell(col);
if(c == null){
r.createCell(col);
}
}
/**
* 対象のCellに罫線を引く<br/>
* @param row 行番号(0~)
* @param cell 列番号(0~)
* @return 対象セルの値
*/
public void setBorder(int row, int col, short borderStyle){
checkNull(sheet);
CellStyle style = wb.createCellStyle();
style.setBorderTop(borderStyle);
style.setBorderBottom(borderStyle);
style.setBorderLeft(borderStyle);
style.setBorderRight(borderStyle);
Cell c = sheet.getRow(row).getCell(col);
c.setCellStyle(style);
}
}
/**
* Dtoのリスト情報をエクセルファイルへ出力する。
*
* @param objectDtoList 出力用Dto
* @param encoding ファイルエンコード
* @param templeateFilePath エクセルテンプレートのパスとファイル名
* @param outputFilePath 出力ファイルのパスと名称
* @param rowNum 出力開始行位置
* @param colNum 出力開始列位置
* @throws BatchException
*/
public void outPutExcelFile(List<?> objectDtoList,
String templeateFilePath,String outputFilePath,
int rowNum, int colNum)
throws BatchException {
int firstRow;
// テンプレートの取得
File file = new File(templeateFilePath);
FileInputStream fileIn = null;
// テンプレートファイルの存在確認
if(!file.exists()){
LinkedHashMap<String,String> addInfo = new LinkedHashMap<String,String>();
addInfo.put("templeateFilePath", templeateFilePath);
logger.error(LogUtil.getMessage("common.FileReadFailure",addInfo));
throw new BatchException("common.FileReadFailure");
}
try {
// 読み込み許可
file.setReadable(true);
// 書き込み許可
file.setWritable(true);
// ファイルオープン
fileIn = new FileInputStream(file);
PoiAccessor poi = new PoiAccessor();
if(!poi.load(fileIn)){
LinkedHashMap<String,String> addInfo = new LinkedHashMap<String,String>();
addInfo.put("templeateFilePath", templeateFilePath);
logger.error(LogUtil.getMessage("common.FileReadFailure"));
throw new BatchException("");
}
// ファイル書き込み
firstRow = rowNum;
for(int i=0; i < objectDtoList.size(); i++ ){
Object objectDto = objectDtoList.get(i);
setCellValue(objectDto,poi,firstRow,colNum);
firstRow++;
}
// 保存先を指定して保存
poi.save(outputFilePath);
} catch (Exception e) {
LinkedHashMap<String,String> addInfo = new LinkedHashMap<String,String>();
addInfo.put("templeateFilePath", templeateFilePath);
logger.error(LogUtil.getMessage("common.FileWriteFailure"));
throw new BatchException(e);
}
return;
}
|
---------------------- 单维数组 ------------------------
DECLARE
TYPE emp_ssn_array IS TABLE OF NUMBER
INDEX BY BINARY_INTEGER;
best_employees emp_ssn_array;
worst_employees emp_ssn_array;
BEGIN
best_employees(1) := '123456';
best_employees(2) := '888888';
worst_employees(1) := '222222';
worst_employees(2) := '666666';
FOR i IN 1..best_employees.count LOOP
DBMS_OUTPUT.PUT_LINE('i='|| i || ', best_employees= ' ||best_employees(i)
|| ', worst_employees= ' ||worst_employees(i));
END LOOP;
END;
---------------------- 多维数组 ------------------------
DECLARE
TYPE emp_type IS RECORD
( emp_id employee_table.emp_id%TYPE,
emp_name employee_table.emp_name%TYPE,
emp_gender employee_table.emp_gender%TYPE );
TYPE emp_type_array IS TABLE OF
emp_type INDEX BY BINARY_INTEGER;
emp_rec_array emp_type_array;
emp_rec emp_type;
BEGIN
emp_rec.emp_id := 300000000;
emp_rec.emp_name := 'Barbara';
emp_rec.emp_gender := 'Female';
emp_rec_array(1) := emp_rec;
emp_rec.emp_id := 300000008;
emp_rec.emp_name := 'Rick';
emp_rec.emp_gender := 'Male';
emp_rec_array(2) := emp_rec;
FOR i IN 1..emp_rec_array.count LOOP
DBMS_OUTPUT.PUT_LINE('i='||i
||', emp_id ='||emp_rec_array(i).emp_id
||', emp_name ='||emp_rec_array(i).emp_name
||', emp_gender = '||emp_rec_array(i).emp_gender);
END LOOP;
END;
-------------- Result --------------
i=1, emp_id =300000000, emp_name =Barbara, emp_gender = Female
i=2, emp_id =300000008, emp_name =Rick, emp_gender = Male
|
package jp.co.ana.kwin.framework.util;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.security.InvalidKeyException;
import java.security.Key;
import java.security.NoSuchAlgorithmException;
import javax.crypto.Cipher;
import javax.crypto.CipherInputStream;
import javax.crypto.KeyGenerator;
import javax.crypto.NoSuchPaddingException;
import javax.crypto.SecretKey;
import javax.crypto.spec.SecretKeySpec;
public class SecretUtil {
/**
* ファイルを暗号化する
*
* @param srcFile 暗号化したいファイルパース
* @param encryptedFile 暗号化したファイルパース
* @param secretKey 暗号化キー
* @return なし
*/
public static void EncryptFile(String srcFile ,String encryptedFile ,SecretKey secretKey){
Cipher cipher = null;
InputStream is = null;
CipherInputStream cis = null;
FileOutputStream fos = null;
try {
cipher = Cipher.getInstance("AES");
cipher.init(Cipher.ENCRYPT_MODE, secretKey);
is = new FileInputStream(new File(srcFile));
cis = new CipherInputStream(is, cipher);
byte[] buffer = new byte[1024];
int i;
fos = new FileOutputStream(new File(encryptedFile));
while((i = cis.read(buffer)) > 0) {
fos.write(buffer, 0, i);
}
} catch (NoSuchAlgorithmException e) {
e.printStackTrace();
} catch (NoSuchPaddingException e) {
e.printStackTrace();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (InvalidKeyException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}finally{
if (is != null) {
try {
is.close();
} catch (IOException e) {
e.printStackTrace();
}
}
if (cis != null) {
try {
cis.close();
} catch (IOException e) {
e.printStackTrace();
}
}
if (fos != null) {
try {
fos.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
/**
* ファイルを復号化する
*
* @param srcFile 復号化したファイルパース
* @param encryptedFile 復号化したファイルパース
* @param secretKey 暗号化キー
* @return なし
*/
public static void DecryptFile(String srcFile ,String decryptedFile ,SecretKey secretKey){
Cipher cipher = null;
InputStream is = null;
CipherInputStream cis = null;
FileOutputStream fos = null;
try {
cipher = Cipher.getInstance("AES");
cipher.init(Cipher.DECRYPT_MODE, secretKey);
is = new FileInputStream(new File(srcFile));
cis = new CipherInputStream(is, cipher);
byte[] buffer = new byte[1024];
int i;
fos = new FileOutputStream(new File(decryptedFile));
while((i = cis.read(buffer)) > 0) {
fos.write(buffer, 0, i);
}
} catch (NoSuchAlgorithmException e) {
e.printStackTrace();
} catch (NoSuchPaddingException e) {
e.printStackTrace();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (InvalidKeyException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}finally{
if (is != null) {
try {
is.close();
} catch (IOException e) {
e.printStackTrace();
}
}
if (cis != null) {
try {
cis.close();
} catch (IOException e) {
e.printStackTrace();
}
}
if (fos != null) {
try {
fos.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
/**
* 暗号鍵を生成する
*
* @return SecretKey 暗号化キー
*/
public static SecretKey GenerateKey() {
KeyGenerator kg;
try {
kg = KeyGenerator.getInstance("AES");
kg.init(256);
return kg.generateKey();
} catch (NoSuchAlgorithmException e) {
e.printStackTrace();
}
return null;
}
/**
* 暗号鍵をファイルに書き込む
*
* @param path キーファイルのパス
* @return 成功:true
* 失敗:false
*/
public static boolean saveKey(SecretKey key,String path) {
File keyFile = new File(path);
try {
FileOutputStream fos = new FileOutputStream(keyFile);
fos.write(key.getEncoded());
} catch (FileNotFoundException e) {
e.printStackTrace();
return false;
} catch (IOException e) {
e.printStackTrace();
return false;
}
return true;
}
/**
* 暗号鍵をファイルに書き込む
*
* @param path キーファイルのパス
* @return 成功:true
* 失敗:false
*/
public static boolean saveEncode(byte[] encode,String path) {
File keyFile = new File(path);
try {
FileOutputStream fos = new FileOutputStream(keyFile);
fos.write(encode);
} catch (FileNotFoundException e) {
e.printStackTrace();
return false;
} catch (IOException e) {
e.printStackTrace();
return false;
}
return true;
}
/**
* 暗号鍵をファイルから読み込む
*
* @param path キーファイルのパス
* @return 暗号鍵
*/
public static SecretKey readKey(String path) {
SecretKey key = null;
File keyFile = new File(path);
try {
FileInputStream fis = new FileInputStream(keyFile);
byte[] buffer = new byte[16];
fis.read(buffer);
key = new SecretKeySpec(buffer, "AES");
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
return key;
}
public static byte[] readEncode(String path) {
File keyFile = new File(path);
byte[] buffer = new byte[1024];
try {
FileInputStream fis = new FileInputStream(keyFile);
fis.read(buffer);
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
return buffer;
}
/**
* 平文を暗号化する。
*
* @param byteS 平文
* @param key 暗号鍵
* @return 暗号文
*/
public static byte[] getEncCode(byte[] byteS, Key key){
byte[] byteFina = null;
Cipher cipher;
try{
cipher = Cipher.getInstance("AES");
cipher.init(Cipher.ENCRYPT_MODE, key);
byteFina = cipher.doFinal(byteS);
} catch (Exception e) {
e.printStackTrace();
} finally {
cipher = null;
}
return byteFina;
}
/**
* 暗号文を平文にする。
*
* @param byteD 暗号文
* @param key 暗号鍵
* @return 平文
*/
public static byte[] getDesCode(byte[] byteD, Key key)
{
Cipher cipher;
byte[] byteFina = null;
try{
cipher = Cipher.getInstance("AES");
cipher.init(Cipher.DECRYPT_MODE, key);
byteFina = cipher.doFinal(byteD, 0, 16);
} catch (Exception e) {
e.printStackTrace();
} finally {
cipher = null;
}
return byteFina;
}
}
package jp.co.ana.kwin.framework.util;
import javax.crypto.SecretKey;
public class SecretUtilTest {
public static void main(String[] args) throws Exception {
// 暗号化キーを保存するファイルパース
String SECRET_KEY_PATH = "C:\\secret.key";
// 暗号化したいファイルパース
String SRC_FILE_PATH = "C:\\Staff.xml";
// 暗号化したファイルパース
String ENCRYPTED_FILE_PATH = "C:\\Staff_encrypted.xml";
// 復号化したファイルパース
String DECRYPTED_FILE_PATH = "C:\\Staff_decrypted.xml";
// 暗号化キーをファイルに保存する
SecretUtil.saveKey(SecretUtil.GenerateKey(), SECRET_KEY_PATH);
// 暗号化キーをファイルから取得する
SecretKey secretKey = SecretUtil.readKey(SECRET_KEY_PATH);
// ファイルを暗号化する
SecretUtil.EncryptFile(SRC_FILE_PATH, ENCRYPTED_FILE_PATH, secretKey);
// ファイルを復号化する
SecretUtil.DecryptFile(ENCRYPTED_FILE_PATH, DECRYPTED_FILE_PATH, secretKey);
}
}
|