JAVA

passwd 암호화

사라링 2012. 5. 15. 18:33

 

import java.io.UnsupportedEncodingException;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;

/**
 *
 *
 * @author
 */
public class strEnDeConvert {

    private  byte[] hexaMap = {0x30,0x31,0x32,0x33,0x34,0x35,0x36,0x37,0x38,0x39,0x61,0x62,0x63,0x64,0x65,0x66,0x67,0x68};

    protected  byte[] getHexaEncodingBytes(byte[] src)
 {
        byte[] buffer=new byte[src.length*2];
        int index=0;

  System.out.println("src.length="+ src.length);  

        for (int i=0; i < src.length; i++)
  {   buffer[index++] = hexaMap[ ( (src[i]&0xf0) >> 4 ) ];
            buffer[index++] = hexaMap[ ( src[i]&0x0f ) ];
        }
  return buffer;
    }

    protected  String getHexaEncoding(byte[] src,String charSet) throws UnsupportedEncodingException
 {
        byte[] strBytes=getHexaEncodingBytes(src);
        return new String(strBytes,charSet);
    }

    protected  String getHexaEncoding(byte[] src)
 {
        byte[] strBytes=getHexaEncodingBytes(src);
        return new String(strBytes);
    }

    /**
     * Digest 알고리즘을 수행한다.
     */
    public String getDigest(String str, String digest, String charset) {
        try
  {   MessageDigest md = MessageDigest.getInstance(digest);
            try
   {   byte[]      raw = str.getBytes(charset);
                byte[] digested = md.digest(raw);
                System.out.println("digested.length="+ digested.length); 
    System.out.println("str.length="+ str.length());

    return getHexaEncoding(digested, charset);
            } catch (UnsupportedEncodingException e) {
            } 
  } catch (NoSuchAlgorithmException e) {
        }       
        return str;
    }
 
 // Test
 public static void main(String[] args) throws Exception
 {
  strEnDeConvert ec = new strEnDeConvert();
  Defines        df = new Defines();
  System.out.println(ec.getDigest("1111122222333334445",df.PASSWORD_DIGEST, df.PASSWORD_CHARSET));
 }

}

 

이 글은 스프링노트에서 작성되었습니다.