@@ -88,31 +88,38 @@ public static byte[] DecryptWithGCM(byte[] keyBytes, byte[] nonceBytes, byte[]?
88
88
if ( cipherBytes . Length < TAG_LENGTH_BYTE ) throw new ArgumentException ( $ "Invalid cipher byte length (expected: more than { TAG_LENGTH_BYTE } , actual: { cipherBytes . Length } ).", nameof ( cipherBytes ) ) ;
89
89
90
90
#if NET5_0_OR_GREATER
91
- using ( AesGcm aes = new AesGcm ( keyBytes ) )
91
+ if ( AesGcm . IsSupported )
92
92
{
93
- byte [ ] cipherWithoutTagBytes = new byte [ cipherBytes . Length - TAG_LENGTH_BYTE ] ;
94
- byte [ ] tagBytes = new byte [ TAG_LENGTH_BYTE ] ;
95
- Buffer . BlockCopy ( cipherBytes , 0 , cipherWithoutTagBytes , 0 , cipherWithoutTagBytes . Length ) ;
96
- Buffer . BlockCopy ( cipherBytes , cipherWithoutTagBytes . Length , tagBytes , 0 , tagBytes . Length ) ;
93
+ if ( ! string . Equals ( paddingMode , PADDING_MODE_NOPADDING , StringComparison . OrdinalIgnoreCase ) )
94
+ throw new NotSupportedException ( ) ;
97
95
98
- byte [ ] plainBytes = new byte [ cipherWithoutTagBytes . Length ] ;
99
- aes . Decrypt ( nonceBytes , cipherWithoutTagBytes , tagBytes , plainBytes , associatedDataBytes ) ;
100
- return plainBytes ;
96
+ using ( AesGcm aes = new AesGcm ( keyBytes ) )
97
+ {
98
+ byte [ ] cipherWithoutTagBytes = new byte [ cipherBytes . Length - TAG_LENGTH_BYTE ] ;
99
+ byte [ ] tagBytes = new byte [ TAG_LENGTH_BYTE ] ;
100
+ Buffer . BlockCopy ( cipherBytes , 0 , cipherWithoutTagBytes , 0 , cipherWithoutTagBytes . Length ) ;
101
+ Buffer . BlockCopy ( cipherBytes , cipherWithoutTagBytes . Length , tagBytes , 0 , tagBytes . Length ) ;
102
+
103
+ byte [ ] plainBytes = new byte [ cipherWithoutTagBytes . Length ] ;
104
+ aes . Decrypt ( nonceBytes , cipherWithoutTagBytes , tagBytes , plainBytes , associatedDataBytes ) ;
105
+ return plainBytes ;
106
+ }
101
107
}
102
- #else
103
- IBufferedCipher cipher = CipherUtilities . GetCipher ( $ "AES/GCM/{ paddingMode } ") ;
104
- ICipherParameters cipherParams = new AeadParameters (
105
- new KeyParameter ( keyBytes ) ,
106
- TAG_LENGTH_BYTE * 8 ,
107
- nonceBytes ,
108
- associatedDataBytes
109
- ) ;
110
- cipher . Init ( false , cipherParams ) ;
111
- byte [ ] plainBytes = new byte [ cipher . GetOutputSize ( cipherBytes . Length ) ] ;
112
- int len = cipher . ProcessBytes ( cipherBytes , 0 , cipherBytes . Length , plainBytes , 0 ) ;
113
- cipher . DoFinal ( plainBytes , len ) ;
114
- return plainBytes ;
115
108
#endif
109
+ {
110
+ IBufferedCipher cipher = CipherUtilities . GetCipher ( $ "AES/GCM/{ paddingMode } ") ;
111
+ ICipherParameters cipherParams = new AeadParameters (
112
+ new KeyParameter ( keyBytes ) ,
113
+ TAG_LENGTH_BYTE * 8 ,
114
+ nonceBytes ,
115
+ associatedDataBytes
116
+ ) ;
117
+ cipher . Init ( false , cipherParams ) ;
118
+ byte [ ] plainBytes = new byte [ cipher . GetOutputSize ( cipherBytes . Length ) ] ;
119
+ int len = cipher . ProcessBytes ( cipherBytes , 0 , cipherBytes . Length , plainBytes , 0 ) ;
120
+ cipher . DoFinal ( plainBytes , len ) ;
121
+ return plainBytes ;
122
+ }
116
123
}
117
124
118
125
/// <summary>
@@ -204,32 +211,38 @@ public static byte[] EncryptWithGCM(byte[] keyBytes, byte[] nonceBytes, byte[]?
204
211
if ( plainBytes is null ) throw new ArgumentNullException ( nameof ( plainBytes ) ) ;
205
212
206
213
#if NET5_0_OR_GREATER
207
- using ( AesGcm aes = new AesGcm ( keyBytes ) )
214
+ if ( AesGcm . IsSupported )
208
215
{
209
- byte [ ] cipherBytes = new byte [ plainBytes . Length ] ;
210
- byte [ ] tagBytes = new byte [ TAG_LENGTH_BYTE ] ;
211
- aes . Encrypt ( nonceBytes , plainBytes , cipherBytes , tagBytes , associatedDataBytes ) ;
216
+ if ( ! string . Equals ( paddingMode , PADDING_MODE_NOPADDING , StringComparison . OrdinalIgnoreCase ) )
217
+ throw new NotSupportedException ( ) ;
212
218
213
- byte [ ] cipherWithTagBytes = new byte [ cipherBytes . Length + tagBytes . Length ] ;
214
- Buffer . BlockCopy ( cipherBytes , 0 , cipherWithTagBytes , 0 , cipherBytes . Length ) ;
215
- Buffer . BlockCopy ( tagBytes , 0 , cipherWithTagBytes , cipherBytes . Length , tagBytes . Length ) ;
216
- return cipherWithTagBytes ;
217
- }
218
- #else
219
+ using ( AesGcm aes = new AesGcm ( keyBytes ) )
220
+ {
221
+ byte [ ] cipherBytes = new byte [ plainBytes . Length ] ;
222
+ byte [ ] tagBytes = new byte [ TAG_LENGTH_BYTE ] ;
223
+ aes . Encrypt ( nonceBytes , plainBytes , cipherBytes , tagBytes , associatedDataBytes ) ;
219
224
220
- IBufferedCipher cipher = CipherUtilities . GetCipher ( $ "AES/GCM/{ paddingMode } ") ;
221
- ICipherParameters cipherParams = new AeadParameters (
222
- new KeyParameter ( keyBytes ) ,
223
- TAG_LENGTH_BYTE * 8 ,
224
- nonceBytes ,
225
- associatedDataBytes
226
- ) ;
227
- cipher . Init ( true , cipherParams ) ;
228
- byte [ ] cipherBytes = new byte [ cipher . GetOutputSize ( plainBytes . Length ) ] ;
229
- int len = cipher . ProcessBytes ( plainBytes , 0 , plainBytes . Length , cipherBytes , 0 ) ;
230
- cipher . DoFinal ( cipherBytes , len ) ;
231
- return cipherBytes ;
225
+ byte [ ] cipherWithTagBytes = new byte [ cipherBytes . Length + tagBytes . Length ] ;
226
+ Buffer . BlockCopy ( cipherBytes , 0 , cipherWithTagBytes , 0 , cipherBytes . Length ) ;
227
+ Buffer . BlockCopy ( tagBytes , 0 , cipherWithTagBytes , cipherBytes . Length , tagBytes . Length ) ;
228
+ return cipherWithTagBytes ;
229
+ }
230
+ }
232
231
#endif
232
+ {
233
+ IBufferedCipher cipher = CipherUtilities . GetCipher ( $ "AES/GCM/{ paddingMode } ") ;
234
+ ICipherParameters cipherParams = new AeadParameters (
235
+ new KeyParameter ( keyBytes ) ,
236
+ TAG_LENGTH_BYTE * 8 ,
237
+ nonceBytes ,
238
+ associatedDataBytes
239
+ ) ;
240
+ cipher . Init ( true , cipherParams ) ;
241
+ byte [ ] cipherBytes = new byte [ cipher . GetOutputSize ( plainBytes . Length ) ] ;
242
+ int len = cipher . ProcessBytes ( plainBytes , 0 , plainBytes . Length , cipherBytes , 0 ) ;
243
+ cipher . DoFinal ( cipherBytes , len ) ;
244
+ return cipherBytes ;
245
+ }
233
246
}
234
247
235
248
/// <summary>
0 commit comments