SSISO Community

시소당

암,복호화 유틸

/*
  *  Created  on  2005.  6.  29.
  *
  *  TODO  To  change  the  template  for  this  generated  file  go  to
  *  Window  -  Preferences  -  Java  -  Code  Style  -  Code  Templates
  */
package  com.crypto;

import  java.security.InvalidKeyException;
import  java.security.NoSuchAlgorithmException;
import  java.security.spec.InvalidKeySpecException;

import  javax.crypto.Cipher;
import  javax.crypto.SecretKey;
import  javax.crypto.SecretKeyFactory;
import  javax.crypto.spec.DESKeySpec;

/**
  *  @author  kkaok
  *  
  *  TODO  To  change  the  template  for  this  generated  type  comment  go  to  Window  -
  *  Preferences  -  Java  -  Code  Style  -  Code  Templates
  */
public  class  SecurityUtil  {
        /*
          *  자바시큐리티  르로그램밍  책  693참조  
          *  byte[]  desKeyData  =  generateHash(strkey);
          */
        private  static  SecretKey  getKey(String  strkey)  throws  InvalidKeyException,  NoSuchAlgorithmException,  InvalidKeySpecException{
                //  입력값을  bytes로  변환
                byte[]  desKeyData  =  strkey.getBytes();
                //  암호화  키  생성
                DESKeySpec  desKeySpec  =  new  DESKeySpec(desKeyData);
                SecretKeyFactory  keyFactory  =  SecretKeyFactory.getInstance("DES");
                return  keyFactory.generateSecret(desKeySpec);
        }
        //  주민등록번호  처럼  영구적인  값을  암호화  하고  다시  복호할  때는  키값이  바뀌면  안되기  때문에  고정된  키값을  갖는  메소드를  사용한다.  
        public  static  String  encrypt(String  data)  throws  Exception  {
                return  encrypt(data,  Constant.SECURITYKEY);
        }
        //  주민등록번호  처럼  영구적인  값을  암호화  하고  다시  복호할  때는  키값이  바뀌면  안되기  때문에  고정된  키값을  갖는  메소드를  사용한다.  
        public  static  String  decrypt(String  data)  throws  Exception  {
                return  decrypt(data,  Constant.SECURITYKEY);
        }
        
        public  static  String  encrypt(String  data,  String  strkey)  throws  Exception  {
                if  (data  ==  null  ||  data.length()  ==  0)
                        return  "";
                //  암호화를  위한  세팅    
                Cipher  cipher  =  Cipher.getInstance("DES/ECB/PKCS5Padding");
                cipher.init(Cipher.ENCRYPT_MODE,  getKey(strkey));

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

                sun.misc.BASE64Encoder  encoder  =  new  sun.misc.BASE64Encoder();
                return  encoder.encode(outputBytes1);
        }
        public  static  String  decrypt(String  data,  String  strkey)  throws  Exception  {
                if  (data  ==  null  ||  data.length()  ==  0)
                        return  "";
                javax.crypto.Cipher  cipher  =  javax.crypto.Cipher
                                .getInstance("DES/ECB/PKCS5Padding");
                cipher.init(javax.crypto.Cipher.DECRYPT_MODE,  getKey(strkey));

                sun.misc.BASE64Decoder  decoder  =  new  sun.misc.BASE64Decoder();
                byte[]  inputBytes1  =  decoder.decodeBuffer(data);
                byte[]  outputBytes2  =  cipher.doFinal(inputBytes1);

                return  new  String(outputBytes2,  "UTF8");
        }

}

  



--------------------------------------------------------------------------------


/*
  *  Created  on  2005.  6.  29.
  *
  *  TODO  To  change  the  template  for  this  generated  file  go  to
  *  Window  -  Preferences  -  Java  -  Code  Style  -  Code  Templates
  */
package  com.crypto;

/**
  *  @author  kkaok
  *
  *  TODO  To  change  the  template  for  this  generated  type  comment  go  to
  *  Window  -  Preferences  -  Java  -  Code  Style  -  Code  Templates
  */
public  class  Constant  {
        public  final  static  String  SECURITYKEY  =  "123456789";
}

[출처]  [본문스크랩]  암,복호화  유틸  |작성자  metaljuce
http://blog.naver.com/metaljuce?Redirect=Log&logNo=40034352444

1645 view

4.0 stars