java版PBOC-DES-MAC加密算法。(测试成功)

帮助类,同上篇文章中的帮助类。可以下载工具验证。

package com.fhk.Decrypt;


//PBOCDES算法
public class PBOCDES {

	public static void main(String[] args) {
		//测试
		String str= "182012121102335900010000000100000";
		String result = Get_PBOC_DES(str.getBytes(),"F007F787335709F4", "0000000000000000");
		System.out.println(result);
	}
	
	/*
	 *  PBOCDES加密
	 *  @param shuju:加密的数据的byte[]
	 *  @param key:密钥 16位十六进制
	 *  @param IV:初始向量,默认为0000000000000000
	 */
	public static String Get_PBOC_DES(byte[] shuju, String key, String IV)
	{
		String returntype = "";
		try
		{
	         //----------------------------------------
	         byte[] keyss = new byte[8];
	         byte[] IVS = new byte[8];
	         keyss = PBOCDESConvertUtil.hexStringToByte(key);
	         IVS = PBOCDESConvertUtil.hexStringToByte(IV);
	         //----------------------------------------
	         byte[] keys = keyss;
	         //数据内容字节数组
	         String slshuju = PBOCDESConvertUtil.bytesToHexString(shuju);
	         int TLen = 0;
	         int DBz = 0;
	         if (slshuju.length() % 16 != 0 || slshuju.length() % 16 == 0)
	         {
	             TLen = (((int)(slshuju.length() / 16)) + 1) * 16;
	             DBz = (slshuju.length() / 16) + 1;
	             slshuju = slshuju + "8";
	             TLen = TLen - slshuju.length();
	             for (int i = 0; i < TLen; i++)
	             {
	                 slshuju = slshuju + "0";
	             }
	         }
	         byte[] Zshuju = new byte[slshuju.length() / 2];
	         Zshuju = PBOCDESConvertUtil.hexStringToByte(slshuju);
	         
	         byte[] D1 = new byte[8];
	         byte[] D2 = new byte[8];
	         byte[] I2 = new byte[8];
	         byte[] I3 = new byte[8];
	         byte[] bytTemp = new byte[8];
	         byte[] bytTempX = new byte[8];
	         //初始向量
	         byte[] I0 = IVS;
	         if (DBz >= 1)
	         {
	             for (int i = 0; i < 8; i++)
	             {
	                 D1[i] = Zshuju[i];
	             }
	             for (int i = 0; i < 8; i++)
	             {
	                 bytTemp[i] = (byte)(I0[i] ^ D1[i]);
	             }
	             I2 = bytTemp;
	             bytTempX = DES.SEncrypt_DES(I2, keys);
	         }
	         if (DBz >= 2)
	         {
	             for (int j = 2; j <= DBz; j++)
	             {
	                 for (int i = (j - 1) * 8; i < j * 8; i++)
	                 {
	                     D2[i - (j - 1) * 8] = Zshuju[i];
	                 }
	                 for (int i = 0; i < 8; i++)
	                 {
	                     bytTemp[i] =  (byte)(bytTempX[i] ^ D2[i]);
	                 }
	                 I3 = bytTemp;
	                 bytTempX = DES.SEncrypt_DES(I3, keys);
	             }
	         }
	         returntype = PBOCDESConvertUtil.bytesToHexString(bytTempX);
	         
		}catch(Exception e)
		{
			returntype = "";
		}
		return returntype;
	}
	
}

package com.fhk.Decrypt;

import java.security.spec.KeySpec;

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

/**
 * DES加密、解密
 * @ClassName DES
 * @author 张月
 * @date 2013年8月8日
 */
public class DES{
	
