Closed madcoda9000 closed 1 year ago
Hello,
i did it by my self.. :-)
I did the following:
/// <summary>
/// Converts plaintext to a ciphertextby a given text and key
/// </summary>
/// <param name="text"></param>
/// <param name="key"></param>
/// <returns>An object with ciphertext, origin and secret data.</returns>
public Task<CryptoResult> EncryptByKeyAsync(string text, string key);
/// <summary>
/// Converts a ciphertext to plaintext by an key
/// </summary>
/// <param name="text"></param>
/// <param name="key"></param>
/// <returns>A decoded plaintext.</returns>
public Task<string> DecryptByKeyAsync(string text, string key);
public async Task<CryptoResult> EncryptByKeyAsync(string text, string key)
{
this._Key = key;
this._isGlobalKey = true;
if (string.IsNullOrEmpty(text))
return null;
var taskItem = CreateItem(0, text, true, key);
var result = await SubtleEncrypt(new List<CryptoResult> { taskItem });
return result.Any() ? result[0] : null;
}
public async Task<string> DecryptByKeyAsync(string text, string key)
{
this._Key = key;
this._isGlobalKey = true;
if (string.IsNullOrEmpty(text))
return null;
var taskItem = CreateItem(0, text, false, key);
var result = await SubtleDecrypt(new List<CryptoResult> { taskItem });
return result.Any() ? result[0] : null;
}
//add crypto service
builder.Services.AddSubtleCrypto();
var res = await _secs.GetS();
if (res!=null && res.Success && res.Data!=null) {
parameters.Add("us", await _crypt.DecryptByKeyAsync(sec.S_Username, res.Data));
parameters.Add("pw", await _crypt.DecryptByKeyAsync(sec.S_Password, res.Data));
res = null;
}
To me this has the following advantages:
Maybe it is something that you're interested too.
Wish you the best.
Hello,
i am using your Library and it is working as expected. But i dont want to put my key in program.cs and i dont want to use a dynamic key.
As i am using a protected api in my project i want to fetch the key from my api and then pass it to the encrypt decrypt method in my wasm (client) project.
I am thinking of something like:
doing it this way i can store the key in the appsettings of the server project and it is not accessible through the browser.
Could that be a change you can think of?
From my view that would be awsome!
Many Thanks for your awsome work and wish you the best...
Sascha