Пример #1
0
        private static bool CalcMac_CBC_X99(PackageBase target, byte[] DATA, byte[] hardKey, byte[] softKey, AlgorithmType alType, byte[] MAC)
        {
            EncryptType tmp = target.EnType;

            try
            {
                Esam.Status eRet = Esam.Status.ESAM_FAIL;
                //Test 为了加速
                target.EnType = EncryptType.Soft;
                //if (target.EnType == EncryptType.Hardware && alType == AlgorithmType.X99)
                //{
                //    eRet = Esam.CalcMac(hardKey, DATA, DATA.Length, MAC);
                //    if (eRet != Esam.Status.ESAM_SUCC)
                //        return false;
                //    else
                //        return true;
                //}
                //else if (target.EnType == EncryptType.Hardware && alType == AlgorithmType.CBC && target.DType == DesType.Des)
                //{
                //    eRet = Esam.CalcMac(hardKey, DATA, DATA.Length, MAC);
                //    if (eRet != Esam.Status.ESAM_SUCC)
                //        return false;
                //    else
                //        return true;
                //}
                //else
                //{
                int    len   = (DATA.Length % 8 == 0 ? DATA.Length : DATA.Length + 8 - DATA.Length % 8);
                byte[] cData = new byte[len];
                Array.Copy(DATA, cData, DATA.Length);
                byte[] tmpRet = new byte[8];
                for (int i = 0; i < len; i += 8)
                {
                    for (int j = 0; j < 8; j++)
                    {
                        tmpRet[j] ^= cData[j + i];
                    }
                    byte[] tmpRetEncrypt = new byte[8];
                    if (target.DType == DesType.Des && target.EnType == EncryptType.Hardware)
                    {
                        //这里用EsamCalcMac函数代替计算,效果相同
                        eRet = Esam.CalcMac(hardKey, tmpRet, 8, tmpRetEncrypt);
                    }
                    else
                    if (target.DType == DesType.Des && target.EnType == EncryptType.Soft)
                    {
                        //DES软加密
                        tmpRetEncrypt = Encrypt.DESEncrypt(tmpRet, softKey);
                        eRet          = Esam.Status.ESAM_SUCC;
                    }
                    else if (target.DType == DesType.TripleDes && alType == AlgorithmType.CBC)
                    {
                        switch (target.EnType)
                        {
                        case EncryptType.Hardware:
                            //这里用EsamCalcMac函数代替计算,效果相同
                            eRet = Esam.CalcMac(hardKey, tmpRet, 8, tmpRetEncrypt);
                            break;

                        case EncryptType.Soft:
                            //3DES软加密
                            tmpRetEncrypt = Encrypt.DES3Encrypt(tmpRet, softKey);
                            eRet          = Esam.Status.ESAM_SUCC;
                            break;
                        }
                    }
                    else if (target.DType == DesType.TripleDes && alType == AlgorithmType.X99)
                    {
                        //这里只有软加密一种可能,硬加密前面已处理
                        if (i == len - 8)
                        {
                            //3DES软加密
                            tmpRetEncrypt = Encrypt.DES3Encrypt(tmpRet, softKey);
                            //AppLog.Write("加密一次", AppLog.LogMessageType.Debug);
                            //AppLog.Write(Utility.bcd2str(softKey, softKey.Length), AppLog.LogMessageType.Debug);
                            //AppLog.Write(Utility.bcd2str(tmpRet, tmpRet.Length), AppLog.LogMessageType.Debug);
                            //AppLog.Write(Utility.bcd2str(tmpRetEncrypt, tmpRetEncrypt.Length), AppLog.LogMessageType.Debug);
                            eRet = Esam.Status.ESAM_SUCC;
                        }
                        else
                        {
                            //DES软加密
                            byte[] tmpKey = new byte[8];
                            Array.Copy(softKey, tmpKey, 8);
                            tmpRetEncrypt = Encrypt.DESEncrypt(tmpRet, tmpKey);
                            eRet          = Esam.Status.ESAM_SUCC;
                        }
                    }

                    if (eRet == Esam.Status.ESAM_SUCC)
                    {
                        Array.Copy(tmpRetEncrypt, tmpRet, 8);
                    }
                    else
                    {
                        return(false);
                    }
                }
                Array.Copy(tmpRet, MAC, 8);
                return(true);
                //}
            }
            catch (System.Exception e)
            {
                Log.Error(target, e);
                return(false);
            }
            finally
            {
                target.EnType = tmp;
            }
        }
