diff --git a/Thirdweb/Thirdweb.Wallets/InAppWallet/EmbeddedWallet/EmbeddedWallet.Misc.cs b/Thirdweb/Thirdweb.Wallets/InAppWallet/EmbeddedWallet/EmbeddedWallet.Misc.cs index ac230732..02917bad 100644 --- a/Thirdweb/Thirdweb.Wallets/InAppWallet/EmbeddedWallet/EmbeddedWallet.Misc.cs +++ b/Thirdweb/Thirdweb.Wallets/InAppWallet/EmbeddedWallet/EmbeddedWallet.Misc.cs @@ -1,5 +1,8 @@ using System.Security.Cryptography; using Nethereum.Web3.Accounts; +using Org.BouncyCastle.Crypto.Paddings; +using Org.BouncyCastle.Crypto.Engines; +using Org.BouncyCastle.Crypto.Parameters; namespace Thirdweb.EWS; @@ -58,6 +61,7 @@ public async Task SignOutAsync() var privateKeyBytes = utf8WithoutBom.GetBytes(privateKey); byte[] encryptedPrivateKeyBytes; + try { using var aes = Aes.Create(); @@ -71,9 +75,32 @@ public async Task SignOutAsync() using var encryptor = aes.CreateEncryptor(); encryptedPrivateKeyBytes = encryptor.TransformFinalBlock(privateKeyBytes, 0, privateKeyBytes.Length); } - catch (Exception ex) + // Fallback to BouncyCastle + catch (Exception) { - throw new InvalidOperationException("Encryption failed.", ex); + try + { + var key = Convert.FromBase64String(plainTextBase64); + + var engine = new AesEngine(); + var blockCipher = new Org.BouncyCastle.Crypto.Modes.CbcBlockCipher(engine); + var cipher = new PaddedBufferedBlockCipher(blockCipher, new Pkcs7Padding()); + + var keyParam = new KeyParameter(key); + var keyParamWithIV = new ParametersWithIV(keyParam, iv); + + cipher.Init(true, keyParamWithIV); + + encryptedPrivateKeyBytes = new byte[cipher.GetOutputSize(privateKeyBytes.Length)]; + var length = cipher.ProcessBytes(privateKeyBytes, 0, privateKeyBytes.Length, encryptedPrivateKeyBytes, 0); + length += cipher.DoFinal(encryptedPrivateKeyBytes, length); + + Array.Resize(ref encryptedPrivateKeyBytes, length); + } + catch (Exception ex) + { + throw new InvalidOperationException("Migration failed", ex); + } } var encryptedData = new byte[iv.Length + encryptedPrivateKeyBytes.Length];