问题描述
我尝试使用AES库中的。
I tried to encrypt in angular using AES library from AES.
我使用 CryptoJS加密了字符串AES中的.AES.encrypt()
方法。
这是我的代码:
var txtloginKod = 'Some String...';
var key = CryptoJS.enc.Utf8.parse('8080808080808080');
var iv = CryptoJS.enc.Utf8.parse('8080808080808080');
var encryptedlogin = CryptoJS.AES.encrypt(CryptoJS.enc.Utf8.parse(txtloginKod), key,
{
keySize: 128 / 8,
iv: iv,
mode: CryptoJS.mode.CBC,
padding: CryptoJS.pad.Pkcs7
});
方法 CryptoJS.AES.encrypt()
将对象返回到我的 encryptedlogin
变量。
The method CryptoJS.AES.encrypt()
return a Object to my encryptedlogin
variable.
我不知道如何将该对象发送到WCF。 C#中的Web服务器
I don't know how to send this object to my WCF Web server in C#
当我尝试发送整个对象(并定义Web服务方法以期望获得C#对象)时,出现以下错误:
When I tried to send the whole object (and define the Web service method to expect get C# Object), I got the error below:
推荐答案
我也有要求将加密的JSON对象发送到.Net CORE API 2.0,并在Internet上进行搜索以查找是否有帮助。我知道现在您已经解决了这个问题,但是为了帮助有兴趣的人,我将提供我的解决方案来帮助他们。
I too had the requirement to send encrypted JSON object to .Net CORE API 2.0 and searched on internet to find if there is any help. I know by now you would have solved this issue but to help anyone interested I am providing my solution to help them.
借助在stackoverflow上找到的示例代码,我设法实施了解决方案。棘手的位是关键,代码的长度必须为16,IV才能正常工作。
With the sample codes I found here on stackoverflow, I managed to implement the solution. The tricky bit is the key and IV has to be 16 in length for the code to work.
public static encrypt(model: any) {
const key = CryptoJS.enc.Utf8.parse('TestMyOwnKeyForI');
const iv = CryptoJS.enc.Utf8.parse('TestMyOwnIV1ForI');
// padding and truncating
const encryptedMessage = CryptoJS.AES.encrypt(JSON.stringify(model), key, {
keySize: 128 / 8,
iv: iv,
mode: CryptoJS.mode.CBC,
padding: CryptoJS.pad.Pkcs7
}).toString();
return encryptedMessage;
}
在CORE中,我实现了如下的客户模型绑定提供程序:
In CORE I've implemented my customer model binding provider as follows:
public class DecryptModelBinderProvider : IModelBinderProvider
{
public IModelBinder GetBinder(ModelBinderProviderContext context)
{
if (context.Metadata.ModelType == typeof(MyModel))
return new DecryptModelBinder();
return null;
}
}
然后我自己的解密模型绑定器如下:
Then my own decryptmodelbinder as follows:
public class DecryptModelBinder : IModelBinder
{
public Task BindModelAsync(ModelBindingContext bindingContext)
{
if (bindingContext == null)
throw new ArgumentNullException(nameof(bindingContext));
using (var sr = new StreamReader(bindingContext.HttpContext.Request.Body))
{
string valueFromBody = sr.ReadToEnd();
if (valueFromBody != null && valueFromBody.Length > 0)
{
var decrypted = Encryption.DecryptString(valueFromBody, "TestMyOwnKeyForI");
var model = JsonConvert.DeserializeObject(decrypted, bindingContext.ModelType);
bindingContext.Result = ModelBindingResult.Success(model);
bindingContext.Model = model;
}
}
return Task.CompletedTask;
}
}
这篇关于使用javascript加密并使用AES算法在C#中解密的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!