Exemplo n.º 1
0
        private void buttonUploadNow_Click(object sender, EventArgs e)
        {
            try
            {
                if (this.listBoxUploadKeywords.Items.Count == 0)
                {
                    MessageBox.Show("At least one keyword must be associated with the data before it is uploaded");
                    return;
                }
                if (this.keyPair == null)
                {
                    MessageBox.Show("You must load user keys first");
                    return;
                }

                if (this.rolesUserControlUploadData.SelectedRoles.Count == 0)
                {
                    MessageBox.Show("You must select at least one role which should have access to the uploaded data");
                    return;
                }

                byte[] fileContent = File.ReadAllBytes(this.labelUploadData.Text);

                AesEncryptionInfo encryptionInfo = SymmetricEncryptor.GenerateSymmetricKeyInfo();

                byte[] fileCiphertext = SymmetricEncryptor.Encrypt(fileContent, encryptionInfo);

                IPreService preProxy = CreatePreProxy();
                byte[]      encSymIv = preProxy.Encrypt(this.keyPair.Public, encryptionInfo.IV);

                preProxy = CreatePreProxy();
                byte[] encSymKey = preProxy.Encrypt(this.keyPair.Public, encryptionInfo.Key);

                byte[] name = SymmetricEncryptor.Encrypt(Path.GetFileName(this.labelUploadData.Text).GetBytes(), encryptionInfo);

                DataEntity entity = new DataEntity();
                entity.Attributes = CollectAndEncryptAttributes(encryptionInfo);
                entity.Payload    = new FilePayload(name, fileCiphertext);
                entity.AesInfo    = new AesEncryptionInfo(encSymKey, encSymIv);
                entity.Id         = Guid.NewGuid();

                entity.Signature = DataSigner.Sign(entity, this.signingKeys);

                IGatewayService proxy = CreateServiceProxy();

                proxy.CreateDataEntities(this.myId, this.rolesUserControlUploadData.SelectedRoles, new[] { entity });

                MessageBox.Show("Done uploading");
            }
            catch (Exception ex)
            {
                MessageBox.Show("Error: " + ex.Message);
                Logger.LogError("Error preparing and uploading data to server", ex);
            }
        }
Exemplo n.º 2
0
        private IList <Attribute> CollectAndEncryptAttributes(AesEncryptionInfo encryptionInfo)
        {
            IList <Attribute> attributes = new List <Attribute>();

            foreach (string s in this.listBoxUploadKeywords.Items)
            {
                byte[] att = SymmetricEncryptor.Encrypt(s.GetBytes(), encryptionInfo);
                attributes.Add(new Attribute(GuidCreator.CreateGuidFromString(s), att));
            }

            return(attributes);
        }
Exemplo n.º 3
0
        public static byte[] Encrypt(byte[] plaintext, AesEncryptionInfo info)
        {
            if (info == null)
            {
                throw new ArgumentNullException("info");
            }
            if (plaintext == null || plaintext.Length <= 0)
            {
                throw new ArgumentNullException("plaintext");
            }
            if (info.Key == null || info.Key.Length <= 0)
            {
                throw new ArgumentNullException("key");
            }
            if (info.IV == null || info.IV.Length <= 0)
            {
                throw new ArgumentNullException("iv");
            }

            MemoryStream memoryStream;
            AesManaged   aesAlg = null;

            try
            {
                // Create the encryption algorithm object with the specified key and IV.
                aesAlg     = new AesManaged();
                aesAlg.Key = info.Key;
                aesAlg.IV  = info.IV;

                // Create an encryptor to perform the stream transform.
                var encryptor = aesAlg.CreateEncryptor(aesAlg.Key, aesAlg.IV);

                // Create the streams used for encryption.
                memoryStream = new MemoryStream();

                using (var cryptoStream = new CryptoStream(memoryStream, encryptor, CryptoStreamMode.Write))
                {
                    cryptoStream.Write(plaintext, 0, plaintext.Length);
                }
            }
            finally
            {
                if (aesAlg != null)
                {
                    aesAlg.Clear();
                }
            }

            // Return the encrypted bytes from the memory stream.
            return(memoryStream.ToArray());
        }
Exemplo n.º 4
0
        private void buttonUploadNow_Click(object sender, EventArgs e)
        {
            try
            {
                if (this.listBoxUploadKeywords.Items.Count == 0)
                {
                    MessageBox.Show("At least one keyword must be associated with the data before it is uploaded");
                    return;
                }
                if (!this.userkeysLoaded)
                {
                    MessageBox.Show("You must load user keys first");
                    return;
                }

                byte[] fileContent = File.ReadAllBytes(this.labelUploadData.Text);

                AesEncryptionInfo encryptionInfo = SymmetricEncryptor.GenerateSymmetricKeyInfo();

                byte[] fileCiphertext = SymmetricEncryptor.Encrypt(fileContent, encryptionInfo);

                IPreService preProxy = CreatePreProxy();
                byte[]      encSymIv = preProxy.Encrypt(this.masterPublicKey, encryptionInfo.IV);

                preProxy = CreatePreProxy();
                byte[] encSymKey = preProxy.Encrypt(this.masterPublicKey, encryptionInfo.Key);

                byte[] name = SymmetricEncryptor.Encrypt(Path.GetFileName(this.labelUploadData.Text).GetBytes(), encryptionInfo);

                DataEntity entity = new DataEntity();
                entity.Attributes = CollectAndEncryptAttributes(encryptionInfo);
                entity.Payload    = new FilePayload(name, fileCiphertext);
                entity.AesInfo    = new AesEncryptionInfo(encSymKey, encSymIv);
                entity.Id         = Guid.NewGuid(); // perhaps base guid on the file path??

                entity.Signature = DataSigner.Sign(entity, this.userSignKeys);

                IGatewayService proxy = CreateServiceProxy();

                proxy.InsertData(GetUserIdentity(), entity);

                MessageBox.Show("Done uploading");
            }
            catch (Exception ex)
            {
                MessageBox.Show("Error: " + ex.Message);
                Logger.LogError("Error preparing and uploading data to server", ex);
            }
        }
Exemplo n.º 5
0
        public static byte[] Decrypt(byte[] ciphertext, AesEncryptionInfo info)
        {
            if (info == null)
            {
                throw new ArgumentNullException("info");
            }
            if (ciphertext == null || ciphertext.Length <= 0)
            {
                throw new ArgumentNullException("plainText");
            }
            if (info.Key == null || info.Key.Length <= 0)
            {
                throw new ArgumentNullException("key");
            }
            if (info.IV == null || info.IV.Length <= 0)
            {
                throw new ArgumentNullException("iv");
            }

            AesManaged aesAlg = null;

            try
            {
                // Create a the encryption algorithm object with the specified key and IV.
                aesAlg     = new AesManaged();
                aesAlg.Key = info.Key;
                aesAlg.IV  = info.IV;

                // Create a decrytor to perform the stream transform.
                var decryptor = aesAlg.CreateDecryptor(aesAlg.Key, aesAlg.IV);

                // Create the streams used for decryption.
                using (var memoryStream = new MemoryStream(ciphertext))
                    using (var cryptoStream = new CryptoStream(memoryStream, decryptor, CryptoStreamMode.Read))
                    {
                        int len = cryptoStream.Read(ciphertext, 0, ciphertext.Length);
                        return(ciphertext.RangeSubset(0, len));
                    }
            }
            finally
            {
                if (aesAlg != null)
                {
                    aesAlg.Clear();
                }
            }
        }