示例#1
0
        public static byte[] Decrypt(byte[] payload, CryptoContext cryptoContext)
        {
            byte[] checksum;
            byte[] clearBytes;

            using (MemoryStream clearBuffer = new MemoryStream())
            {
                //if (Log.IsDebugEnabled)
                //	Log.Debug($"Full payload\n{Package.HexDump(payload)}");

                var input = cryptoContext.InputStream;
                var csDecrypt = cryptoContext.CryptoStreamIn;

                input.Position = 0;
                input.SetLength(0);
                input.Write(payload, 0, payload.Length);
                input.Position = 0;

                var buffer = new byte[payload.Length];
                var read = csDecrypt.Read(buffer, 0, buffer.Length);
                if (read <= 0) Log.Warn("Read 0 lenght from crypto stream");
                clearBuffer.Write(buffer, 0, read);
                csDecrypt.Flush();

                var fullResult = clearBuffer.ToArray();

                //if (Log.IsDebugEnabled)
                //	Log.Debug($"Full content\n{Package.HexDump(fullResult)}");

                clearBytes = (byte[]) fullResult.Take(fullResult.Length - 8).ToArray();
                checksum = fullResult.Skip(fullResult.Length - 8).ToArray();
            }

            return clearBytes;
        }
示例#2
0
        public static byte[] Encrypt(byte[] payload, CryptoContext cryptoContext)
        {
            var csEncrypt = cryptoContext.CryptoStreamOut;
            var output    = cryptoContext.OutputStream;

            output.Position = 0;
            output.SetLength(0);

            using (MemoryStream hashStream = new MemoryStream())
            {
                // hash

                SHA256Managed crypt = new SHA256Managed();

                hashStream.Write(BitConverter.GetBytes(Interlocked.Increment(ref cryptoContext.SendCounter)), 0, 8);
                hashStream.Write(payload, 0, payload.Length);
                hashStream.Write(cryptoContext.Algorithm.Key, 0, cryptoContext.Algorithm.Key.Length);
                var hashBuffer = hashStream.ToArray();

                byte[] validationCheckSum = crypt.ComputeHash(hashBuffer, 0, hashBuffer.Length);

                byte[] content = payload.Concat(validationCheckSum.Take(8)).ToArray();

                csEncrypt.Write(content, 0, content.Length);
                csEncrypt.Flush();
            }

            return(output.ToArray());
        }
示例#3
0
        public static byte[] Encrypt(byte[] payload, CryptoContext cryptoContext)
        {
            var csEncrypt = cryptoContext.CryptoStreamOut;
            var output = cryptoContext.OutputStream;
            output.Position = 0;
            output.SetLength(0);

            using (MemoryStream hashStream = new MemoryStream())
            {
                // hash

                SHA256Managed crypt = new SHA256Managed();

                hashStream.Write(BitConverter.GetBytes(Interlocked.Increment(ref cryptoContext.SendCounter)), 0, 8);
                hashStream.Write(payload, 0, payload.Length);
                hashStream.Write(cryptoContext.Algorithm.Key, 0, cryptoContext.Algorithm.Key.Length);
                var hashBuffer = hashStream.ToArray();

                byte[] validationCheckSum = crypt.ComputeHash(hashBuffer, 0, hashBuffer.Length);

                byte[] content = payload.Concat(validationCheckSum.Take(8)).ToArray();

                csEncrypt.Write(content, 0, content.Length);
                csEncrypt.Flush();
            }

            return output.ToArray();
        }
示例#4
0
        public static byte[] Encrypt(Memory <byte> payload, CryptoContext cryptoContext)
        {
            using (MemoryStream hashStream = new MemoryStream())
            {
                // hash

                SHA256Managed crypt = new SHA256Managed();

                hashStream.Write(BitConverter.GetBytes(Interlocked.Increment(ref cryptoContext.SendCounter)), 0, 8);
                hashStream.Write(payload.Span);
                hashStream.Write(cryptoContext.Key, 0, cryptoContext.Key.Length);
                var hashBuffer = hashStream.ToArray();

                Span <byte> validationCheckSum = crypt.ComputeHash(hashBuffer, 0, hashBuffer.Length);

                Span <byte> clear = stackalloc byte[payload.Length + 8];
                payload.Span.CopyTo(clear);
                validationCheckSum.Slice(0, 8).CopyTo(clear.Slice(payload.Length));

                var cipher = cryptoContext.Encryptor;

                byte[] encrypted = new byte[clear.Length];
                int    length    = cipher.ProcessBytes(clear.ToArray(), encrypted, 0);
                //cipher.DoFinal(outputBytes, length); //Do the final block

                return(encrypted);
            }
        }
