[12074] | 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 | * Author Change Date
|
---|
| 28 | * ******************************************************************************
|
---|
| 29 | * Jan Källman Added 2013-01-05
|
---|
| 30 | *******************************************************************************/
|
---|
| 31 | using System;
|
---|
| 32 | using System.Collections.Generic;
|
---|
| 33 | using System.IO;
|
---|
| 34 | using System.Linq;
|
---|
| 35 | using System.Text;
|
---|
| 36 |
|
---|
| 37 | namespace OfficeOpenXml.Encryption
|
---|
| 38 | {
|
---|
| 39 | [Flags]
|
---|
| 40 | internal enum Flags
|
---|
| 41 | {
|
---|
| 42 | Reserved1 = 1, // (1 bit): MUST be set to zero, and MUST be ignored.
|
---|
| 43 | Reserved2 = 2, // (1 bit): MUST be set to zero, and MUST be ignored.
|
---|
| 44 | fCryptoAPI = 4, // (1 bit): A flag that specifies whether CryptoAPI RC4 or [ECMA-376] encryption is used. It MUST be set to 1 unless fExternal is 1. If fExternal is set to 1, it MUST be set to zero.
|
---|
| 45 | fDocProps = 8, // (1 bit): MUST be set to zero if document properties are encrypted. Otherwise, it MUST be set to 1. Encryption of document properties is specified in section 2.3.5.4.
|
---|
| 46 | fExternal = 16, // (1 bit): If extensible encryption is used, it MUST be set to 1. Otherwise, it MUST be set to zero. If this field is set to 1, all other fields in this structure MUST be set to zero.
|
---|
| 47 | fAES = 32 //(1 bit): If the protected content is an [ECMA-376] document, it MUST be set to 1. Otherwise, it MUST be set to zero. If the fAES bit is set to 1, the fCryptoAPI bit MUST also be set to 1
|
---|
| 48 | }
|
---|
| 49 | internal enum AlgorithmID
|
---|
| 50 | {
|
---|
| 51 | Flags = 0x00000000, // Determined by Flags
|
---|
| 52 | RC4 = 0x00006801, // RC4
|
---|
| 53 | AES128 = 0x0000660E, // 128-bit AES
|
---|
| 54 | AES192 = 0x0000660F, // 192-bit AES
|
---|
| 55 | AES256 = 0x00006610 // 256-bit AES
|
---|
| 56 | }
|
---|
| 57 | internal enum AlgorithmHashID
|
---|
| 58 | {
|
---|
| 59 | App = 0x00000000,
|
---|
| 60 | SHA1 = 0x00008004,
|
---|
| 61 | }
|
---|
| 62 | internal enum ProviderType
|
---|
| 63 | {
|
---|
| 64 | Flags = 0x00000000,//Determined by Flags
|
---|
| 65 | RC4 = 0x00000001,
|
---|
| 66 | AES = 0x00000018,
|
---|
| 67 | }
|
---|
| 68 | /// <summary>
|
---|
| 69 | /// Encryption Header inside the EncryptionInfo stream
|
---|
| 70 | /// </summary>
|
---|
| 71 | internal class EncryptionHeader
|
---|
| 72 | {
|
---|
| 73 | internal Flags Flags;
|
---|
| 74 | internal int SizeExtra; //MUST be 0x00000000.
|
---|
| 75 | internal AlgorithmID AlgID; //MUST be 0x0000660E (AES-128), 0x0000660F (AES-192), or 0x00006610 (AES-256).
|
---|
| 76 | internal AlgorithmHashID AlgIDHash; //MUST be 0x00008004 (SHA-1).
|
---|
| 77 | internal int KeySize; //MUST be 0x00000080 (AES-128), 0x000000C0 (AES-192), or 0x00000100 (AES-256).
|
---|
| 78 | internal ProviderType ProviderType; //SHOULD<10> be 0x00000018 (AES).
|
---|
| 79 | internal int Reserved1; //Undefined and MUST be ignored.
|
---|
| 80 | internal int Reserved2; //MUST be 0x00000000 and MUST be ignored.
|
---|
| 81 | internal string CSPName; //SHOULD<11> be set to either "Microsoft Enhanced RSA and AES Cryptographic Provider" or "Microsoft Enhanced RSA and AES Cryptographic Provider (Prototype)" as a null-terminated Unicode string.
|
---|
| 82 | internal byte[] WriteBinary()
|
---|
| 83 | {
|
---|
| 84 | MemoryStream ms = new MemoryStream();
|
---|
| 85 | BinaryWriter bw = new BinaryWriter(ms);
|
---|
| 86 |
|
---|
| 87 | bw.Write((int)Flags);
|
---|
| 88 | bw.Write(SizeExtra);
|
---|
| 89 | bw.Write((int)AlgID);
|
---|
| 90 | bw.Write((int)AlgIDHash);
|
---|
| 91 | bw.Write((int)KeySize);
|
---|
| 92 | bw.Write((int)ProviderType);
|
---|
| 93 | bw.Write(Reserved1);
|
---|
| 94 | bw.Write(Reserved2);
|
---|
| 95 | bw.Write(Encoding.Unicode.GetBytes(CSPName));
|
---|
| 96 |
|
---|
| 97 | bw.Flush();
|
---|
| 98 | return ms.ToArray();
|
---|
| 99 | }
|
---|
| 100 | }
|
---|
| 101 | }
|
---|