|
1 | | -using Security.Cryptography; |
2 | | -using System; |
| 1 | +using System; |
3 | 2 | using System.Collections.Generic; |
4 | 3 | using System.IO; |
5 | 4 | using System.Linq; |
6 | 5 | using System.Security.Cryptography; |
7 | 6 | using System.Text; |
8 | | -using Org.BouncyCastle.Asn1.Nist; |
9 | | -using Org.BouncyCastle.Asn1.X9; |
10 | 7 | using Org.BouncyCastle.Crypto; |
| 8 | +using Org.BouncyCastle.Crypto.Engines; |
| 9 | +using Org.BouncyCastle.Crypto.Modes; |
11 | 10 | using Org.BouncyCastle.Crypto.Parameters; |
12 | 11 | using Org.BouncyCastle.Security; |
13 | 12 |
|
@@ -73,30 +72,17 @@ private static byte[] AddPaddingToInput(byte[] data) |
73 | 72 |
|
74 | 73 | private static byte[] EncryptAes(byte[] nonce, byte[] cek, byte[] message) |
75 | 74 | { |
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); |
94 | 78 |
|
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); |
96 | 83 |
|
97 | | - return ciphertext.Concat(tag).ToArray(); |
98 | | - } |
99 | | - } |
| 84 | + //byte[] tag = cipher.GetMac(); |
| 85 | + return cipherText; |
100 | 86 | } |
101 | 87 |
|
102 | 88 | public static byte[] HKDFSecondStep(byte[] key, byte[] info, int length) |
|
0 commit comments