Skip to content

Commit

Permalink
Fixes issue #890. Swiched 'RijndaelManaged' to 'Aes' for AES encryption
Browse files Browse the repository at this point in the history
  • Loading branch information
JanKallman committed Jun 14, 2023
1 parent 0b14384 commit 9fa4ce2
Show file tree
Hide file tree
Showing 4 changed files with 17 additions and 19 deletions.
2 changes: 1 addition & 1 deletion src/EPPlus/Core/Worksheet/WorksheetCopyHelper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ internal static ExcelWorksheet Copy(ExcelWorksheets worksheets, string name, Exc
string relID = worksheets.CreateWorkbookRel(name, sheetID, uriWorksheet, false, null);

ExcelWorksheet added = new ExcelWorksheet(nsm, pck, relID, uriWorksheet, name, sheetID, worksheets.Count + pck._worksheetAdd, eWorkSheetHidden.Visible);

added.SetNewUid();
//Copy comments
if (copy.ThreadedComments.Count > 0)
{
Expand Down
18 changes: 1 addition & 17 deletions src/EPPlus/Encryption/EncryptionHandler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -198,11 +198,7 @@ private MemoryStream EncryptPackageAgile(byte[] package, ExcelEncryption encrypt
private byte[] EncryptDataAgile(byte[] data, EncryptionInfoAgile encryptionInfo, HashAlgorithm hashProvider)
{
var ke = encryptionInfo.KeyEncryptors[0];
#if Core
var aes = Aes.Create();
#else
RijndaelManaged aes = new RijndaelManaged();
#endif
aes.KeySize = ke.KeyBits;
aes.Mode = CipherMode.CBC;
aes.Padding = PaddingMode.Zeros;
Expand Down Expand Up @@ -472,11 +468,7 @@ private EncryptionInfoBinary CreateEncryptionInfo(string password, AlgorithmID a
}
private byte[] EncryptData(byte[] key, byte[] data, bool useDataSize)
{
#if (Core)
var aes = Aes.Create();
#else
RijndaelManaged aes = new RijndaelManaged();
#endif
aes.KeySize = key.Length * 8;
aes.Mode = CipherMode.ECB;
aes.Padding = PaddingMode.Zeros;
Expand Down Expand Up @@ -673,11 +665,7 @@ private MemoryStream DecryptBinary(EncryptionInfoBinary encryptionInfo, string p
encryptionInfo.Header.AlgID == AlgorithmID.AES256
)
{
#if (Core)
var decryptKey = Aes.Create();
#else
RijndaelManaged decryptKey = new RijndaelManaged();
#endif
decryptKey.KeySize = encryptionInfo.Header.KeySize;
decryptKey.Mode = CipherMode.ECB;
decryptKey.Padding = PaddingMode.None;
Expand Down Expand Up @@ -716,11 +704,7 @@ private MemoryStream DecryptBinary(EncryptionInfoBinary encryptionInfo, string p
/// <returns></returns>
private bool IsPasswordValid(byte[] key, EncryptionInfoBinary encryptionInfo)
{
#if (Core)
var decryptKey = Aes.Create();
#else
RijndaelManaged decryptKey = new RijndaelManaged();
#endif
decryptKey.KeySize = encryptionInfo.Header.KeySize;
decryptKey.Mode = CipherMode.ECB;
decryptKey.Padding = PaddingMode.None;
Expand Down Expand Up @@ -845,7 +829,7 @@ private SymmetricAlgorithm GetEncryptionAlgorithm(EncryptionInfoAgile.Encryption
switch (encr.CipherAlgorithm)
{
case eCipherAlgorithm.AES:
return new RijndaelManaged();
return Aes.Create();
case eCipherAlgorithm.DES:
return new DESCryptoServiceProvider();
case eCipherAlgorithm.TRIPLE_DES:
Expand Down
5 changes: 4 additions & 1 deletion src/EPPlus/ExcelWorksheet.cs
Original file line number Diff line number Diff line change
Expand Up @@ -1238,7 +1238,10 @@ private void CreateVmlCollection()
}
}
}

internal void SetNewUid()
{
SetXmlNodeString("@xr:uid", "{" + Guid.NewGuid().ToString().ToUpperInvariant() + "}");
}
private void CreateXml()
{
_worksheetXml = new XmlDocument();
Expand Down
11 changes: 11 additions & 0 deletions src/EPPlusTest/Issues.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5016,5 +5016,16 @@ public void I892()
SaveAndCleanup(p);
}
}
[TestMethod]
public void SaveDialogIssue890()
{
using (var package = OpenTemplatePackage("issue890.xlsx"))
{
ExcelWorksheet worksheet = package.Workbook.Worksheets[0];
ExcelWorksheet newWorksheet = package.Workbook.Worksheets.Add("Sheet2", worksheet);
SaveAndCleanup(package);
}
}

}
}

0 comments on commit 9fa4ce2

Please sign in to comment.