GetBytesFromOffset() public static method

public static GetBytesFromOffset ( byte bytesArray, int offset, int len ) : byte[]
bytesArray byte
offset int
len int
return byte[]
Exemplo n.º 1
0
        internal static RSAItems InterpretRSA(byte[] identityBytes)
        {
            int      index    = 0;
            RSAItems rsaItems = new RSAItems();

            rsaItems.DevInfo = new DeviceInfo();

            try
            {
                int    len       = MessageBuilder.GetLength(identityBytes, index, out index);
                int    tagLen    = 0;
                String tagValStr = "";
                while (true)
                {
                    if (index > (len - GMPConstants.LEN_DATA_TAG))
                    {
                        break;
                    }
                    //get next tag
                    int tag    = MessageBuilder.GetTag(identityBytes, index, out index);
                    int tagVal = 0;
                    switch (tag)
                    {
                    case GMPCommonTags.TAG_SEQUNCE:
                        tagLen             = MessageBuilder.GetLength(identityBytes, index, out index);
                        tagVal             = MessageBuilder.ConvertBcdToInt(identityBytes, index, tagLen);
                        index             += tagLen;
                        rsaItems.SeqNumber = tagVal;
                        break;

                    case GMPDataTags.DT_MOD_KEY:
                        tagLen           = MessageBuilder.GetLength(identityBytes, index, out index);
                        rsaItems.Modulus = MessageBuilder.GetBytesFromOffset(identityBytes, index, tagLen);
                        index           += tagLen;
                        break;

                    case GMPDataTags.DT_EXP_KEY:
                        tagLen            = MessageBuilder.GetLength(identityBytes, index, out index);
                        rsaItems.Exponent = MessageBuilder.GetBytesFromOffset(identityBytes, index, tagLen);
                        index            += tagLen;
                        break;

                    case GMPDataTags.DT_ECR_BRAND:
                        tagLen    = MessageBuilder.GetLength(identityBytes, index, out index);
                        tagValStr = MessageBuilder.ByteArrayToString(identityBytes, index, tagLen);
                        index    += tagLen;
                        rsaItems.DevInfo.Brand = tagValStr;
                        break;

                    case GMPDataTags.DT_ECR_MODEL:
                        tagLen    = MessageBuilder.GetLength(identityBytes, index, out index);
                        tagValStr = MessageBuilder.ByteArrayToString(identityBytes, index, tagLen);
                        index    += tagLen;
                        rsaItems.DevInfo.Model = tagValStr;
                        break;

                    case GMPCommonTags.TAG_RESP_CODE:
                        tagLen    = MessageBuilder.GetLength(identityBytes, index, out index);
                        tagValStr = MessageBuilder.ByteArrayToString(identityBytes, index, tagLen);
                        index    += tagLen;
                        if (!tagValStr.Equals(GMPResCodes.SUCCESS))
                        {
                            throw new Exception("İşlem başarısız");
                        }
                        break;

                    case GMPDataTags.DT_ECR_SERIAL:
                        tagLen    = MessageBuilder.GetLength(identityBytes, index, out index);
                        tagValStr = MessageBuilder.ByteArrayToString(identityBytes, index, tagLen);
                        index    += tagLen;
                        rsaItems.DevInfo.TerminalNo = tagValStr;
                        break;

                    default:
                        tagLen = MessageBuilder.GetLength(identityBytes, index, out index);
                        index += tagLen;
                        break;
                    }
                }
            }
            catch { rsaItems.SeqNumber = -1; }

            //if no response code
            //rsaItems.SeqNumber = -1;
            return(rsaItems);
        }
