コード例 #1
0
ファイル: CTransaction.cs プロジェクト: radtek/RLPayment
        protected bool CalcMac_CBC_X99(byte[] DATA, byte[] hardKey, byte[] softKey, AlgorithmType alType, byte[] MAC)
        {
            try
            {
                Esam.FuncRet eRet = Esam.FuncRet.ESAM_FAIL;
                if (EnType == EncryptType.Hardware && alType == AlgorithmType.X99)
                {
                    eRet = Esam.EsamCalcMac(hardKey, DATA, DATA.Length, MAC);
                    if (eRet != Esam.FuncRet.ESAM_SUCC)
                    {
                        return(false);
                    }
                    else
                    {
                        return(true);
                    }
                }
                else if (EnType == EncryptType.Hardware && alType == AlgorithmType.CBC && DType == DesType.Des)
                {
                    eRet = Esam.EsamCalcMac(hardKey, DATA, DATA.Length, MAC);
                    if (eRet != Esam.FuncRet.ESAM_SUCC)
                    {
                        return(false);
                    }
                    else
                    {
                        return(true);
                    }
                }
                else
                {
                    EncryptType tmp = EnType;
                    EnType = EncryptType.Soft;
                    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 (DType == DesType.Des && EnType == EncryptType.Hardware)
                        //{
                        //    //这里用EsamCalcMac函数代替计算,效果相同
                        //    eRet = Esam.EsamCalcMac(Global.gMackey, tmpRet, 8, tmpRetEncrypt);
                        //}
                        //else
                        if (DType == DesType.Des && EnType == EncryptType.Soft)
                        {
                            //DES软加密
                            tmpRetEncrypt = DesEncrypt(tmpRet, softKey);
                            eRet          = Esam.FuncRet.ESAM_SUCC;
                        }
                        else if (DType == DesType.TripleDes && alType == AlgorithmType.CBC)
                        {
                            switch (EnType)
                            {
                            case EncryptType.Hardware:
                                //这里用EsamCalcMac函数代替计算,效果相同
                                eRet = Esam.EsamCalcMac(hardKey, tmpRet, 8, tmpRetEncrypt);
                                break;

                            case EncryptType.Soft:
                                //3DES软加密
                                tmpRetEncrypt = TripleDesEncrypt(tmpRet, softKey);
                                eRet          = Esam.FuncRet.ESAM_SUCC;
                                break;
                            }
                        }
                        else if (DType == DesType.TripleDes && alType == AlgorithmType.X99)
                        {
                            //这里只有软加密一种可能,硬加密前面已处理
                            if (i == len - 8)
                            {
                                //3DES软加密
                                tmpRetEncrypt = TripleDesEncrypt(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.FuncRet.ESAM_SUCC;
                            }
                            else
                            {
                                //DES软加密
                                byte[] tmpKey = new byte[8];
                                Array.Copy(softKey, tmpKey, 8);
                                tmpRetEncrypt = DesEncrypt(tmpRet, tmpKey);
                                eRet          = Esam.FuncRet.ESAM_SUCC;
                            }
                        }

                        if (eRet == Esam.FuncRet.ESAM_SUCC)
                        {
                            Array.Copy(tmpRetEncrypt, tmpRet, 8);
                        }
                        else
                        {
                            return(false);
                        }
                    }
                    Array.Copy(tmpRet, MAC, 8);
                    EnType = tmp;
                    return(true);
                }
            }
            catch (System.Exception e)
            {
                AppLog.Write("[CalcMac_CBC_X99]Error!\n" + e.ToString(), AppLog.LogMessageType.Error, this.GetType());
                return(false);
            }
        }
コード例 #2
0
ファイル: CTransaction.cs プロジェクト: radtek/RLPayment
        private bool CalcMac_ECB(byte[] DATA, byte[] MAC)
        {
            try
            {
                Esam.FuncRet eRet       = Esam.FuncRet.ESAM_FAIL;
                byte[]       xorRet     = CalcMacXor(DATA);
                string       strXor     = 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 (EnType)
                {
                case EncryptType.Hardware:
                    //这里用EsamCalcMac函数代替计算,效果相同
                    eRet = Esam.EsamCalcMac(Global.gMackey, beforeByte, 8, encryptData);
                    break;

                case EncryptType.Soft:
                    switch (DType)
                    {
                    case DesType.Des:
                        //DES软加密
                        encryptData = DesEncrypt(beforeByte, WorkKey);
                        break;

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

                    case EncryptType.Soft:
                        switch (DType)
                        {
                        case DesType.Des:
                            //DES软加密
                            encryptData = DesEncrypt(xorRet, WorkKey);
                            break;

                        case DesType.TripleDes:
                            //3DES软加密
                            encryptData = TripleDesEncrypt(xorRet, WorkKey);
                            break;
                        }
                        eRet = Esam.FuncRet.ESAM_SUCC;
                        break;
                    }
                    #endregion
                    if (eRet == Esam.FuncRet.ESAM_SUCC)
                    {
                        strXor  = 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)
            {
                AppLog.Write("[CalcMac_ECB]Error!\n" + e.ToString(), AppLog.LogMessageType.Error, this.GetType());
                return(false);
            }
        }