Skip to content

Commit 88690eb

Browse files
author
Cory Thompson
committed
Remove dependency on AuthenticatedAesCng which is unavailable on mono
1 parent 80ec12c commit 88690eb

File tree

1 file changed

+12
-26
lines changed

1 file changed

+12
-26
lines changed

src/Util/Encryptor.cs

Lines changed: 12 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,12 @@
1-
using Security.Cryptography;
2-
using System;
1+
using System;
32
using System.Collections.Generic;
43
using System.IO;
54
using System.Linq;
65
using System.Security.Cryptography;
76
using System.Text;
8-
using Org.BouncyCastle.Asn1.Nist;
9-
using Org.BouncyCastle.Asn1.X9;
107
using Org.BouncyCastle.Crypto;
8+
using Org.BouncyCastle.Crypto.Engines;
9+
using Org.BouncyCastle.Crypto.Modes;
1110
using Org.BouncyCastle.Crypto.Parameters;
1211
using Org.BouncyCastle.Security;
1312

@@ -73,30 +72,17 @@ private static byte[] AddPaddingToInput(byte[] data)
7372

7473
private static byte[] EncryptAes(byte[] nonce, byte[] cek, byte[] message)
7574
{
76-
using (AuthenticatedAesCng aes = new AuthenticatedAesCng())
77-
{
78-
aes.CngMode = CngChainingMode.Gcm;
79-
80-
aes.Key = cek;
81-
82-
aes.IV = nonce;
83-
84-
using (MemoryStream ms = new MemoryStream())
85-
using (IAuthenticatedCryptoTransform encryptor = aes.CreateAuthenticatedEncryptor())
86-
using (CryptoStream cs = new CryptoStream(ms, encryptor, CryptoStreamMode.Write))
87-
{
88-
// Encrypt the secret message
89-
cs.Write(message, 0, message.Length);
90-
91-
// Finish the encryption and get the output authentication tag and ciphertext
92-
cs.FlushFinalBlock();
93-
byte[] ciphertext = ms.ToArray();
75+
GcmBlockCipher cipher = new GcmBlockCipher(new AesFastEngine());
76+
AeadParameters parameters = new AeadParameters(new KeyParameter(cek), 128, nonce);
77+
cipher.Init(true, parameters);
9478

95-
byte[] tag = encryptor.GetTag();
79+
//Generate Cipher Text With Auth Tag
80+
byte[] cipherText = new byte[cipher.GetOutputSize(message.Length)];
81+
int len = cipher.ProcessBytes(message, 0, message.Length, cipherText, 0);
82+
cipher.DoFinal(cipherText, len);
9683

97-
return ciphertext.Concat(tag).ToArray();
98-
}
99-
}
84+
//byte[] tag = cipher.GetMac();
85+
return cipherText;
10086
}
10187

10288
public static byte[] HKDFSecondStep(byte[] key, byte[] info, int length)

0 commit comments

Comments
 (0)