コード例 #1
0
        /// <summary>
        /// Create based on a stream.
        /// </summary>
        /// <param name="storageStream">Storage stream, usually FileStream</param>
        /// <param name="password">Password. Pass null for plaintext.</param>
        /// <throws>InvalidPasswordException if password is incorrect</throws>
        /// <throws>FileNotFoundException if file does not exist</throws>
        /// <throws>FileFormatException: file is in the wrong format</throws>
        public OfficeCryptoStream(Stream storageStream, String password)
        {
            _storage = storageStream;
            Password = password;

            if (storageStream.Length == 0)
            {
                // No need to decrypt, stream is already 0-length
                return;
            }

            // Check if the file is actually encrypted or plaintext
            bool isPlain     = IsPlaintext(storageStream);
            bool isEncrypted = IsEncrypted(storageStream);

            if (!isPlain && !isEncrypted)
            {
                Close(); // In ctor, cannot rely on client/using to close
                throw new FileFormatException("File is neither plaintext package nor Office 2007 encrypted.");
            }

            // Read the file
            byte[] contents = new byte[storageStream.Length];
            storageStream.Read(contents, 0, contents.Length);

            // Decrypt if needed
            if (isEncrypted)
            {
                if (String.IsNullOrEmpty(Password))
                {
                    Close(); // In ctor, cannot rely on client/using to close
                    throw new InvalidPasswordException("Password not provided.");
                }

                try
                {
                    OfficeCrypto oc = new OfficeCrypto();
                    contents = oc.DecryptToArray(contents, password);
                }
                catch (Exception)
                {
                    Close(); // In ctor, cannot rely on client/using to close
                    throw;
                }
            }

            // Write out to the memory stream
            base.Write(contents, 0, contents.Length);
            base.Flush();
            base.Position = 0;
        }
コード例 #2
0
        /// <summary>
        /// Create based on a stream.
        /// </summary>
        /// <param name="storageStream">Storage stream, usually FileStream</param>
        /// <param name="password">Password. Pass null for plaintext.</param>
        /// <throws>InvalidPasswordException if password is incorrect</throws>
        /// <throws>FileNotFoundException if file does not exist</throws>
        /// <throws>FileFormatException: file is in the wrong format</throws>
        public OfficeCryptoStream(Stream storageStream, String password)
        {
            _storage = storageStream;
            Password = password;

            if (storageStream.Length == 0)
            {
                // No need to decrypt, stream is already 0-length
                return;
            }

            // Check if the file is actually encrypted or plaintext
            bool isPlain = IsPlaintext(storageStream);
            bool isEncrypted = IsEncrypted(storageStream);
            if (!isPlain && !isEncrypted)
            {
                Close(); // In ctor, cannot rely on client/using to close
                throw new FileFormatException("File is neither plaintext package nor Office 2007 encrypted.");
            }

            // Read the file
            byte[] contents = new byte[storageStream.Length];
            storageStream.Read(contents, 0, contents.Length);

            // Decrypt if needed
            if (isEncrypted)
            {
                if (String.IsNullOrEmpty(Password))
                {
                    Close(); // In ctor, cannot rely on client/using to close
                    throw new InvalidPasswordException("Password not provided.");
                }

                try
                {
                    OfficeCrypto oc = new OfficeCrypto();
                    contents = oc.DecryptToArray(contents, password);
                }
                catch (Exception)
                {
                    Close(); // In ctor, cannot rely on client/using to close
                    throw;
                }
            }

            // Write out to the memory stream
            base.Write(contents, 0, contents.Length);
            base.Flush();
            base.Position = 0;
        }
コード例 #3
0
        /// <summary>
        /// Static test function exposed by the class
        /// </summary>
        public static Package OfficePasswordHash(string filename, string password)
        {
            OfficeCrypto officeCrypto = new OfficeCrypto();

            try
            {
                Package package = officeCrypto.DecryptToPackage(filename, password);
                Console.WriteLine("Package decrypted and opened");
                return(package);
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.Message);
            }

            return(null);
        }
コード例 #4
0
        /// <summary>
        /// Encrypt and write out to storage stream.
        /// NOTE: Don't forget to call Close()
        /// </summary>
        public void Save()
        {
            _storage.Seek(0, SeekOrigin.Begin);
            _storage.SetLength(0);
            _storage.Position = 0;

            if (Encrypted)
            {
                // Encrypt this to the storage stream
                OfficeCrypto oc = new OfficeCrypto();
                oc.EncryptToStream(base.ToArray(), Password, _storage);
            }
            else
            {
                // Just write the contents to storage stream
                base.WriteTo(_storage);
            }
        }
コード例 #5
0
        public static bool EncryptPackageFile(string filename, string password, out byte[] encryptionInfo, out byte[] encryptedPackage)
        {
            OfficeCrypto officeCrypto = new OfficeCrypto();

            encryptionInfo   = null;
            encryptedPackage = null;

            try
            {
                officeCrypto.EncryptPackage(filename, password, out encryptionInfo, out encryptedPackage);
                Console.WriteLine("Package encrypted");
                officeCrypto.TestEncrytion(password, encryptionInfo, encryptedPackage);
                return(true);
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.Message);
            }

            return(false);
        }
コード例 #6
0
        /// <summary>
        /// Static test function exposed by the class
        /// </summary>
        public static Package OfficePasswordHash(string filename, string password)
        {
            OfficeCrypto officeCrypto = new OfficeCrypto();

            try
            {
                Package package = officeCrypto.DecryptToPackage(filename, password);
                Console.WriteLine("Package decrypted and opened");
                return package;
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.Message);
            }

            return null;
        }
コード例 #7
0
        public static bool EncryptPackageFile(string filename, string password, out byte[] encryptionInfo, out byte[] encryptedPackage)
        {
            OfficeCrypto officeCrypto = new OfficeCrypto();

            encryptionInfo = null;
            encryptedPackage = null;

            try
            {
                officeCrypto.EncryptPackage(filename, password, out encryptionInfo, out encryptedPackage);
                Console.WriteLine("Package encrypted");
                officeCrypto.TestEncrytion(password, encryptionInfo, encryptedPackage);
                return true;
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.Message);
            }

            return false;
        }
コード例 #8
0
        /// <summary>
        /// Encrypt and write out to storage stream.
        /// NOTE: Don't forget to call Close()
        /// </summary>
        public void Save()
        {
            _storage.Seek(0, SeekOrigin.Begin);
            _storage.SetLength(0);
            _storage.Position = 0;

            if (Encrypted)
            {
                // Encrypt this to the storage stream
                OfficeCrypto oc = new OfficeCrypto();
                oc.EncryptToStream(base.ToArray(), Password, _storage);
            }
            else
            {
                // Just write the contents to storage stream
                base.WriteTo(_storage);
            }
        }