	 /**
	  * DES加密
	  * @param HexString  字符串(16位16进制字符串)
	  * @param keyStr     密钥16个1
	  * @throws Exception
	  */
	 public static byte[] SEncrypt_DES(byte[] HexString,byte[] keyStr) throws Exception{
			 try {
				 byte[] theCph = new byte[8];
			        try {
			           byte[] theKey = null;
			           byte[] theMsg = null;
			           theMsg = HexString;
			           theKey = keyStr;
			           KeySpec ks = new DESKeySpec(theKey);
			           SecretKeyFactory kf = SecretKeyFactory.getInstance("DES");
			           SecretKey ky = kf.generateSecret(ks);
			           Cipher cf = Cipher.getInstance("DES/ECB/NoPadding");
			           cf.init(Cipher.ENCRYPT_MODE,ky);
			           theCph = cf.doFinal(theMsg);
//			           System.out.println("*************DES加密****************");
//			           System.out.println("密钥    : "+bytesToHex(keyStr));
//			           System.out.println("字符串: "+bytesToHex(theMsg));
//			           System.out.println("加密后: "+bytesToHex(theCph));
			        } catch (Exception e) {
			           e.printStackTrace();
			        }
			        return theCph;
			} catch (Exception e) {
				throw e;
			}
	     }
	 
//	 //进行ECB模式的DES加密,已验证成功
//	 public static void main(String[] args) throws Exception {
//		 byte[] s = SEncrypt_DES(PBOCDESConvertUtil.hexStringToByte("0123456789ABCDEF"),PBOCDESConvertUtil.hexStringToByte("1111111111111111"));
//		 System.out.println(s.length);
//	}
	 
		/**
		 * DES解密
		 * 
		 * @param hexStr  16位十六进制字符串
		 * @param keyStr  密钥16个1
		 * @param modeStr 解密模式:ECB
		 * @throws Exception
		 */
		 public static byte[] SDecrypt_DES(byte[] hexStr,byte[] keyStr) throws Exception{
				 try {
					  String algorithm = "DES/ECB/NoPadding";
				      byte[] theCph = new byte[8];
			           byte[] theKey = null;
			           byte[] theMsg = null;
			           theMsg = hexStr;
			           theKey = keyStr;
			           KeySpec ks = new DESKeySpec(theKey);
			           SecretKeyFactory kf
			              = SecretKeyFactory.getInstance("DES");
			           SecretKey ky = kf.generateSecret(ks);
			           Cipher cf = Cipher.getInstance(algorithm);
			           cf.init(Cipher.DECRYPT_MODE,ky);
			           theCph = cf.doFinal(theMsg);
//				           System.out.println("*************DES解密****************");
//				           System.out.println("密钥    : "+bytesToHex(theKey));
//				           System.out.println("字符串: "+bytesToHex(theMsg));
//				           System.out.println("解密后: "+bytesToHex(theCph));
			           
			        return theCph; 
				 
				} catch (Exception e) {
					throw e;
				}
		 }
	 
	 public static byte[] hexToBytes(String str) {
		 try {
			 if (str==null) {
		          return null;
		       } else if (str.length() < 2) {
		          return null;
		       } else {
		          int len = str.length() / 2;
		          byte[] buffer = new byte[len];
		          for (int i=0; i<len; i++) {
		              buffer[i] = (byte) Integer.parseInt(
		                 str.substring(i*2,i*2+2),16);
		          }
		          return buffer;
		       }

		} catch (Exception e) {
			throw e;
		}
    }
    public static String bytesToHex(byte[] data) {
    	try {
    		if (data==null) {
   	         return null;
   	      	} else {
	   	       int len = data.length;
	   	       String str = "";
	   	       for (int i=0; i<len; i++) {
	   	        if ((data[i]&0xFF)<16) str = str + "0"
	   	                  + java.lang.Integer.toHexString(data[i]&0xFF);
	   	        else str = str
	   	                  + java.lang.Integer.toHexString(data[i]&0xFF);
   	       		}
	   	       return str.toUpperCase();
   	      }
		} catch (Exception e) {
			throw e;
		}
    }  
    
   
}



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值