环境是 asp.net core 3.1
目前我们接入了微信的发送代金券, 我们给用户发送了很多的代金券, 现在希望能收到用户消费代金券的回执信息, 所以这次接入核销事件回调通知
消费完代金券后收到微信推送过来的报文信息是
{
"id": "da6f347c-9160-56c8-9435-441919007f3a",
"create_time": "2020-04-28T21:11:36+08:00",
"resource_type": "encrypt-resource",
"event_type": "COUPON.USE",
"summary": "代金券核销通知",
"resource": {
"original_type": "coupon",
"algorithm": "AEAD_AES_256_GCM",
"ciphertext": "/////////这个还没有用到就报错了. 因此我就隐去了",
"associated_data": "coupon",
"nonce": "9xCooUyvImYw"
}
}
解密代码用的是腾讯官方的代码, 执行过程中报错了. 下面代码中写了注释的一行报错了
private static string ALGORITHM = "AES/GCM/NoPadding";
private static int TAG_LENGTH_BIT = 128;
private static int NONCE_LENGTH_BYTE = 12;
private static string AES_KEY = "Q9HBMJ2P0Z6N4F7V5DKUCAI8TSL3XE1YWGRO";
public static string AesGcmDecrypt(string associatedData, string nonce, string ciphertext)
{
GcmBlockCipher gcmBlockCipher = new GcmBlockCipher(new AesEngine());
AeadParameters aeadParameters = new AeadParameters(
new KeyParameter(Encoding.UTF8.GetBytes(AES_KEY)),
128,
Encoding.UTF8.GetBytes(nonce),
Encoding.UTF8.GetBytes(associatedData));
gcmBlockCipher.Init(false, aeadParameters);//执行到这里就报错了, 见下面的错误信息
byte[] data = Convert.FromBase64String(ciphertext);
byte[] plaintext = new byte[gcmBlockCipher.GetOutputSize(data.Length)];
int length = gcmBlockCipher.ProcessBytes(data, 0, data.Length, plaintext, 0);
gcmBlockCipher.DoFinal(plaintext, length);
return Encoding.UTF8.GetString(plaintext);
}
d堆栈信息
发生异常: CLR/System.ArgumentException
“System.ArgumentException”类型的异常在 BouncyCastle.Crypto.dll 中发生,但未在用户代码中进行处理: 'Key length not 128/192/256 bits.'
在 Org.BouncyCastle.Crypto.Engines.AesEngine.GenerateWorkingKey(Byte[] key, Boolean forEncryption)
在 Org.BouncyCastle.Crypto.Engines.AesEngine.Init(Boolean forEncryption, ICipherParameters parameters)
在 Org.BouncyCastle.Crypto.Modes.GcmBlockCipher.Init(Boolean forEncryption, ICipherParameters parameters)
在 NSwagJson.WeiXinCashCouponApi.Utils.AesGcm.AesGcmDecrypt(String associatedData, String nonce, String ciphertext) 在 /Users/jianjl/code/nswagjson.weixincashcouponapi/NSwagJson.WeiXinCashCouponApi/Utils/AesGcm.cs 中: 第 32 行
在 NSwagJson.WeiXinCashCouponApi.Controllers.CallBackController.CallBack(CouponCallBackRequest body) 在 /Users/jianjl/code/nswagjson.weixincashcouponapi/NSwagJson.WeiXinCashCouponApi/Controllers/CallBackController.cs 中: 第 80 行
在 Microsoft.Extensions.Internal.ObjectMethodExecutor.Execute(Object target, Object[] parameters)
在 Microsoft.AspNetCore.Mvc.Infrastructure.ActionMethodExecutor.SyncActionResultExecutor.Execute(IActionResultTypeMapper mapper, ObjectMethodExecutor executor, Object controller, Object[] arguments)
在 Microsoft.AspNetCore.Mvc.Infrastructure.ControllerActionInvoker.InvokeActionMethodAsync()
在 Microsoft.AspNetCore.Mvc.Infrastructure.ControllerActionInvoker.Next(State& next, Scope& scope, Object& state, Boolean& isCompleted)
在 Microsoft.AspNetCore.Mvc.Infrastructure.ControllerActionInvoker.InvokeNextActionFilterAsync()
谢邀,秘钥错了,aeskey是商户的apiv3key,长度是32位。如有帮助,多点几个有用。谢谢。
检查下是否用的接口获取的平台证书解密后的明文进行加密的