示例#5
0
        public static ReadOnlyMemory <byte> Decrypt(ReadOnlyMemory <byte> payload, CryptoContext cryptoContext)
        {
            IBufferedCipher       cipher = cryptoContext.Decryptor;
            ReadOnlyMemory <byte> clear  = cipher.ProcessBytes(payload.ToArray());

            //TODO: Verify hash!
            return(clear.Slice(0, clear.Length - 8));
        }
示例#6
0
        public static byte[] Decrypt(byte[] payload, CryptoContext cryptoContext)
        {
            IBufferedCipher cipher = cryptoContext.Decryptor;

            byte[] clear = cipher.ProcessBytes(payload);
            //TODO: Verify hash!
            return(clear.AsSpan(0, clear.Length - 8).ToArray());
        }
示例#7
0
        public static byte[] Decrypt(byte[] payload, CryptoContext cryptoContext)
        {
            //byte[] checksum;
            byte[] clearBytes;

            {
                var    cipher = cryptoContext.Decryptor;
                byte[] clear  = new byte[payload.Length];
                int    length = cipher.ProcessBytes(payload, clear, 0);
                //cipher.DoFinal(comparisonBytes, length); //Do the final block

                clearBytes = (byte[])clear.Take(clear.Length - 8).ToArray();
                //checksum = fullResult.Skip(fullResult.Length - 8).ToArray();
            }

            return(clearBytes);
        }
示例#8
0
        public static byte[] Decrypt(byte[] payload, CryptoContext cryptoContext)
        {
            byte[] checksum;
            byte[] clearBytes;

            using (MemoryStream clearBuffer = new MemoryStream())
            {
                //if (Log.IsDebugEnabled)
                //	Log.Debug($"Full payload\n{Package.HexDump(payload)}");

                var input     = cryptoContext.InputStream;
                var csDecrypt = cryptoContext.CryptoStreamIn;

                input.Position = 0;
                input.SetLength(0);
                input.Write(payload, 0, payload.Length);
                input.Position = 0;

                var buffer = new byte[payload.Length];
                var read   = csDecrypt.Read(buffer, 0, buffer.Length);
                if (read <= 0)
                {
                    Log.Warn("Read 0 lenght from crypto stream");
                }
                clearBuffer.Write(buffer, 0, read);
                csDecrypt.Flush();

                var fullResult = clearBuffer.ToArray();

                //if (Log.IsDebugEnabled)
                //	Log.Debug($"Full content\n{Package.HexDump(fullResult)}");

                clearBytes = (byte[])fullResult.Take(fullResult.Length - 8).ToArray();
                checksum   = fullResult.Skip(fullResult.Length - 8).ToArray();
            }

            return(clearBytes);
        }
示例#9
0
        public static byte[] Encrypt(Memory <byte> payload, CryptoContext cryptoContext)
        {
            // hash
            int hashPoolLen      = 8 + payload.Length + cryptoContext.Key.Length;
            var hashBufferPooled = ArrayPool <byte> .Shared.Rent(hashPoolLen);

            Span <byte> hashBuffer = hashBufferPooled.AsSpan();

            BitConverter.GetBytes(Interlocked.Increment(ref cryptoContext.SendCounter)).CopyTo(hashBuffer.Slice(0, 8));
            payload.Span.CopyTo(hashBuffer.Slice(8));
            cryptoContext.Key.CopyTo(hashBuffer.Slice(8 + payload.Length));
            using var hasher = new SHA256Managed();
            Span <byte> validationCheckSum = hasher.ComputeHash(hashBufferPooled, 0, hashPoolLen).AsSpan(0, 8);

            ArrayPool <byte> .Shared.Return(hashBufferPooled);

            IBufferedCipher cipher    = cryptoContext.Encryptor;
            var             encrypted = new byte[payload.Length + 8];
            int             length    = cipher.ProcessBytes(payload.ToArray(), encrypted, 0);

            cipher.ProcessBytes(validationCheckSum.ToArray(), encrypted, length);

            return(encrypted);
        }