11using System . Security . Cryptography ;
22using Nethereum . Web3 . Accounts ;
3+ using System ;
4+ using Org . BouncyCastle . Crypto ;
5+ using Org . BouncyCastle . Crypto . Paddings ;
6+ using Org . BouncyCastle . Crypto . Engines ;
7+ using Org . BouncyCastle . Crypto . Parameters ;
38
49namespace Thirdweb . EWS ;
510
@@ -58,6 +63,7 @@ public async Task SignOutAsync()
5863 var privateKeyBytes = utf8WithoutBom . GetBytes ( privateKey ) ;
5964
6065 byte [ ] encryptedPrivateKeyBytes ;
66+
6167 try
6268 {
6369 using var aes = Aes . Create ( ) ;
@@ -71,9 +77,32 @@ public async Task SignOutAsync()
7177 using var encryptor = aes . CreateEncryptor ( ) ;
7278 encryptedPrivateKeyBytes = encryptor . TransformFinalBlock ( privateKeyBytes , 0 , privateKeyBytes . Length ) ;
7379 }
74- catch ( Exception ex )
80+ // Fallback to BouncyCastle
81+ catch ( Exception )
7582 {
76- throw new InvalidOperationException ( "Encryption failed." , ex ) ;
83+ try
84+ {
85+ var key = Convert . FromBase64String ( plainTextBase64 ) ;
86+
87+ var engine = new AesEngine ( ) ;
88+ var blockCipher = new Org . BouncyCastle . Crypto . Modes . CbcBlockCipher ( engine ) ;
89+ var cipher = new PaddedBufferedBlockCipher ( blockCipher , new Pkcs7Padding ( ) ) ;
90+
91+ var keyParam = new KeyParameter ( key ) ;
92+ var keyParamWithIV = new ParametersWithIV ( keyParam , iv ) ;
93+
94+ cipher . Init ( true , keyParamWithIV ) ;
95+
96+ encryptedPrivateKeyBytes = new byte [ cipher . GetOutputSize ( privateKeyBytes . Length ) ] ;
97+ var length = cipher . ProcessBytes ( privateKeyBytes , 0 , privateKeyBytes . Length , encryptedPrivateKeyBytes , 0 ) ;
98+ length += cipher . DoFinal ( encryptedPrivateKeyBytes , length ) ;
99+
100+ Array . Resize ( ref encryptedPrivateKeyBytes , length ) ;
101+ }
102+ catch ( Exception ex )
103+ {
104+ throw new InvalidOperationException ( "Migration failed" , ex ) ;
105+ }
77106 }
78107
79108 var encryptedData = new byte [ iv . Length + encryptedPrivateKeyBytes . Length ] ;
0 commit comments