2007년 11월 16일 금요일

Using base 64 Crypto 2 Md5 Algorithm.

package raphyr.util;

import java.security.MessageDigest;

import java.security.NoSuchAlgorithmException;

public class SecurityUtil

{

private Base64 Base64 = null;

/**

* byte[] ret = HashUtil.digest("MD5", "abcd".getBytes());

*/

public byte[] digest(String alg, byte[] input) throws NoSuchAlgorithmException

{

MessageDigest md = MessageDigest.getInstance(alg);

return md.digest(input);

}

public String getCryptoMD5String(String inputValue) throws Exception

{

Base64 = new Base64();

if( inputValue == null ) throw new Exception("Can't conver to Message Digest 5 String value!!");

byte[] ret = digest("MD5", inputValue.getBytes());

String result = Base64.encode(ret);

return result;

}

}

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

package raphyr.util;

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;

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

public class Base64
{
public String encode(byte[] encodeBytes)
{
BASE64Encoder base64Encoder = new BASE64Encoder();
ByteArrayInputStream bin = new ByteArrayInputStream(encodeBytes);
ByteArrayOutputStream bout = new ByteArrayOutputStream();

byte[] buf = null;

try
{
base64Encoder.encodeBuffer(bin, bout);
}
catch(Exception e)
{
System.out.println("Exception");
e.printStackTrace();
}
buf = bout.toByteArray();

return new String(buf).trim();
}
public byte[] decode(String strDecode)
{
BASE64Decoder base64Decoder = new BASE64Decoder();
ByteArrayInputStream bin = new ByteArrayInputStream(strDecode.getBytes());
ByteArrayOutputStream bout = new ByteArrayOutputStream();

byte[] buf = null;

try
{
base64Decoder.decodeBuffer(bin, bout);
}
catch(Exception e)
{
System.out.println("Exception");
e.printStackTrace();
}
buf = bout.toByteArray();

return buf;
}
}

0 개의 덧글: