Beispiel #1
0
        private static Stream DecryptStream(TOle2File DataStream, TEncryptionParameters EncParams, TEncryptionKey Key)
        {
            DataStream.SelectStream(XlsxConsts.ContentString);
            byte[] EncryptedSize = new byte[8];
            DataStream.Read(EncryptedSize, EncryptedSize.Length);
            AesManaged       Engine    = null;
            ICryptoTransform Decryptor = null;

            try
            {
                Engine = TEncryptionUtils.CreateEngine(EncParams);
                if (!Key.VariableIV)
                {
                    Decryptor = Engine.CreateDecryptor(Key.Key, Key.IV);
                }
                return(new TXlsxCryptoStreamReader(DataStream, BitOps.GetCardinal(EncryptedSize, 0), Engine, Decryptor, Key));
            }
            catch
            {
                if (Engine != null)
                {
                    ((IDisposable)Engine).Dispose();
                }
                if (Decryptor != null)
                {
                    Decryptor.Dispose();
                }
                throw;
            }
        }
Beispiel #2
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);
                }
            }
        }
Beispiel #3
0
        internal override bool VerifyPass(string Password, TEncryptionParameters EncParams, TEncryptionKey Key)
        {
            if (!base.VerifyPass(Password, EncParams, Key))
            {
                return(false);
            }

            Key.CalcKey(TStandardEncryptionKey.BlockKey, null);
            byte[] DecriptedVerifier;
            byte[] DecriptedVerifierHash;
            using (AesManaged Engine = TEncryptionUtils.CreateEngine(EncParams))
            {
                using (ICryptoTransform Decryptor = Engine.CreateDecryptor(Key.Key, Key.IV))
                {
                    DecriptedVerifier     = DecryptBytes(EncryptedVerifier, Decryptor, -1);
                    DecriptedVerifierHash = DecryptBytes(EncryptedVerifierHash, Decryptor, -1);
                }
            }

            using (HashAlgorithm hasher = TEncryptionKey.CreateHasher())
            {
                byte[] DecriptedVerifierHash2 = hasher.ComputeHash(DecriptedVerifier);
                if (!FlxUtils.CompareMem(DecriptedVerifierHash, 0, DecriptedVerifierHash2, 0, VerifierHashSizeBytes))
                {
                    return(false);
                }
            }

            return(true);
        }
Beispiel #4
0
 private static byte[] GetBytesFromCryptoStream(CryptoStream cs, int MaxSize)
 {
     using (MemoryStream ms = new MemoryStream()) //not the most efficient code in the world, but should be used for small things
     {
         TEncryptionUtils.CopyStream(cs, ms, MaxSize);
         return(ms.ToArray());
     }
 }
Beispiel #5
0
 protected override byte[] DeriveIV(HashAlgorithm hasher, byte[] BlockKey)
 {
     byte[] ResultHash = Salt;
     if (BlockKey != null)
     {
         ResultHash = hasher.ComputeHash(Concat(Salt, BlockKey));
     }
     return(TEncryptionUtils.PadArray(ResultHash, BlockSize, 0x36));
 }
Beispiel #6
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);
        }
Beispiel #7
0
        internal override bool VerifyPass(string Password, TEncryptionParameters EncParams, TEncryptionKey Key)
        {
            if (!base.VerifyPass(Password, EncParams, Key))
            {
                return(false);
            }

            using (AesManaged Engine = TEncryptionUtils.CreateEngine(EncParams))
            {
                byte[] DecriptedVerifierHashInput;
                Key.CalcKey(TAgileEncryptionKey.VerifierHashInputBlockKey, null);
                using (ICryptoTransform Decryptor = Engine.CreateDecryptor(Key.Key, Key.IV))
                {
                    DecriptedVerifierHashInput = DecryptBytes(EncryptedVerifierHashInput, Decryptor, Key.Salt.Length); //this is the value padded to a blocksize multiple. We want only the Salt.Length initial bytes.
                    DecriptedVerifierHashInput = Key.Hash(DecriptedVerifierHashInput);
                }

                byte[] DecriptedVerifierHashValue;
                Key.CalcKey(TAgileEncryptionKey.VerifierHashValueBlockKey, null);
                using (ICryptoTransform Decryptor = Engine.CreateDecryptor(Key.Key, Key.IV))
                {
                    DecriptedVerifierHashValue = DecryptBytes(EncryptedVerifierHashValue, Decryptor, DecriptedVerifierHashInput.Length); //this is the 20 byte value of the hash + 12 "0" so it goes up to 32. (32 is 2*blocksize)
                }

                if (!FlxUtils.CompareMem(DecriptedVerifierHashValue, DecriptedVerifierHashInput))
                {
                    return(false);
                }

                byte[] DecriptedKeyValue;
                Key.CalcKey(TAgileEncryptionKey.VerifierKeyValueBlockKey, null);
                using (ICryptoTransform Decryptor = Engine.CreateDecryptor(Key.Key, Key.IV))
                {
                    DecriptedKeyValue = DecryptBytes(EncryptedKeyValue, Decryptor, Key.KeySizeInBytes);
                }

                Key.Key = DecriptedKeyValue;
            }

            return(true);
        }
Beispiel #8
0
 protected override byte[] DeriveKey(HashAlgorithm hasher, byte[] hfinal)
 {
     return(TEncryptionUtils.PadArray(hfinal, KeySizeInBytes, 0x36));
 }
Beispiel #9
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());
            }
        }