C# Adyen,无法获得正确的SHA-256加密

C# Adyen,无法获得正确的SHA-256加密,c#,encryption,sha,payment-processing,adyen,C#,Encryption,Sha,Payment Processing,Adyen,我使用签名字符串和私钥生成一个使用SHA-256加密的公钥 哈希函数是标准的C SHA-256哈希函数: string CalculateHMAC256(string hmacKey, string signingstring) { byte[] key = Encoding.UTF8.GetBytes(hmacKey); byte[] data = Encoding.UTF8.GetBytes(signingstring); using

我使用签名字符串和私钥生成一个使用SHA-256加密的公钥

哈希函数是标准的C SHA-256哈希函数:

string CalculateHMAC256(string hmacKey, string signingstring)
    {
        byte[] key = Encoding.UTF8.GetBytes(hmacKey);
        byte[] data = Encoding.UTF8.GetBytes(signingstring);
        using (HMACSHA256 hmac = new HMACSHA256(key))
        {
            byte[] result = hmac.ComputeHash(data);
            return Convert.ToBase64String(result);
        }
    }
字符串签名如下所示: 允许的方法:blockedMethods:countryCode:currencyCode:merchantAccount:merchantReference:offset:orderData:paymentAmount:sessionValidity:ShippeForedate:ShopperMail:shopperLocale:shopperReference:skinCode::GB:GBP:MyTest:abc::1:2017-04-28815:07:10+01:00:2017-04-30::en_US::neDRF4H4

我也尝试过使用转义“:”作为建议: 允许的方法:阻止的方法:国家代码:货币代码:商品计数:商品参考:抵销:订单数据:付款金额:会话有效期:发货日期:shopperEmail:shopperLocale:shopperReference:skinCode::GB:GBP:MyTest:abc::1:2017-04-29T13:34:48+01:00:2017-05-01::en_US::neDRF4H4

然后我的html表单如下所示:

 <form ngNoForm name="frmsp" id="frmsp" target="_blank" 
    action="https://test.barclaycardsmartpay.com/hpp/pay.shtml"
    method="post">
    <input type="hidden" name="merchantSig" [value]="merchantSignature" />
    <input type="hidden" name="currencyCode" [value]="smartPayment?.currencyCode" />
    <input type="hidden" name="merchantAccount" [value]="smartPayment?.merchantAccount" />
    <input type="hidden" name="merchantReference" [value]="smartPayment?.merchantReference" />
    <input type="hidden" name="paymentAmount" [value]="smartPayment?.paymentAmount" />
    <input type="hidden" name="sessionValidity" [value]="smartPayment?.sessionValidity" />
    <input type="hidden" name="shipBeforeDate" [value]="smartPayment?.shipBeforeDate" />
    <input type="hidden" name="shopperLocale" [value]="smartPayment?.shopperLocale" />
    <input type="hidden" name="orderData" [value]="smartPayment?.orderData" />
    <input type="hidden" name="skinCode" [value]="smartPayment?.skinCode" />
    <input type="hidden" name="countryCode" [value]="smartPayment?.countryCode" />
    <input type="hidden" name="shopperEmail" [value]="smartPayment?.shopperEmail" />
    <input type="hidden" name="shopperReference" [value]="smartPayment?.shopperReference" />
    <input type="hidden" name="allowedMethods" [value]="smartPayment?.allowedMethods" />
    <input type="hidden" name="blockedMethods" [value]="smartPayment?.blockedMethods" />
    <input type="hidden" name="offset" [value]="smartPayment?.offset" />    
    <input type="submit" value="Process Payment" (click)="buttonClicked()">
其中字段值是使用angular2计算的-使用与构建签名字符串相同的字段值,因此我确信数据匹配

当我发送表单时,我收到一个错误,说明商户签名不正确

签名字符串的格式可能不正确? 我正在尝试使用托管支付实现对巴克莱卡智能支付的呼叫。此处的文档: 托管支付页面集成指南


注:巴克莱银行文件已于2012年最后更新,对SHA-1的引用现在使用SHA-256。它是使用Adyen支付系统构建的,我使用以下示例代码重用了字符串生成示例:

您的签名字符串有问题。在所有值中,您需要将\替换为\和:替换为:

我还建议使用此示例中的代码生成并创建加密签名

使用下面的代码,我得到了与Adyen提供的测试页面上相同的签名

 // Computes the Base64 encoded signature using the HMAC algorithm with the HMACSHA256 hashing function.
    string CalculateHMAC(string hmacKey, string signingstring)
{
    byte[] key = PackH(hmacKey);
    byte[] data = Encoding.UTF8.GetBytes(signingstring);

    try
    {
        using (HMACSHA256 hmac = new HMACSHA256(key))
        {
            // Compute the hmac on input data bytes
            byte[] rawHmac = hmac.ComputeHash(data);

            // Base64-encode the hmac
            return Convert.ToBase64String(rawHmac);
        }
    }
    catch (Exception e)
    {
        throw new Exception("Failed to generate HMAC : " + e.Message);
    }
}

byte[] PackH(string hex)
{
    if ((hex.Length % 2) == 1)
    {
        hex += '0';
    }

    byte[] bytes = new byte[hex.Length / 2];
    for (int i = 0; i < hex.Length; i += 2)
    {
        bytes[i / 2] = Convert.ToByte(hex.Substring(i, 2), 16);
    }

    return bytes;
}

我现在也在做同样的事情:你在checkhmac页面上尝试过这个表单吗?太好了,希望我们能一起解决这个问题。是的。我可以通过在该页面上生成签名,然后创建一个类似于上面的html表单来匹配测试页面中的那些值来让它工作。问题是,我永远无法获得与该页面上生成的签名相匹配的商户签名。我实际上使用的是barclays smartpay测试页面,我想它与您所说的页面基本相同。您是否将您的签名字符串与测试页面上生成的签名字符串进行了比较?我看到您的签名字符串对符:应替换为\:我尝试了以下签名字符串:allowedMethods \:blockedMethods \:countryCode\:currencyCode\:merchantAccount\:merchantReference\:offset\:orderData\:paymentAmount\:sessionValidity\:ShipperForedate\:ShopperMail\:shopperLocale\:shopperReference\:skinCode\:\:MyTest\:abc\:\:GBP\:1\:2017-04-29T13\:34\:48+01\:00\:2017-05-01\:01\:en\u US\:neDRF4H4我需要转义日期吗html表单?我已尝试使用测试页中的签名字符串,然后使用该签名字符串获取签名,但这给了我一个错误的SHA-256加密。您只需在值中替换:即可!这个:被用作分隔符。你是我的英雄。谢谢你!我已经在这上面呆了很长时间了。实际上,我的代码中有另一个方法,但当我的字符串没有正确转义时,它就不起作用了。也许你也可以在回答中提到逃跑。
currencyCode:merchantAccount:merchantReference:paymentAmount:sessionValidity:shipBeforeDate:shopperLocale:skinCode