现在的位置: 首页 > 综合 > 正文

java和C#对16位的十六进制MAckey进行加密解密。 java解密C#用DES加密的字符串

2013年02月14日 ⁄ 综合 ⁄ 共 3184字 ⁄ 字号 评论关闭

java代码:

package c.m;

import java.security.spec.KeySpec;

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

public class DES{
	
	public static void main(String[] args) throws Exception {
	   //DES加密 16进制字符串
	  ENCRYPTMethod("5C656FF73140F705","1111111111111111");
	  
	  //DES解密---使用ECB模式
	  test2("A104E4BA4E121B4E","1111111111111111","DES/ECB/NoPadding");
	 }
	 
	 
	 /**
	  * DES加密
	  * @param HexString  字符串(16位16进制字符串)
	  * @param keyStr     密钥16个1
	  * @throws Exception
	  */
	 public static void ENCRYPTMethod(String HexString,String keyStr) throws Exception{ 
	        try {
	           byte[] theKey = null;
	           byte[] theMsg = null;
	           theMsg = hexToBytes(HexString);
	           theKey = hexToBytes(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);
	           byte[] theCph = cf.doFinal(theMsg);
	           System.out.println("*************DES加密****************");
	           System.out.println("密钥    : "+bytesToHex(theKey));
	           System.out.println("字符串: "+bytesToHex(theMsg));
	           System.out.println("加密后: "+bytesToHex(theCph));
	        } catch (Exception e) {
	           e.printStackTrace();
	           return;
	        }
	     }
	 
		/**
		 * DES解密
		 * 
		 * @param hexStr  16位十六进制字符串
		 * @param keyStr  密钥16个1
		 * @param modeStr 解密模式:ECB
		 * @throws Exception
		 */
		 public static void test2(String hexStr,String keyStr,String modeStr) throws Exception{
			 	
		        String algorithm = modeStr;
		        try {
		           byte[] theKey = null;
		           byte[] theMsg = null;
		           theMsg = hexToBytes(hexStr);
		           theKey = hexToBytes(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);
		           byte[] theCph = cf.doFinal(theMsg);
		           System.out.println("*************DES解密****************");
		           System.out.println("密钥    : "+bytesToHex(theKey));
		           System.out.println("字符串: "+bytesToHex(theMsg));
		           System.out.println("解密后: "+bytesToHex(theCph));
		           
		        } catch (Exception e) {
		           e.printStackTrace();
		           return;
		        }
		 
		 }
	 
	 public static byte[] hexToBytes(String str) {
	       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;
	       }

	    }
	    public static String bytesToHex(byte[] data) {
	      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();
	     }
	     }  
}

C#代码:

  //对MAC进行DES加密
        public static string Encrypt_DES16(string str_in_data, string str_DES_KEY) //数据为十六进制
        {
            try
            {
                byte[] shuju = new byte[8];
                byte[] keys = new byte[8];
                for (int i = 0; i < 8; i++)
                {
                    shuju[i] = Convert.ToByte(str_in_data.Substring(i * 2, 2), 16);
                    keys[i] = Convert.ToByte(str_DES_KEY.Substring(i * 2, 2), 16);
                }
                DES desEncrypt = new DESCryptoServiceProvider();
                desEncrypt.Mode = CipherMode.ECB;
                //desEncrypt.Key = ASCIIEncoding.ASCII.GetBytes(str_DES_KEY);
                desEncrypt.Key = keys;
                byte[] Buffer;
                Buffer = shuju;//ASCIIEncoding.ASCII.GetBytes(str_in_data);
                ICryptoTransform transForm = desEncrypt.CreateEncryptor();
                byte[] R;
                R = transForm.TransformFinalBlock(Buffer, 0, Buffer.Length);
                string return_str = "";
                foreach (byte b in R)
                {
                    return_str += b.ToString("X2");
                }
                return_str = return_str.Substring(0, 16);
                return return_str;
            }
            catch (Exception e)
            {
                throw e;
            }
        }

抱歉!评论已关闭.