public async Task Init(byte[] recipientId, byte[] privateKey)
        {
            const int contentInfoHeaderSize = 16;
            var       contentInfoHeader     = new byte[contentInfoHeaderSize];

            await this.sourceStream.ReadAsync(contentInfoHeader, 0, contentInfoHeaderSize);

            int infoSize = (int)VirgilCipherBase.DefineContentInfoSize(contentInfoHeader);

            if (infoSize == 0 || infoSize < contentInfoHeaderSize)
            {
                throw new VirgilException("Encrypted file does not contain embedded content info.");
            }

            var contentInfo = new byte[infoSize];

            await this.sourceStream.ReadAsync(contentInfo, contentInfoHeaderSize, infoSize - contentInfoHeaderSize);

            Array.Copy(contentInfoHeader, contentInfo, contentInfoHeaderSize);

            this.virgilCipher.SetContentInfo(contentInfo);

            this.chunkSize = (int)this.virgilCipher.StartDecryptionWithKey(recipientId, privateKey);
            this.buffer    = new byte[this.chunkSize];

            this.hashOrigianl.Start();
            this.hashEncrypted.Start();
        }
Exemple #2
0
        public async Task Init(byte[] recipientId, byte[] privateKey, string password)
        {
            this.recipientId = recipientId;
            this.privateKey  = privateKey;
            this.password    = password;

            const int contentInfoHeaderSize = 16;
            var       contentInfoHeader     = new byte[contentInfoHeaderSize];

            await this.sourceStream.ReadAsync(contentInfoHeader, 0, contentInfoHeaderSize);

            int infoSize = (int)VirgilCipherBase.DefineContentInfoSize(contentInfoHeader);

            if (infoSize == 0 || infoSize < contentInfoHeaderSize)
            {
                throw new VirgilException("Encrypted file does not contain embedded content info.");
            }

            var contentInfo = new byte[infoSize];

            await this.sourceStream.ReadAsync(contentInfo, contentInfoHeaderSize, infoSize - contentInfoHeaderSize);

            Array.Copy(contentInfoHeader, contentInfo, contentInfoHeaderSize);

            this.virgilCipher.SetContentInfo(contentInfo);

            if (!string.IsNullOrEmpty(password))
            {
                this.chunkSize = (int)this.virgilCipher.StartDecryptionWithKey(
                    recipientId, privateKey,
                    Encoding.UTF8.GetBytes(password));
            }
            else
            {
                this.chunkSize = (int)this.virgilCipher.StartDecryptionWithKey(recipientId, privateKey);
            }

            this.buffer = new byte[this.chunkSize];
        }
Exemple #3
0
        public byte[] Decrypt(byte[] cipherData, string privateKeyPassword = null)
        {
            using (var cipher = new VirgilCipher())
            {
                var contentInfoSize = VirgilCipherBase.DefineContentInfoSize(cipherData);
                if (contentInfoSize == 0)
                {
                    throw new ArgumentException("Content info header is missing or corrupted", nameof(cipherData));
                }

                byte[] result;
                if (privateKeyPassword != null)
                {
                    result = cipher.DecryptWithKey(cipherData, this.GetRecepientId(), this.PrivateKey.Data,
                                                   privateKeyPassword.GetBytes());
                }
                else
                {
                    result = cipher.DecryptWithKey(cipherData, this.GetRecepientId(), this.PrivateKey.Data);
                }

                return(result);
            }
        }