본문 바로가기

Programming/Java

[Secret] Encrypt / Decrypt

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.security.Key;
import java.security.NoSuchAlgorithmException;
import java.security.SecureRandom;

import javax.crypto.Cipher;
import javax.crypto.KeyGenerator;

import sun.misc.BASE64Decoder;
import sun.misc.BASE64Encoder;

public class Crypto {

        public static Key key = null;
        public static final String defaultkeyfileurl = "defaultkey.key";

        /**
        * 비밀키 생성메소드
        * @return  void
        * @exception java.io.IOException,java.security.NoSuchAlgorithmException
        */
        public static File makekey() throws IOException, NoSuchAlgorithmException {
                return makekey(defaultkeyfileurl);
        }

        public static File makekey(String filename) throws IOException, NoSuchAlgorithmException {
                File tempfile = new File(".",filename);
                KeyGenerator generator = KeyGenerator.getInstance("DES");

                generator.init(new SecureRandom());

                Key key = generator.generateKey();

                ObjectOutputStream out = new ObjectOutputStream(new FileOutputStream(tempfile));

                out.writeObject(key);
                out.close();

                return tempfile;
        }

        /**
        * 지정된 비밀키를 가지고 오는 메서드
        * @return  Key 비밀키 클래스
        * @exception Exception
        */
        private static Key getKey() throws Exception {
                if(key != null) {
                        return key;
                } else {
                        return getKey(defaultkeyfileurl);
                }
        } 

        private static Key getKey(String fileurl) throws Exception {
                if(key == null) {
                        File file = new File(fileurl);
                        if(!file.exists()) {
                                file = makekey();
                        }
                        if(file.exists()) {
                                ObjectInputStream in = new ObjectInputStream(new FileInputStream(fileurl));
                                key = (Key)in.readObject();
                                in.close();
                        } else {
                                throw new Exception("암호키객체를 생성할 수 없습니다.");
                        }
                }
                return key;
        }  

        /**
        * 문자열 대칭 암호화
        * @param   ID  비밀키 암호화를 희망하는 문자열
        * @return  String  암호화된 ID
        * @exception Exception
        */
        public static String symmetryEncrypt(String ID) throws Exception {
                if ( ID == null || ID.length() == 0 ) {
                        return "";
                }

                Cipher cipher = Cipher.getInstance("DES/ECB/PKCS5Padding");
                cipher.init(Cipher.ENCRYPT_MODE,getKey());
                String amalgam = ID;

                byte[] inputBytes1 = amalgam.getBytes("UTF8");
                byte[] outputBytes1 = cipher.doFinal(inputBytes1);

                BASE64Encoder encoder = new BASE64Encoder();

                String outputStr1 = encoder.encode(outputBytes1);

                return outputStr1;
        }   

        /**
        * 문자열 대칭 복호화
        * @param   codedID  비밀키 복호화를 희망하는 문자열
        * @return  String  복호화된 ID
        * @exception Exception
        */
        public static String symmetryDecrypt(String codedID) throws Exception {
                if ( codedID == null || codedID.length() == 0 ) {
                        return "";
                }

                Cipher cipher = javax.crypto.Cipher.getInstance("DES/ECB/PKCS5Padding");
                cipher.init(Cipher.DECRYPT_MODE, getKey());
                BASE64Decoder decoder = new BASE64Decoder();

                byte[] inputBytes1  = decoder.decodeBuffer(codedID);
                byte[] outputBytes2 = cipher.doFinal(inputBytes1);

                String strResult = new String(outputBytes2,"UTF8");

                return strResult;
        }

}

'Programming > Java' 카테고리의 다른 글

[Log4j] Logger Properties 설정  (0) 2010.03.04
[Beginner] Text Read  (0) 2010.01.28
[Beginner] Excel Read - POI  (0) 2010.01.28
[Beginner] HashMap, List GetDataString  (0) 2010.01.19
[Beginner] HttpServletRequest - HashMap, List Convert  (1) 2010.01.19