private static void ExecuteBlock(Blockchain.Block block)
        {
            if (block == null || !block.IsValid())
            {
                return;
            }
            var dateAndTime   = block.Timestamp;
            var blockData     = block.DataByteArray;
            var version       = blockData[0];
            var type          = (DataType)blockData[1];
            var password      = DecryptPassword(blockData, out var encryptedDataPosition);
            var len           = blockData.Length - encryptedDataPosition;
            var encryptedData = new byte[len];

            Buffer.BlockCopy(blockData, encryptedDataPosition, encryptedData, 0, len);
            var dataElement = Cryptography.Decrypt(encryptedData, password);
            var dataLen     = dataElement.Length - 128;
            var data        = new byte[dataLen];

            Buffer.BlockCopy(dataElement, 0, data, 0, dataLen);
            System.Security.Cryptography.HashAlgorithm hashType = new System.Security.Cryptography.SHA256Managed();
            var hashData        = hashType.ComputeHash(data);
            var signatureOfData = new byte[128];

            Buffer.BlockCopy(dataElement, dataLen, signatureOfData, 0, 128);
            // Find the author
            string author = null;

            foreach (var participant in _participants)
            {
                var rsa = new System.Security.Cryptography.RSACryptoServiceProvider();
                rsa.ImportCspBlob(Convert.FromBase64String(participant));
                if (!rsa.VerifyHash(hashData, System.Security.Cryptography.CryptoConfig.MapNameToOID("SHA256"),
                                    signatureOfData))
                {
                    continue;
                }
                author = participant;
                break;
            }
            //var SignatureOfData = GetMyRSA().SignHash(HashData, System.Security.Cryptography.CryptoConfig.MapNameToOID("SHA256"));
            //var Signatures = Block.GetAllBodySignature();
            //var Author = _Participants.Find(x => Signatures.ContainsKey(x));
            if (author == null)
            {
                System.Diagnostics.Debug.WriteLine("Block written by an impostor");
            }
            else
            {
                var isMy = author == GetMyPublicKey();
                ViewMessage(dateAndTime, type, data, isMy);
            }
        }
 private static void ExecuteBlock(Blockchain.Block Block)
 {
     if (Block != null && Block.IsValid())
     {
         var      DateAndTime   = Block.Timestamp;
         var      BlockData     = Block.DataByteArray;
         byte     Version       = BlockData[0];
         DataType Type          = (DataType)BlockData[1];
         var      Password      = DecryptPassword(BlockData, out int EncryptedDataPosition);
         var      Len           = BlockData.Length - EncryptedDataPosition;
         var      EncryptedData = new byte[Len];
         Buffer.BlockCopy(BlockData, EncryptedDataPosition, EncryptedData, 0, Len);
         var    DataElement = Cryptography.Decrypt(EncryptedData, Password);
         int    DataLen     = DataElement.Length - 128;
         byte[] Data        = new byte[DataLen];
         Buffer.BlockCopy(DataElement, 0, Data, 0, DataLen);
         System.Security.Cryptography.HashAlgorithm hashType = new System.Security.Cryptography.SHA256Managed();
         byte[] HashData        = hashType.ComputeHash(Data);
         byte[] SignatureOfData = new byte[128];
         Buffer.BlockCopy(DataElement, DataLen, SignatureOfData, 0, 128);
         // Find the author
         string Author = null;
         foreach (var Partecipant in _Participants)
         {
             System.Security.Cryptography.RSACryptoServiceProvider RSAalg = new System.Security.Cryptography.RSACryptoServiceProvider();
             RSAalg.ImportCspBlob(Convert.FromBase64String(Partecipant));
             if (RSAalg.VerifyHash(HashData, System.Security.Cryptography.CryptoConfig.MapNameToOID("SHA256"), SignatureOfData))
             {
                 Author = Partecipant;
                 break;
             }
         }
         //var SignatureOfData = GetMyRSA().SignHash(HashData, System.Security.Cryptography.CryptoConfig.MapNameToOID("SHA256"));
         //var Signatures = Block.GetAllBodySignature();
         //var Author = _Participants.Find(x => Signatures.ContainsKey(x));
         if (Author == null)
         {
             System.Diagnostics.Debug.WriteLine("Block written by an impostor");
         }
         else
         {
             var IsMy = Author == GetMyPublicKey();
             ViewMessage(DateAndTime, Type, Data, IsMy);
         }
     }
 }
 private static void SendData(DataType type, byte[] data)
 {
     new System.Threading.Thread(() =>
     {
         try
         {
             // Specifications of the cryptogram data format (Version 0):
             // First byte (version): Indicates the version of the technical specification, if this parameter changes everything in the data package it can follow other specifications
             // Second byte: Indicates the type of data that contains this block: Text, Image, Audio (in the future also new implementations).
             // Global Password: Variable length data that contains the password for each participant in the chat room. The length of this data depends on the number of participants. For this purpose, see the EncryptPasswordForParticipants function. The protocol includes more than 2 participants in a chat room.
             // Encrypted data: This is the real data (message, photo, audio, etc.), encrypted according to an algorithm contained in the Cryptography.Encrypt class. The encryption is made with an xor between the original data and a random data generated with a repetitive hash that starting from the password.
             const byte version    = 0;
             byte[] blockchainData = { version, (byte)type };
             var password          = GeneratePassword();
             var globalPassword    = EncryptPasswordForParticipants(password);
             System.Security.Cryptography.HashAlgorithm hashType = new System.Security.Cryptography.SHA256Managed();
             var hashData        = hashType.ComputeHash(data);
             var signatureOfData = GetMyRsa().SignHash(hashData, System.Security.Cryptography.CryptoConfig.MapNameToOID("SHA256"));
             var encryptedData   = Cryptography.Encrypt(data.Concat(signatureOfData).ToArray(), password);
             blockchainData      = blockchainData.Concat(globalPassword).Concat(encryptedData).ToArray();
             _blockchain.RequestAnyNewBlocks();
             if (blockchainData.Length * 2 + 4096 <= _blockchain.MaxBlockLength)
             {
                 var newBlock      = new Blockchain.Block(_blockchain, blockchainData);
                 var blockPosition = _blockchain.Length();
                 if (!newBlock.IsValid())
                 {
                     System.Diagnostics.Debugger.Break();
                 }
                 _blockchain.SyncBlockToNetwork(newBlock, blockPosition);
                 ViewMessage(newBlock.Timestamp, type, data, true);
             }
             else
             {
                 Functions.Alert(Resources.Dictionary.ExceededBlockSizeLimit);
             }
         }
         catch (Exception ex)
         {
             Functions.Alert(ex.Message);
         }
         _sending = 0;
     }).Start();
 }
 private static void SendData(DataType Type, byte[] Data)
 {
     new System.Threading.Thread(() =>
     {
         try
         {
             const byte Version    = 0;
             byte[] BlockchainData = { Version, (byte)Type };
             var Password          = GeneratePassword();
             var GlobalPassword    = EncryptPasswordForParticipants(Password);
             System.Security.Cryptography.HashAlgorithm hashType = new System.Security.Cryptography.SHA256Managed();
             byte[] HashData     = hashType.ComputeHash(Data);
             var SignatureOfData = GetMyRSA().SignHash(HashData, System.Security.Cryptography.CryptoConfig.MapNameToOID("SHA256"));
             var EncryptedData   = Cryptography.Encrypt(Data.Concat(SignatureOfData).ToArray(), Password);
             BlockchainData      = BlockchainData.Concat(GlobalPassword).Concat(EncryptedData).ToArray();
             Blockchain.RequestAnyNewBlocks();
             if (BlockchainData.Length * 2 + 4096 <= Blockchain.MaxBlockLenght)
             {
                 Blockchain.Block NewBlock = new Blockchain.Block(Blockchain, BlockchainData);
                 var BlockPosition         = Blockchain.Length();
                 if (!NewBlock.IsValid())
                 {
                     System.Diagnostics.Debugger.Break();
                 }
                 Blockchain.SyncBlockToNetwork(NewBlock, BlockPosition);
                 ViewMessage(NewBlock.Timestamp, Type, Data, true);
             }
             else
             {
                 Functions.Alert(Resources.Dictionary.ExceededBlockSizeLimit);
             }
         }
         catch (Exception Ex)
         {
             Functions.Alert(Ex.Message);
         }
         Sending = 0;
     }).Start();
 }