Skip to content
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -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;

Expand Down Expand Up @@ -58,6 +61,7 @@ public async Task SignOutAsync()
var privateKeyBytes = utf8WithoutBom.GetBytes(privateKey);

byte[] encryptedPrivateKeyBytes;

try
{
using var aes = Aes.Create();
Expand All @@ -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];
Expand Down
Loading