Beispiel #1
0
        /// <summary>
        /// Unpacks the response from bytes.
        /// </summary>
        /// <param name="requestBytes">The request bytes.</param>
        /// <param name="rsaProvider">The RSA provider.</param>
        /// <param name="aesProvider">The aes provider.</param>
        /// <returns></returns>
        public static VirtualSecuredResponseRawMessage UnpackResponseFromBytes(this byte[] requestBytes, RSACryptoServiceProvider rsaProvider, RijndaelProvider aesProvider)
        {
            // Byte[] composition: [Schema Version]{1}[UTC Stamp]{4}[Encrypted Body]{N}.

            try
            {
                rsaProvider.CheckNullObject(nameof(rsaProvider));
                aesProvider.CheckNullObject(nameof(aesProvider));

                requestBytes.CheckNullOrEmptyCollection(nameof(requestBytes));

                var result = new VirtualSecuredResponseRawMessage
                {
                    // Index1
                    SchemaVersion = Convert.ToInt32(requestBytes[0])
                };

                var currentIndex = 1;
                // Index2
                var stampBytes = requestBytes.Read(_stampIndicationByteLength, ref currentIndex);
                result.Stamp = GetUtcStampFromOffsetBytes(stampBytes);

                ValidateStamp(result.Stamp);

                // Index5
                var dataBytes = requestBytes.SubArray(currentIndex);
                result.Data = aesProvider.DecryptAes(dataBytes);

                return(result);
            }
            catch (Exception ex)
            {
                throw ex.Handle();
            }
        }
Beispiel #2
0
        /// <summary>
        /// Packs to bytes.
        /// </summary>
        /// <param name="responseMessage">The response message.</param>
        /// <param name="rsaProvider">The RSA provider.</param>
        /// <param name="aesProvider">The aes provider.</param>
        /// <returns></returns>
        public static byte[] PackToBytes(this VirtualSecuredResponseRawMessage responseMessage, RSACryptoServiceProvider rsaProvider, RijndaelProvider aesProvider)
        {
            // Byte[] composition: [Schema Version]{1}[UTC Stamp]{4}[Encrypted Body]{N}.

            try
            {
                rsaProvider.CheckNullObject(nameof(rsaProvider));
                aesProvider.CheckNullObject(nameof(aesProvider));

                responseMessage.CheckNullObject(nameof(responseMessage));

                responseMessage.SchemaVersion = _schemaVersion;

                List <byte> result = new List <byte>(_packInitialCapacity)
                {
                    // index1: version.
                    Convert.ToByte(responseMessage.SchemaVersion)
                };

                // index2: Stamp.
                result.AddRange(GetStampBytes(responseMessage.Stamp ?? DateTime.UtcNow));

                // index3: Encrypted content
                result.AddRange(aesProvider.EncryptAes(responseMessage.Data));

                return(result.ToArray());
            }
            catch (Exception ex)
            {
                throw ex.Handle();
            }
        }