245 lines
7.8 KiB
Java
245 lines
7.8 KiB
Java
package seed.utils;
|
|
|
|
import java.awt.AlphaComposite;
|
|
import java.awt.Graphics2D;
|
|
import java.awt.Image;
|
|
import java.awt.image.BufferedImage;
|
|
import java.io.File;
|
|
import java.io.FileInputStream;
|
|
import java.io.FileNotFoundException;
|
|
import java.io.FileOutputStream;
|
|
import java.io.IOException;
|
|
import java.security.InvalidAlgorithmParameterException;
|
|
import java.security.InvalidKeyException;
|
|
import java.security.NoSuchAlgorithmException;
|
|
import java.security.spec.InvalidKeySpecException;
|
|
import java.util.Random;
|
|
|
|
import javax.crypto.BadPaddingException;
|
|
import javax.crypto.Cipher;
|
|
import javax.crypto.IllegalBlockSizeException;
|
|
import javax.crypto.NoSuchPaddingException;
|
|
import javax.crypto.SecretKey;
|
|
import javax.crypto.SecretKeyFactory;
|
|
import javax.crypto.spec.PBEKeySpec;
|
|
import javax.crypto.spec.PBEParameterSpec;
|
|
import javax.imageio.ImageIO;
|
|
|
|
import org.apache.log4j.Logger;
|
|
|
|
public class SeedFileUtils {
|
|
|
|
private static Logger log = Logger.getLogger(SeedFileUtils.class);
|
|
|
|
private static final String password = "seedFile";
|
|
|
|
public static void setSeedEncryptFile(String infile) {
|
|
|
|
FileInputStream inFile = null;
|
|
FileOutputStream outFile = null;
|
|
|
|
try{
|
|
|
|
inFile = new FileInputStream(infile);
|
|
outFile = new FileOutputStream(infile + ".des");
|
|
|
|
// Use PBEKeySpec to create a key based on a password.
|
|
// The password is passed as a character array
|
|
|
|
PBEKeySpec keySpec = new PBEKeySpec(password.toCharArray());
|
|
SecretKeyFactory keyFactory =
|
|
SecretKeyFactory.getInstance("PBEWithMD5AndDES");
|
|
SecretKey passwordKey = keyFactory.generateSecret(keySpec);
|
|
|
|
// PBE = hashing + symmetric encryption. A 64 bit random
|
|
// number (the salt) is added to the password and hashed
|
|
// using a Message Digest Algorithm (MD5 in this example.).
|
|
// The number of times the password is hashed is determined
|
|
// by the interation count. Adding a random number and
|
|
// hashing multiple times enlarges the key space.
|
|
|
|
byte[] salt = new byte[8];
|
|
Random rnd = new Random();
|
|
rnd.nextBytes(salt);
|
|
int iterations = 100;
|
|
|
|
//Create the parameter spec for this salt and interation count
|
|
|
|
PBEParameterSpec parameterSpec = new PBEParameterSpec(salt, iterations);
|
|
|
|
// Create the cipher and initialize it for encryption.
|
|
|
|
Cipher cipher = Cipher.getInstance("PBEWithMD5AndDES");
|
|
cipher.init(Cipher.ENCRYPT_MODE, passwordKey, parameterSpec);
|
|
|
|
// Need to write the salt to the (encrypted) file. The
|
|
// salt is needed when reconstructing the key for decryption.
|
|
|
|
outFile.write(salt);
|
|
|
|
// Read the file and encrypt its bytes.
|
|
|
|
byte[] input = new byte[64];
|
|
int bytesRead;
|
|
while ((bytesRead = inFile.read(input)) != -1)
|
|
{
|
|
byte[] output = cipher.update(input, 0, bytesRead);
|
|
if (output != null) outFile.write(output);
|
|
}
|
|
|
|
byte[] output = cipher.doFinal();
|
|
if (output != null) outFile.write(output);
|
|
|
|
inFile.close();
|
|
outFile.flush();
|
|
outFile.close();
|
|
|
|
} catch (InvalidKeySpecException e) {
|
|
log.error("CHECK ERROR:",e);
|
|
} catch (IllegalBlockSizeException e) {
|
|
log.error("CHECK ERROR:",e);
|
|
} catch (BadPaddingException e) {
|
|
log.error("CHECK ERROR:",e);
|
|
} catch (NoSuchAlgorithmException e) {
|
|
log.error("CHECK ERROR:",e);
|
|
} catch (NoSuchPaddingException e) {
|
|
log.error("CHECK ERROR:",e);
|
|
} catch (InvalidKeyException e) {
|
|
log.error("CHECK ERROR:",e);
|
|
} catch (InvalidAlgorithmParameterException e) {
|
|
log.error("CHECK ERROR:",e);
|
|
} catch(FileNotFoundException e){
|
|
log.error("CHECK ERROR:",e);
|
|
} catch(IOException e){
|
|
log.error("CHECK ERROR:",e);
|
|
} finally{
|
|
if(inFile!=null){try{inFile.close();}catch(IOException e){log.error("CHECK ERROR:",e);}}
|
|
if(outFile!=null){try{outFile.close();}catch(IOException e){log.error("CHECK ERROR:",e);}}
|
|
}
|
|
}
|
|
|
|
public static void getSeedDecryptFile(String infile) {
|
|
|
|
FileInputStream inFile = null;
|
|
FileOutputStream outFile = null;
|
|
|
|
try{
|
|
|
|
inFile = new FileInputStream(infile);
|
|
outFile = new FileOutputStream(infile + ".dcr");
|
|
|
|
PBEKeySpec keySpec = new PBEKeySpec(password.toCharArray());
|
|
SecretKeyFactory keyFactory =
|
|
SecretKeyFactory.getInstance("PBEWithMD5AndDES");
|
|
SecretKey passwordKey = keyFactory.generateSecret(keySpec);
|
|
|
|
// Read in the previouly stored salt and set the iteration count.
|
|
|
|
byte[] salt = new byte[8];
|
|
inFile.read(salt);
|
|
int iterations = 100;
|
|
|
|
PBEParameterSpec parameterSpec = new PBEParameterSpec(salt, iterations);
|
|
|
|
// Create the cipher and initialize it for decryption.
|
|
|
|
Cipher cipher = Cipher.getInstance("PBEWithMD5AndDES");
|
|
cipher.init(Cipher.DECRYPT_MODE, passwordKey, parameterSpec);
|
|
|
|
|
|
byte[] input = new byte[64];
|
|
int bytesRead;
|
|
while ((bytesRead = inFile.read(input)) != -1)
|
|
{
|
|
byte[] output = cipher.update(input, 0, bytesRead);
|
|
if (output != null)
|
|
outFile.write(output);
|
|
}
|
|
|
|
byte[] output = cipher.doFinal();
|
|
if (output != null)
|
|
outFile.write(output);
|
|
|
|
inFile.close();
|
|
outFile.flush();
|
|
outFile.close();
|
|
|
|
} catch (InvalidKeySpecException e) {
|
|
log.error("CHECK ERROR:",e);
|
|
} catch (IllegalBlockSizeException e) {
|
|
log.error("CHECK ERROR:",e);
|
|
} catch (BadPaddingException e) {
|
|
log.error("CHECK ERROR:",e);
|
|
} catch (NoSuchAlgorithmException e) {
|
|
log.error("CHECK ERROR:",e);
|
|
} catch (NoSuchPaddingException e) {
|
|
log.error("CHECK ERROR:",e);
|
|
} catch (InvalidKeyException e) {
|
|
log.error("CHECK ERROR:",e);
|
|
} catch (InvalidAlgorithmParameterException e) {
|
|
log.error("CHECK ERROR:",e);
|
|
} catch(FileNotFoundException e){
|
|
log.error("CHECK ERROR:",e);
|
|
} catch(IOException e){
|
|
log.error("CHECK ERROR:",e);
|
|
} finally{
|
|
if(inFile!=null){try{inFile.close();}catch(IOException e){log.error("CHECK ERROR:",e);}}
|
|
if(outFile!=null){try{outFile.close();}catch(IOException e){log.error("CHECK ERROR:",e);}}
|
|
}
|
|
}
|
|
|
|
public static void setWaterMarkImg(String orgImg, String markImg){
|
|
|
|
float alpha = 1;
|
|
FileOutputStream out = null;
|
|
|
|
try{
|
|
|
|
// org image file
|
|
File file = new File(orgImg);
|
|
|
|
// watermark image file
|
|
File markFile = new File(markImg);
|
|
|
|
if(file.exists() && markFile.exists()) {
|
|
|
|
Image src = ImageIO.read(file);
|
|
|
|
if(src != null){
|
|
|
|
int width = src.getWidth(null);
|
|
int height = src.getHeight(null);
|
|
|
|
BufferedImage image = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
|
|
|
|
Graphics2D graphics2D = image.createGraphics();
|
|
graphics2D.drawImage(src, 0, 0, width, height, null);
|
|
|
|
Image mark_img = ImageIO.read(markFile);
|
|
|
|
if(mark_img != null){
|
|
|
|
int mark_img_width = mark_img.getWidth(null);
|
|
int mark_img_height = mark_img.getHeight(null);
|
|
|
|
graphics2D.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_ATOP, alpha));
|
|
|
|
// 우측 하단에 워터마크 표시
|
|
graphics2D.drawImage(mark_img, (width / 2) - (mark_img_width / 2), (height / 2) - (mark_img_height / 2), mark_img_width, mark_img_height, null);
|
|
graphics2D.dispose();
|
|
|
|
out = new FileOutputStream(orgImg);
|
|
// JPEGImageEncoder encoder = JPEGCodec.createJPEGEncoder(out);
|
|
// encoder.encode(image);
|
|
}
|
|
}
|
|
}
|
|
|
|
} catch (Exception e){
|
|
log.error(e);
|
|
}finally{
|
|
if(out!=null){try{out.close();}catch(IOException e){log.error(e);}}
|
|
}
|
|
}
|
|
}
|