/// <summary> /// 计算MAC /// </summary> /// <param name="reqData"></param> /// <param name="macKey"></param> /// <returns></returns> public static byte[] getMac(byte[] reqDataBytes, byte[] macKeyBytes) { reqDataBytes = getEightMultiplesData(reqDataBytes); int dataLen = reqDataBytes.Length; int groupLen = dataLen / 8; byte[][] body = new byte[groupLen][]; int index = 0; for (int i = 0; i < groupLen; i++) { body[i] = new byte[8]; Array.Copy(reqDataBytes, index, body[i], 0, 8); index += 8; } byte[] zero = new byte[8]; for (int i = 0; i < groupLen; i++) { zero = StringUtil.xor(body[i], zero); zero = DesEncryptUtil.desEncrypt(zero, macKeyBytes); } body = (byte[][])null; return(zero); }
/// <summary> /// 计算MAC /// </summary> /// <param name="reqData"></param> /// <param name="macKey"></param> /// <returns></returns> public static byte[] getMac(byte[] reqDataBytes, byte[] macKeyBytes) { byte[] left = new byte[8]; byte[] right = new byte[8]; Array.Copy(macKeyBytes, 0, left, 0, 8); Array.Copy(macKeyBytes, 8, right, 0, 8); byte[] macTemp = ANSIX99.getMac(reqDataBytes, left); string macTempStr = StringUtil.byteToHexString(macTemp); byte[] desRightTemp = DesEncryptUtil.desDecrypt(macTemp, right); string desRightTempStr = StringUtil.byteToHexString(desRightTemp); byte[] mac = DesEncryptUtil.desEncrypt(desRightTemp, left); string macStr = StringUtil.byteToHexString(mac); return(mac); }
public static byte[] getMac(byte[] reqDataBytes, byte[] keyBytes) { int dataLen = reqDataBytes.Length; int groupLen = dataLen / 8; byte[][] body = new byte[groupLen][]; int index = 0; for (int i = 0; i < groupLen; i++) { body[i] = new byte[8]; Array.Copy(reqDataBytes, index, body[i], 0, 8); index += 8; } byte[] zero = new byte[] { 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 }; for (int i = 0; i < groupLen; i++) { zero = StringUtil.xor(body[i], zero); } body = null; string bodyXor = StringUtil.byteToHexString(zero);//异或的结果 Console.WriteLine("异或结果:" + bodyXor); zero = null; byte[] bodyXor_1 = Encoding.UTF8.GetBytes(bodyXor.Substring(0, 8)); Console.WriteLine("左半部分:" + StringUtil.byteToHexString(bodyXor_1)); /** 开始进行加密[第一次] **/ byte[] bodyXor_encrypt = DesEncryptUtil.desEncrypt(bodyXor_1, keyBytes); Console.WriteLine("左半部分加密结果:" + StringUtil.byteToHexString(bodyXor_encrypt)); bodyXor_1 = null; int bodyLength = bodyXor.Length; byte[] bodyXor_2 = Encoding.UTF8.GetBytes(bodyXor.Substring(8, 8)); Console.WriteLine("右半部分:" + StringUtil.byteToHexString(bodyXor_2)); byte[] keyXor = StringUtil.xor(bodyXor_encrypt, bodyXor_2); Console.WriteLine("加密结果异或右半部分:" + StringUtil.byteToHexString(keyXor)); bodyXor_encrypt = null; bodyXor_2 = null; /** 开始进行加密[第二次] **/ byte[] keyXor_encrypt = DesEncryptUtil.desEncrypt(keyXor, keyBytes); Console.WriteLine("加密后:" + StringUtil.byteToHexString(keyXor_encrypt)); keyXor = null; return(keyXor_encrypt); }