3DES 알고리즘 암호 화 복호화

5315 단어 3DES 알고리즘
package com.common.util.CipherUtil;


import java.io.ByteArrayOutputStream;
import java.io.UnsupportedEncodingException;
import java.security.*;
import javax.crypto.*;
import javax.crypto.spec.SecretKeySpec;

/**
 *     DESede(3DES)   ,       
 */
public class TDESUtil 
{
	private static final String Algorithm = "DESede"; //        ,   DES,DESede,Blowfish
	private static final String hexString="0123456789ABCDEF";

	
	/**
	 * 													 
	 * @param keybyte      ,   24  
	 * @param src 	      (               。 )
	 * @return
	 */
	public static byte[] encryptMode(byte[] keybyte, byte[] src)
	{
		try 
		{
			//                   
			SecretKey deskey = new SecretKeySpec(keybyte, Algorithm);
			//   
			Cipher c1 = Cipher.getInstance(Algorithm);
			c1.init(Cipher.ENCRYPT_MODE, deskey);
			return c1.doFinal(src);
		} 
		catch (java.security.NoSuchAlgorithmException e1) 
		{
			e1.printStackTrace();
		} 
		catch (javax.crypto.NoSuchPaddingException e2) 
		{
			e2.printStackTrace();
		} 
		catch (java.lang.Exception e3)
		{
			e3.printStackTrace();
		}
		return null;
	}

	/**
	 * 
	 * @param keybyte   
	 * @param src	       
	 * @return
	 */
	public static byte[] decryptMode(byte[] keybyte, byte[] src)
	{
		try 
		{
			//     
			SecretKey deskey = new SecretKeySpec(keybyte, Algorithm);
			//   
			Cipher c1 = Cipher.getInstance(Algorithm);
			c1.init(Cipher.DECRYPT_MODE, deskey);
			return c1.doFinal(src);
		} 
		catch (java.security.NoSuchAlgorithmException e1) 
		{
			e1.printStackTrace();
		}
		catch (javax.crypto.NoSuchPaddingException e2)
		{
			e2.printStackTrace();
		}
		catch (java.lang.Exception e3)
		{
			e3.printStackTrace();
		}
		return null;
	}

	/**
	 *      16  
	 * @param str
	 * @return
	 */
	public static String encode(String str) 
	{ 
		//             
		byte[] bytes=str.getBytes(); 
		StringBuilder sb=new StringBuilder(bytes.length*2); 

		//             2 16     
		for(int i=0;i<bytes.length;i++) 
		{ 
			sb.append(hexString.charAt((bytes[i]&0xf0)>>4)); 
			sb.append(hexString.charAt((bytes[i]&0x0f)>>0)); 
		} 
		return sb.toString(); 
	} 
	/**
	 * 
	 * @param bytes
	 * @return
	 *  16          ,       (    ) 
	 */ 
	public static String decode(String bytes) 
	{ 
		ByteArrayOutputStream baos=new ByteArrayOutputStream(bytes.length()/2); 
		//  2 16            
		for(int i=0;i<bytes.length();i+=2) 
			baos.write((hexString.indexOf(bytes.charAt(i))<<4 |hexString.indexOf(bytes.charAt(i+1)))); 
		return new String(baos.toByteArray()); 
	} 

	//           
	public static String byte2hex(byte[] b) 
	{
		String hs = "";
		String stmp = "";
		for (int n = 0; n < b.length; n++) {
			stmp = (java.lang.Integer.toHexString(b[n] & 0XFF));
			if (stmp.length() == 1)
				hs = hs + "0" + stmp;
			else
				hs = hs + stmp;
			if (n < b.length - 1)
				hs = hs + ":";
		}
		return hs.toUpperCase();
	}


	public static void main(String[] args) throws UnsupportedEncodingException 
	{
		//        ,   JCE        
		//  addProvider                (            ,   )
		Security.addProvider(new com.sun.crypto.provider.SunJCE());
		
		//    
//		String szSrc = "This is a 3DES test.   ";
		String sourcePlatformID = "110000" ;
		String userID = "21" ;
		String date = "1350008287462" ; 
		String randomNum = "123456";
		String ticket = sourcePlatformID + "&" + userID + "&" + date +randomNum ;
		
		System.out.println("       :" + ticket);
		//     
	    String aString = "123456123456123456123456";
	    //           
	    final byte[] keyBytes = { 0x11, 0x22, 0x4F, 0x58, (byte)0x88, 0x10,
                0x40, 0x38, 0x28, 0x25, 0x79, 0x51, (byte) 0xCB, (byte) 0xDD,
                0x55, 0x66, 0x77, 0x29, 0x74, (byte) 0x98, 0x30, 0x40, 0x36,
                (byte) 0xE2 }; 
	    
		byte[] encoded = encryptMode(aString.getBytes(), ticket.getBytes());
		System.out.println("       :" + new String(encoded,"UTF8"));

		byte[] srcBytes = decryptMode(aString.getBytes(), encoded);
		System.out.println("       :" + new String(srcBytes));
		/*------------------------------------------------------------*/
		
		String byt="      16     ";
		byt=encode(byt);
		System.out.println("     16  : "+byt);
		
		byt=decode(byt);
		System.out.println("16          : "+byt);

	}
}

좋은 웹페이지 즐겨찾기