자바 알 기 쉬 운 MD5 암호 화(직접 실행 가능)(2)

7769 단어 자바MD5 암호 화
프로그램 전문 은 다음 과 같 습 니 다. 

package com.neusoft.test.util.crypt;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.net.URLDecoder;
import java.net.URLEncoder;
import java.security.MessageDigest;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import javax.crypto.Cipher;
import javax.crypto.SecretKey;
import javax.crypto.SecretKeyFactory;
import javax.crypto.spec.DESedeKeySpec;
import sun.misc.BASE64Decoder;
import sun.misc.BASE64Encoder;
/**
* <p>Title: </p>
*
* <p>Description: </p>
*
*<p>Date : 2005-08-11</p>
*
* <p>Copyright: Copyright (c) 2005 neusoft</p>
*
* <p>Company: neusoft</p>
*
* @author mengk
* @version 1.00
*
* <p>------------------------------------------------------------</p>
* <p> </p>
* <p> </p>
* <p> 1 </p>
*/
public class Endecrypt {
/**
* MD5
* @param String SPKEY
* @return byte[] md5 byte[]
*/
private byte[] md5(String strSrc)
{
byte[] returnByte = null;
try
{
MessageDigest md5 = MessageDigest.getInstance("MD5");
returnByte = md5.digest(strSrc.getBytes("GBK"));
}
catch(Exception e)
{
e.printStackTrace();
}
return returnByte;
}
/**
* 3-DES
* , 24 ,md5 16 , 8 0
* @param String SPKEY
* @return byte[] md5 byte[]
*/
private byte[] getEnKey(String spKey)
{
byte[] desKey=null;
try
{
byte[] desKey1 = md5(spKey);
desKey = new byte[24];
int i = 0;
while (i < desKey1.length && i < 24) {
desKey[i] = desKey1[i];
i++;
}
if (i < 24) {
desKey[i] = 0;
i++;
}
}
catch(Exception e){
e.printStackTrace();
}
return desKey;
}
/**
* 3-DES
* @param byte[] src 3-DES byte[]
* @param byte[] enKey 3-DES
* @return byte[] 3-DES byte[]
*/
public byte[] Encrypt(byte[] src,byte[] enKey)
{
byte[] encryptedData = null;
try
{
DESedeKeySpec dks = new DESedeKeySpec(enKey);
SecretKeyFactory keyFactory = SecretKeyFactory.getInstance("DESede");
SecretKey key = keyFactory.generateSecret(dks);
Cipher cipher = Cipher.getInstance("DESede");
cipher.init(Cipher.ENCRYPT_MODE, key);
encryptedData = cipher.doFinal(src);
}
catch(Exception e)
{
e.printStackTrace();
}
return encryptedData;
}
/**
* Base64
* @param byte[] src
*
* @return String
*/
public String getBase64Encode(byte[] src)
{
String requestValue="";
try{
BASE64Encoder base64en = new BASE64Encoder();
requestValue=base64en.encode(src);
//System.out.println(requestValue);
}
catch(Exception e){
e.printStackTrace();
}

return requestValue;
}
/**
*
* base64 3-DES ,
* , , uni-wise ,
* “sp ”。 , ,
* , ,
* 。 。
* c# , 。
*
*/
private String filter(String str)
{
String output = null;
StringBuffer sb = new StringBuffer();
for(int i = 0; i < str.length(); i++)
{
int asc = str.charAt(i);
if(asc != 10 && asc != 13)
sb.append(str.subSequence(i, i + 1));
}
output = new String(sb);
return output;
}
/**
* URLDecoder.encode(strEncoding)
* @param String src
*
* @return String
*/
public String getURLEncode(String src)
{
String requestValue="";
try{

requestValue = URLEncoder.encode(src);
}
catch(Exception e){
e.printStackTrace();
}

return requestValue;
}
/**
* 3-DES
* @param String src 3-DES String
* @param String spkey SPKEY
* @return String 3-DES String
*/
public String get3DESEncrypt(String src,String spkey)
{
String requestValue="";
try{


// 3-DES
byte[] enKey = getEnKey(spkey);
// 3-DES \"UTF-16LE\"
byte[] src2 = src.getBytes("UTF-16LE");
// 3-DES
byte[] encryptedData = Encrypt(src2,enKey);


// 3-DES BASE64
String base64String = getBase64Encode(encryptedData);
//BASE64
String base64Encrypt = filter(base64String);

// BASE64 HTML
requestValue=getURLEncode(base64Encrypt);
//System.out.println(requestValue);
}
catch(Exception e){
e.printStackTrace();
}

return requestValue;
}
/**
* URLDecoder.decode(strEncoding)
* @param String src
*
* @return String
*/
public String getURLDecoderdecode(String src)
{
String requestValue="";
try{

requestValue = URLDecoder.decode(src);
}
catch(Exception e){
e.printStackTrace();
}

return requestValue;
}
/**
*
* 3-DES ( )。
* @param byte[] src 3-DES byte[]
* @param String spkey SPKEY
* @return String 3-DES String
*/
public String deCrypt(byte[] debase64,String spKey)
{
String strDe = null;
Cipher cipher = null;
try
{
cipher=Cipher.getInstance("DESede");
byte[] key = getEnKey(spKey);
DESedeKeySpec dks = new DESedeKeySpec(key);
SecretKeyFactory keyFactory = SecretKeyFactory.getInstance("DESede");
SecretKey sKey = keyFactory.generateSecret(dks);
cipher.init(Cipher.DECRYPT_MODE, sKey);
byte ciphertext[] = cipher.doFinal(debase64);
strDe = new String(ciphertext,"UTF-16LE");
}
catch(Exception ex)
{
strDe = "";
ex.printStackTrace();
}
return strDe;
}
/**
* 3-DES
* @param String src 3-DES String
* @param String spkey SPKEY
* @return String 3-DES String
*/
public String get3DESDecrypt(String src,String spkey)
{
String requestValue="";
try{


// 3-DES

//URLDecoder.decodeTML
String URLValue=getURLDecoderdecode(src);

// 3-DES BASE64

BASE64Decoder base64Decode = new BASE64Decoder();
byte[] base64DValue = base64Decode.decodeBuffer(URLValue);

// 3-DES \"UTF-16LE\"
requestValue = deCrypt(base64DValue,spkey);
}
catch(Exception e){
e.printStackTrace();
}
return requestValue;
}
public static void main(String[] args) {
Endecrypt test = new Endecrypt();
String oldString = " ";
String SPKEY = "1234";
System.out.println("1。 SPKEY : "+SPKEY);
System.out.println("2。 : "+oldString);
String reValue = test.get3DESEncrypt(oldString,SPKEY);
reValue = reValue.trim().intern();
System.out.println(" 3-DES : "+reValue);
String reValue2 = test.get3DESDecrypt(reValue,SPKEY);
System.out.println(" 3-DES : "+reValue2);
}
}

좋은 웹페이지 즐겨찾기