Пример #2
0
        private static bool CalcMac_ECB(PackageBase target, byte[] DATA, byte[] hardKey, byte[] softKey, byte[] MAC)
        {
            try
            {
                Esam.SetWorkmode(Esam.WorkMode.Encrypt);
                Esam.Status eRet       = Esam.Status.ESAM_FAIL;
                byte[]      xorRet     = CalcMacXor(DATA);
                string      strXor     = Utility.bcd2str(xorRet, 8);
                byte[]      newByte    = Encoding.ASCII.GetBytes(strXor);
                byte[]      beforeByte = new byte[8];
                byte[]      afterByte  = new byte[8];
                Array.Copy(newByte, beforeByte, 8);
                Array.Copy(newByte, 8, afterByte, 0, 8);
                byte[] encryptData = new byte[8];
                #region 第一次计算
                switch (target.EnType)
                {
                case EncryptType.Hardware:
                    //这里用EsamCalcMac函数代替计算,效果相同
                    eRet = Esam.CalcMac(hardKey, beforeByte, 8, encryptData);
                    break;

                case EncryptType.Soft:
                    switch (target.DType)
                    {
                    case DesType.Des:
                        //DES软加密
                        encryptData = Encrypt.DESEncrypt(beforeByte, softKey);
                        break;

                    case DesType.TripleDes:
                        //3DES软加密
                        encryptData = Encrypt.DES3Encrypt(beforeByte, softKey);
                        break;
                    }
                    eRet = Esam.Status.ESAM_SUCC;
                    break;
                }
                #endregion
                if (eRet == Esam.Status.ESAM_SUCC)
                {
                    for (int i = 0; i < 8; i++)
                    {
                        xorRet[i] = Convert.ToByte(encryptData[i] ^ afterByte[i]);
                    }
                    #region 第二次计算
                    switch (target.EnType)
                    {
                    case EncryptType.Hardware:
                        //这里用EsamCalcMac函数代替计算,效果相同
                        eRet = Esam.CalcMac(hardKey, xorRet, 8, encryptData);
                        break;

                    case EncryptType.Soft:
                        switch (target.DType)
                        {
                        case DesType.Des:
                            //DES软加密
                            encryptData = Encrypt.DESEncrypt(xorRet, softKey);
                            break;

                        case DesType.TripleDes:
                            //3DES软加密
                            encryptData = Encrypt.DES3Encrypt(xorRet, softKey);
                            break;
                        }
                        eRet = Esam.Status.ESAM_SUCC;
                        break;
                    }
                    #endregion
                    if (eRet == Esam.Status.ESAM_SUCC)
                    {
                        strXor  = Utility.bcd2str(encryptData, 8);
                        newByte = Encoding.ASCII.GetBytes(strXor);
                        //MAC = new byte[8];
                        Array.Copy(newByte, MAC, 8);
                        return(true);
                    }
                    else
                    {
                        return(false);
                    }
                }
                else
                {
                    return(false);
                }
            }
            catch (System.Exception e)
            {
                Log.Error(target, e);
                return(false);
            }
            finally
            {
                Esam.SetWorkmode(Esam.WorkMode.Default);
            }
        }
Пример #3
0
 protected PackageBase(PackageBase pb)
 {
     readConfig();
     SendPackage = new Iso8583Package(pb.SendPackage);
     RecvPackage = new Iso8583Package(mSchemaFile);
 }
Пример #4
0
 protected bool RemoveWork(PackageBase instance)
 {
     return(RemoveWork(mSectionName, instance));
 }
Пример #5
0
 protected void EnqueueWork(PackageBase instance)
 {
     EnqueueWork(mSectionName, instance);
 }
Пример #6
0
 protected static void AsyncTransaction(PackageBase package, PackageBase.ResultHandler handler)
 {
     package.OnResult = handler;
     SendMessage(ActivityHandler.TRANSACTION, package);
 }