Exemplo n.º 2
0
        public FPUResponse(byte[] bytesRead)
        {
            int index   = 0;
            int msgType = 0;

            try
            {
                byte lrc = 0;
                for (int i = 0; i < (bytesRead.Length - 1); i++)
                {
                    lrc ^= bytesRead[i];
                }
                if (bytesRead[bytesRead.Length - 1] != lrc)
                {
                    // Throw CRCException
                    throw new Exception("LRC not match");
                }

                index = 0;
                //TERMINAL SERIAL
                List <byte> serial = new List <byte>();
                for (int i = 0; i < GMPConstants.LEN_SERIAL; i++)
                {
                    serial.Add(bytesRead[index + i]);
                }
                this.fiscalId = Encoding.ASCII.GetString(serial.ToArray());
                index        += GMPConstants.LEN_SERIAL;

                //MESSAGE TYPE
                msgType = MessageBuilder.ByteArrayToHex(bytesRead, index, 3);


                /* get message len */
                index += 3;

                if (msgType == Identifier.KEY_RESPONSE)
                {
                    int tempIndex = 0;
                    int len       = MessageBuilder.GetLength(bytesRead, index, out tempIndex);
                    data = MessageBuilder.GetBytesFromOffset(bytesRead, index, len + tempIndex - index);
                    return;
                }

                if (msgType != RESPONSE_MSG_ID)
                {
                    if (msgType != Identifier.TRIPLEDES_RESPONSE)
                    {
                        throw new InvalidOperationException("Response Message Incorrect");
                    }
                }
            }
            catch (InvalidOperationException)
            {
                throw new Exception("Message id error");
            }
            catch (Exception ex)
            {
                throw new Exception("Invalid data");
            }
            try
            {
                int len = MessageBuilder.GetLength(bytesRead, index, out index);
                if (MainForm.TripleKey != null && msgType == RESPONSE_MSG_ID)
                {
                    byte[] msgBuffer = MessageBuilder.GetBytesFromOffset(bytesRead, index, len);
                    byte[] desData   = MessageBuilder.DecryptTriple(msgBuffer,
                                                                    msgBuffer.Length,
                                                                    MainForm.TripleKey);
                    bytesRead = desData;
                    index     = 0;
                    len       = bytesRead.Length;
                }
                int msgOffset = index;
                int lastIndex = index;
                while (index < len + msgOffset)
                {
                    //get next tag
                    lastIndex = index;
                    int tag = MessageBuilder.GetTag(bytesRead, index, out index);

                    switch (tag)
                    {
                    case GMPCommonTags.TAG_SEQUNCE:
                        int lenSeq = MessageBuilder.GetLength(bytesRead, index, out index);
                        int seq    = MessageBuilder.ConvertBcdToInt(bytesRead, index, lenSeq);
                        index      += lenSeq;
                        this.seqNum = seq;
                        break;

                    case FPUDataTags.ERROR:
                        int lenErrorTag = MessageBuilder.GetLength(bytesRead, index, out index);
                        this.errCode = bytesRead[index];    // MessageBuilder.ConvertBcdToInt(bytesRead, index, lenErrorTag);
                        index       += lenErrorTag;
                        break;

                    case FPUDataTags.STATE:
                        int lenStateTag = MessageBuilder.GetLength(bytesRead, index, out index);
                        int state       = MessageBuilder.ConvertBcdToInt(bytesRead, index, lenStateTag);
                        index        += lenStateTag;
                        this.fpuState = (State)state;
                        break;

                    case FPUDataTags.ENDOFMSG:
                        int lenEndMsg = MessageBuilder.GetLength(bytesRead, index, out index);
                        index = len;
                        break;

                    default:
                        int tagLenValue = MessageBuilder.GetLength(bytesRead, index, out index);
                        int buffIndx    = 0;
                        int counter     = 0;
                        int tagIdSize   = (index - lastIndex);
                        if (data != null && data.Length > 0)
                        {
                            buffIndx = data.Length;
                            int buffSize = data.Length + tagLenValue + tagIdSize;
                            Array.Resize(ref data, buffSize);
                        }
                        else
                        {
                            data = new byte[tagLenValue + tagIdSize];
                        }
                        while (buffIndx < data.Length)
                        {
                            data[buffIndx] = bytesRead[counter + lastIndex];
                            counter++;
                            buffIndx++;
                        }
                        index += tagLenValue;
                        break;
                    }
                }
            }
            catch (Exception ex)
            {
                throw new Exception("Invalid Data");
            }

            if (errCode == 0)
            {
                if (this.SequenceNum < 0)
                {
                    throw new Exception("Invalid Data");
                }
            }
        }