Exemplo n.º 1
0
        public static byte[] Encrypt(IExchangeEncrypt exchangeEncrypt, byte[] value)
        {
            if (exchangeEncrypt.ExchangeAlgorithm == ExchangeAlgorithm.Rsa2048)
            {
                return(Rsa2048.Encrypt(exchangeEncrypt.PublicKey, value));
            }

            return(null);
        }
Exemplo n.º 2
0
        public static ArraySegment <byte> ToSignatureMessageBlock(SignatureMessage message, IExchangeEncrypt publicKey)
        {
            if (message == null)
            {
                throw new ArgumentNullException("message");
            }
            if (publicKey == null)
            {
                throw new ArgumentNullException("publicKey");
            }

            ArraySegment <byte> value;

            using (Stream messageStream = message.Export(_bufferManager))
                using (Stream compressStream = ContentConverter.Compress(messageStream))
                    using (Stream paddingStream = ContentConverter.AddPadding(compressStream, 1024 * 256))
                        using (Stream hashStream = ContentConverter.AddHash(paddingStream))
                            using (Stream cryptostream = ContentConverter.Encrypt(hashStream, publicKey))
                                using (Stream typeStream = ContentConverter.AddType(cryptostream, "SignatureMessage"))
                                {
                                    value = new ArraySegment <byte>(_bufferManager.TakeBuffer((int)typeStream.Length), 0, (int)typeStream.Length);
                                    typeStream.Read(value.Array, value.Offset, value.Count);
                                }

            return(value);
        }
Exemplo n.º 3
0
        private static Stream Encrypt(Stream stream, IExchangeEncrypt publicKey)
        {
            if (stream == null) throw new ArgumentNullException("stream");
            if (publicKey == null) throw new ArgumentNullException("publicKey");

            try
            {
                BufferStream outStream = null;

                try
                {
                    outStream = new BufferStream(_bufferManager);
                    outStream.WriteByte((byte)ConvertCryptoAlgorithm.Aes256);

                    byte[] cryptoKey = new byte[32];
                    _random.GetBytes(cryptoKey);

                    {
                        var encryptedBuffer = Exchange.Encrypt(publicKey, cryptoKey);
                        outStream.Write(NetworkConverter.GetBytes((int)encryptedBuffer.Length), 0, 4);
                        outStream.Write(encryptedBuffer, 0, encryptedBuffer.Length);
                    }

                    byte[] iv = new byte[32];
                    _random.GetBytes(iv);
                    outStream.Write(iv, 0, iv.Length);

                    using (Stream inStream = new WrapperStream(stream, true))
                    {
                        using (var rijndael = new RijndaelManaged() { KeySize = 256, BlockSize = 256, Mode = CipherMode.CBC, Padding = PaddingMode.PKCS7 })
                        using (CryptoStream cs = new CryptoStream(inStream, rijndael.CreateEncryptor(cryptoKey, iv), CryptoStreamMode.Read))
                        {
                            byte[] buffer = null;

                            try
                            {
                                buffer = _bufferManager.TakeBuffer(1024 * 4);

                                int i = -1;

                                while ((i = cs.Read(buffer, 0, buffer.Length)) > 0)
                                {
                                    outStream.Write(buffer, 0, i);
                                }
                            }
                            finally
                            {
                                if (buffer != null)
                                {
                                    _bufferManager.ReturnBuffer(buffer);
                                }
                            }
                        }
                    }

                    outStream.Seek(0, SeekOrigin.Begin);
                }
                catch (Exception)
                {
                    if (outStream != null)
                    {
                        outStream.Dispose();
                    }

                    throw;
                }

                return outStream;
            }
            catch (Exception e)
            {
                throw new ArgumentException(e.Message, e);
            }
        }
Exemplo n.º 4
0
        private static Stream Encrypt(Stream stream, IExchangeEncrypt publicKey)
        {
            if (stream == null)
            {
                throw new ArgumentNullException("stream");
            }
            if (publicKey == null)
            {
                throw new ArgumentNullException("publicKey");
            }

            try
            {
                BufferStream outStream = null;

                try
                {
                    outStream = new BufferStream(_bufferManager);
                    outStream.WriteByte((byte)ConvertCryptoAlgorithm.Aes256);

                    byte[] cryptoKey = new byte[32];
                    _random.GetBytes(cryptoKey);

                    {
                        var encryptedBuffer = Exchange.Encrypt(publicKey, cryptoKey);
                        outStream.Write(NetworkConverter.GetBytes((int)encryptedBuffer.Length), 0, 4);
                        outStream.Write(encryptedBuffer, 0, encryptedBuffer.Length);
                    }

                    byte[] iv = new byte[32];
                    _random.GetBytes(iv);
                    outStream.Write(iv, 0, iv.Length);

                    using (Stream inStream = new WrapperStream(stream, true))
                    {
                        using (var rijndael = new RijndaelManaged()
                        {
                            KeySize = 256, BlockSize = 256, Mode = CipherMode.CBC, Padding = PaddingMode.PKCS7
                        })
                            using (CryptoStream cs = new CryptoStream(inStream, rijndael.CreateEncryptor(cryptoKey, iv), CryptoStreamMode.Read))
                            {
                                byte[] buffer = null;

                                try
                                {
                                    buffer = _bufferManager.TakeBuffer(1024 * 4);

                                    int i = -1;

                                    while ((i = cs.Read(buffer, 0, buffer.Length)) > 0)
                                    {
                                        outStream.Write(buffer, 0, i);
                                    }
                                }
                                finally
                                {
                                    if (buffer != null)
                                    {
                                        _bufferManager.ReturnBuffer(buffer);
                                    }
                                }
                            }
                    }

                    outStream.Seek(0, SeekOrigin.Begin);
                }
                catch (Exception)
                {
                    if (outStream != null)
                    {
                        outStream.Dispose();
                    }

                    throw;
                }

                return(outStream);
            }
            catch (Exception e)
            {
                throw new ArgumentException(e.Message, e);
            }
        }
Exemplo n.º 5
0
        public static ArraySegment<byte> ToSignatureMessageBlock(SignatureMessage package, IExchangeEncrypt publicKey)
        {
            if (package == null) throw new ArgumentNullException("package");
            if (publicKey == null) throw new ArgumentNullException("publicKey");

            ArraySegment<byte> value;

            using (Stream packageStream = package.Export(_bufferManager))
            using (Stream compressStream = ContentConverter.Compress(packageStream))
            using (Stream paddingStream = ContentConverter.AddPadding(compressStream, 1024 * 256))
            using (Stream hashStream = ContentConverter.AddHash(paddingStream))
            using (Stream cryptostream = ContentConverter.Encrypt(hashStream, publicKey))
            using (Stream typeStream = ContentConverter.AddType(cryptostream, "SignatureMessage"))
            {
                value = new ArraySegment<byte>(_bufferManager.TakeBuffer((int)typeStream.Length), 0, (int)typeStream.Length);
                typeStream.Read(value.Array, value.Offset, value.Count);
            }

            return value;
        }