Free cookie consent management tool by TermsFeed Policy Generator

source: branches/3026_IntegrationIntoSymSpace/HeuristicLab.ExtLibs/HeuristicLab.EPPlus/4.0.3/EPPlus-4.0.3/ExcelEncryption.cs @ 17399

Last change on this file since 17399 was 12074, checked in by sraggl, 10 years ago

#2341: Added EPPlus-4.0.3 to ExtLibs

File size: 5.9 KB
Line 
1/*******************************************************************************
2 * You may amend and distribute as you like, but don't remove this header!
3 *
4 * EPPlus provides server-side generation of Excel 2007/2010 spreadsheets.
5 * See http://www.codeplex.com/EPPlus for details.
6 *
7 * Copyright (C) 2011  Jan Källman
8 *
9 * This library is free software; you can redistribute it and/or
10 * modify it under the terms of the GNU Lesser General Public
11 * License as published by the Free Software Foundation; either
12 * version 2.1 of the License, or (at your option) any later version.
13
14 * This library is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. 
17 * See the GNU Lesser General Public License for more details.
18 *
19 * The GNU Lesser General Public License can be viewed at http://www.opensource.org/licenses/lgpl-license.php
20 * If you unfamiliar with this license or have questions about it, here is an http://www.gnu.org/licenses/gpl-faq.html
21 *
22 * All code and executables are provided "as is" with no warranty either express or implied.
23 * The author accepts no liability for any damage or loss of business that this product may cause.
24 *
25 * Code change notes:
26 *
27 ***************************************************************************************
28 * This class is created with the help of the MS-OFFCRYPTO PDF documentation... http://msdn.microsoft.com/en-us/library/cc313071(office.12).aspx
29 * Decrypytion library for Office Open XML files(Lyquidity) and Sminks very nice example
30 * on "Reading compound documents in c#" on Stackoverflow. Many thanks!
31 ***************************************************************************************
32 * 
33 * Code change notes:
34 *
35 * Author             Change            Date
36 *******************************************************************************
37 * Jan Källman    Added   10-AUG-2010
38 * Jan Källman    License changed GPL-->LGPL 2011-12-27
39 *******************************************************************************/
40using System;
41using System.Collections.Generic;
42using System.Text;
43
44namespace OfficeOpenXml
45{
46    /// <summary>
47    /// Encryption Algorithm
48    /// </summary>
49    public enum EncryptionAlgorithm
50    {
51        /// <summary>
52        /// 128-bit AES. Default
53        /// </summary>
54        AES128,
55        /// <summary>
56        /// 192-bit AES.
57        /// </summary>
58        AES192,
59        /// <summary>
60        /// 256-bit AES.
61        /// </summary>
62        AES256
63    }
64    /// <summary>
65    /// The major version of the Encryption
66    /// </summary>
67    public enum EncryptionVersion
68    {
69        /// <summary>
70        /// Standard Encryption.
71        /// Used in Excel 2007 and previous version with compatibility pack.
72        /// <remarks>Default AES 128 with SHA-1 as the hash algorithm. Spincount is hardcoded to 50000</remarks>
73        /// </summary>
74        Standard,
75        /// <summary>
76        /// Agile Encryption.
77        /// Used in Excel 2010-
78        /// Default.
79        /// </summary>
80        Agile
81    }
82    /// <summary>
83    /// How and if the workbook is encrypted
84    ///<seealso cref="ExcelProtection"/>
85    ///<seealso cref="ExcelSheetProtection"/>
86    /// </summary>
87    public class ExcelEncryption
88    {
89        /// <summary>
90        /// Constructor
91        /// <remarks>Default AES 256 with SHA-512 as the hash algorithm. Spincount is set to 100000</remarks>
92        /// </summary>
93        internal ExcelEncryption()
94        {
95            Algorithm = EncryptionAlgorithm.AES256;
96        }
97        /// <summary>
98        /// Constructor
99        /// </summary>
100        /// <param name="encryptionAlgorithm">Algorithm used to encrypt the package. Default is AES128</param>
101        internal ExcelEncryption(EncryptionAlgorithm encryptionAlgorithm)
102        {
103            Algorithm = encryptionAlgorithm;
104        }       
105        bool _isEncrypted = false;
106        /// <summary>
107        /// Is the package encrypted
108        /// </summary>
109        public bool IsEncrypted
110        {
111            get
112            {
113                return _isEncrypted;
114            }
115            set
116            {
117                _isEncrypted = value;
118                if (_isEncrypted)
119                {
120                    if (_password == null) _password = "";
121                }
122                else
123                {
124                    _password = null;
125                }
126            }
127        }
128        string _password=null;
129        /// <summary>
130        /// The password used to encrypt the workbook.
131        /// </summary>
132        public string Password
133        {
134            get
135            {
136                return _password;
137            }
138            set
139            {
140                _password = value;
141                _isEncrypted = (value != null);
142            }
143        }
144        /// <summary>
145        /// Algorithm used for encrypting the package. Default is AES 128-bit for standard and AES 256 for agile
146        /// </summary>
147        public EncryptionAlgorithm Algorithm { get; set; }
148        private EncryptionVersion _version = EncryptionVersion.Agile;
149        /// <summary>
150        /// The version of the encryption.       
151        /// </summary>
152        public EncryptionVersion Version
153        {
154            get
155            {
156                return _version;
157            }
158            set
159            {
160                if (value != Version)
161                {
162                    if (value == EncryptionVersion.Agile)
163                    {
164                        Algorithm = EncryptionAlgorithm.AES256;
165                    }
166                    else
167                    {
168                        Algorithm = EncryptionAlgorithm.AES128;
169                    }
170                    _version = value;
171                }
172            }
173        }
174    }
175}
Note: See TracBrowser for help on using the repository browser.