Ejemplo n.º 1
0
        private void FinishEncryption()
        {
            TEncryptionParameters EncParams = GetEncryptionParams(Protection);
            AesManaged            EncEngine = TEncryptionUtils.CreateEngine(EncParams);

            TAgileEncryptionKey DataKey = TAgileEncryptionKey.CreateForWriting(null, EncEngine.KeySize / 8);

            DataKey.Key = TEncryptionUtils.GetRandom(DataKey.KeySizeInBytes);

            TAgileEncryptionKey KeyKey = TAgileEncryptionKey.CreateForWriting(Protection.OpenPassword, EncEngine.KeySize / 8);

            byte[] WorkLen = new byte[8];
            BitOps.SetCardinal(WorkLen, 0, WorkingStream.Length - WorkStreamZeroPos);

            EncryptStream(EncEngine, DataKey, WorkLen);

            using (MemoryStream ms = new MemoryStream())
            {
                using (TOle2File Ole2File = new TOle2File(GetEmptyEncryptedFile()))
                {
                    Ole2File.PrepareForWrite(ms, XlsxConsts.EncryptionInfoString, new string[0]);
                    CreateInfoStream(Ole2File, EncEngine, EncParams, KeyKey, DataKey);
                }

                ms.Position = 0;

                using (TOle2File Ole2File = new TOle2File(ms))
                {
                    Ole2File.PrepareForWrite(TargetStream, XlsxConsts.ContentString, new string[0]);
                    WorkingStream.Position = 0;
                    TEncryptionUtils.CopyStream(WorkingStream, Ole2File);
                }
            }
        }
Ejemplo n.º 2
0
        public static TAgileEncryptionKey CreateForWriting(string aPassword, int KeySizeInBytes)
        {
            const int           SaltSize = 16;
            TAgileEncryptionKey Result   = new TAgileEncryptionKey();

            Result.SpinCount      = 100000;
            Result.BlockSize      = 16;
            Result.KeySizeInBytes = KeySizeInBytes;
            Result.Salt           = TEncryptionUtils.GetRandom(SaltSize);

            if (aPassword != null)
            {
                Result.Password = Encoding.Unicode.GetBytes(aPassword);
                Result.PreCalcKey();
            }

            return(Result);
        }
Ejemplo n.º 3
0
        private byte[] GetInfoStreamXml(AesManaged EncEngine, TEncryptionParameters EncParams, TAgileEncryptionKey KeyKey, TAgileEncryptionKey DataKey)
        {
            const string KeyEncryptorPasswordNamespace = "http://schemas.microsoft.com/office/2006/keyEncryptor/password";

            using (MemoryStream ms = new MemoryStream())
            {
                XmlWriterSettings Settings = new XmlWriterSettings();
                Settings.Encoding = new System.Text.UTF8Encoding(false);
                using (XmlWriter xml = XmlWriter.Create(ms, Settings))
                {
                    xml.WriteStartDocument(true);
                    xml.WriteStartElement("encryption", "http://schemas.microsoft.com/office/2006/encryption");
                    xml.WriteAttributeString("xmlns", "p", null, KeyEncryptorPasswordNamespace);
                    xml.WriteStartElement("keyData");
                    WriteAgileCipherParams(xml, EncParams, DataKey);
                    xml.WriteEndElement();

                    xml.WriteStartElement("dataIntegrity");

                    byte[] HMacKeyBlockKey = new byte[] { 0x5f, 0xb2, 0xad, 0x01, 0x0c, 0xb9, 0xe1, 0xf6 };
                    DataKey.CalcDataIV(HMacKeyBlockKey);
                    byte[] HMacKey = TEncryptionUtils.GetRandom((int)DataKey.HashSizeBytes());
                    using (ICryptoTransform Encryptor = EncEngine.CreateEncryptor(DataKey.Key, DataKey.IV))
                    {
                        byte[] EncryptedHMacKey = TAgileEncryptionVerifier.EncryptBytes(HMacKey, Encryptor, DataKey.BlockSize);
                        xml.WriteAttributeString("encryptedHmacKey", Convert.ToBase64String(EncryptedHMacKey));
                    }

                    HMAC HMacCalc = new HMACSHA1(HMacKey);
                    WorkingStream.Position = 0;
                    byte[] HMac            = HMacCalc.ComputeHash(WorkingStream);
                    byte[] HMacValBlockKey = new byte[] { 0xa0, 0x67, 0x7f, 0x02, 0xb2, 0x2c, 0x84, 0x33 };
                    DataKey.CalcDataIV(HMacValBlockKey);
                    using (ICryptoTransform Encryptor = EncEngine.CreateEncryptor(DataKey.Key, DataKey.IV))
                    {
                        byte[] EncryptedHMacValue = TAgileEncryptionVerifier.EncryptBytes(HMac, Encryptor, DataKey.BlockSize);
                        xml.WriteAttributeString("encryptedHmacValue", Convert.ToBase64String(EncryptedHMacValue));
                    }

                    xml.WriteEndElement();
                    xml.WriteStartElement("keyEncryptors");
                    xml.WriteStartElement("keyEncryptor");
                    xml.WriteAttributeString("uri", KeyEncryptorPasswordNamespace);
                    xml.WriteStartElement("encryptedKey", KeyEncryptorPasswordNamespace);


                    xml.WriteAttributeString("spinCount", Convert.ToString(KeyKey.SpinCount, CultureInfo.InvariantCulture));
                    WriteAgileCipherParams(xml, EncParams, KeyKey);

                    byte[] RandData = TEncryptionUtils.GetRandom(KeyKey.Salt.Length);
                    KeyKey.CalcKey(TAgileEncryptionKey.VerifierHashInputBlockKey, null);
                    using (ICryptoTransform Encryptor = EncEngine.CreateEncryptor(KeyKey.Key, KeyKey.IV))
                    {
                        byte[] EncryptedVerifierHashInput = TAgileEncryptionVerifier.EncryptBytes(RandData, Encryptor, KeyKey.BlockSize);
                        xml.WriteAttributeString("encryptedVerifierHashInput", Convert.ToBase64String(EncryptedVerifierHashInput));
                    }


                    KeyKey.CalcKey(TAgileEncryptionKey.VerifierHashValueBlockKey, null);
                    using (ICryptoTransform Encryptor = EncEngine.CreateEncryptor(KeyKey.Key, KeyKey.IV))
                    {
                        byte[] EncryptedVerifierHashValue = TAgileEncryptionVerifier.EncryptBytes(KeyKey.Hash(RandData), Encryptor, KeyKey.BlockSize);
                        xml.WriteAttributeString("encryptedVerifierHashValue", Convert.ToBase64String(EncryptedVerifierHashValue));
                    }

                    KeyKey.CalcKey(TAgileEncryptionKey.VerifierKeyValueBlockKey, null);
                    using (ICryptoTransform Encryptor = EncEngine.CreateEncryptor(KeyKey.Key, KeyKey.IV))
                    {
                        byte[] EncryptedKeyValue = TAgileEncryptionVerifier.EncryptBytes(DataKey.Key, Encryptor, KeyKey.BlockSize);
                        xml.WriteAttributeString("encryptedKeyValue", Convert.ToBase64String(EncryptedKeyValue));
                    }
                    xml.WriteEndElement();
                    xml.WriteEndElement();
                    xml.WriteEndElement();

                    xml.WriteEndElement();
                    xml.WriteEndDocument();
                }
                return(ms.ToArray());
            }
        }