@@ -228,11 +228,18 @@ public static string SealBase64(string plaintext, string publicKeyBase64)
228228 var publicKey = DecodeBase64Exact ( publicKeyBase64 , PublicKeyBytes , "public key" ) ;
229229 var message = Encoding . UTF8 . GetBytes ( plaintext ) ;
230230 var ciphertext = new byte [ message . Length + SealBytes ] ;
231- if ( Native . crypto_box_seal ( ciphertext , message , ( ulong ) message . LongLength , publicKey ) != 0 )
231+ try
232+ {
233+ if ( Native . crypto_box_seal ( ciphertext , message , ( ulong ) message . LongLength , publicKey ) != 0 )
234+ {
235+ throw new InvalidOperationException ( "Encryption failed." ) ;
236+ }
237+ return Convert . ToBase64String ( ciphertext ) ;
238+ }
239+ finally
232240 {
233- throw new InvalidOperationException ( "Encryption failed." ) ;
241+ CryptographicOperations . ZeroMemory ( message ) ;
234242 }
235- return Convert . ToBase64String ( ciphertext ) ;
236243 }
237244
238245 public static string OpenSealBase64 ( string ciphertextBase64 , string privateKeyBase64 )
@@ -253,7 +260,7 @@ private static string OpenSealBase64Core(string ciphertextBase64, string private
253260 var ciphertext = Convert . FromBase64String ( ciphertextBase64 ) ;
254261 if ( ciphertext . Length < SealBytes )
255262 {
256- throw new InvalidOperationException ( $ "Invalid sealed box. Expected at least { SealBytes } bytes but got { ciphertext . Length } .") ;
263+ throw new ArgumentException ( $ "Invalid sealed box. Expected at least { SealBytes } bytes but got { ciphertext . Length } .") ;
257264 }
258265 var privateKey = DecodeBase64Exact ( privateKeyBase64 , SecretKeyBytes , "private key" ) ;
259266 var publicKey = new byte [ PublicKeyBytes ] ;
@@ -291,7 +298,7 @@ private static byte[] DecodeBase64Exact(string value, int expectedLength, string
291298 var bytes = Convert . FromBase64String ( value ) ;
292299 if ( bytes . Length != expectedLength )
293300 {
294- throw new InvalidOperationException ( $ "Invalid { label } . Expected { expectedLength } bytes but got { bytes . Length } .") ;
301+ throw new ArgumentException ( $ "Invalid { label } . Expected { expectedLength } bytes but got { bytes . Length } .") ;
295302 }
296303 return bytes ;
297304 }
0 commit comments