Securepay支付网关集成问题

时间:2012-09-10 10:44:35

标签: c# asp.net cryptography payment-gateway sha

我正在使用我的ASP.NET网页进行Securepay集成,根据他们的文档,我从以下信息生成SHA1:

指纹是上述字段的SHA1哈希加上此订单中的SecurePay交易密码,带有管道分隔符“|”:

  1. EPS_MERCHANTID
  2. 交易密码(由SecurePay支持提供)
  3. EPS_TXNTYPE(为0)
  4. EPS_REFERENCEID(用于测试目的123)
  5. EPS_AMOUNT(用于测试目的100.00)
  6. EPS_TIMESTAMP(用于测试目的20120910203805)
  7. 虽然我已遵循上述指示,但每当我付款时,它都会显示“ 无效指纹 ”。示例代码:

    FormsAuthentication
      .HashPasswordForStoringInConfigFile("xxx|xxx|0|123|100.00|201‌20910203805","sha1")
      .ToLower();`
    

3 个答案:

答案 0 :(得分:1)

检查您是否正确结束了这一行,并使用尾随“|”或删除不必要的尾随'|'。

同时检查您使用的方法是否在方法中添加了任何会扭曲您所期望的内容的内容。 (我想根据你所使用的特定机器制作的盐不知道它是否会这样做)

我一直在尝试使用此处生成哈希http://shagenerator.com/

ABC|password|1|Te‌​st Reference|1.00|20120912123421

给出:

25a1804285bafc078f45e41056bcdc42e0508b6f

您可以使用我的输入获得与您的代码相同的密钥吗?

更新

您可以尝试使用此方法代替HashPasswordForStoringInConfigFile(),看看您是否接近:

private string GetSHA1String(string text)
{
    var UE = new UnicodeEncoding();
    var message = UE.GetBytes(text);

    var hashString = new SHA1Managed(); 
    var hex = string.Empty;

    var hashValue = hashString.ComputeHash(message); 
    foreach (byte b in hashValue)
    { 
        hex += String.Format("{0:x2}", b);
    } 

    return hex;
}

更新2:

检查您的编码,我发现我可以将哈希输出与:

匹配
var UE = new UTF8Encoding();

更新3:

以下代码在控制台应用程序中为我工作,我看到哈希生成相同的值,并且我能够将输出与http://shagenerator.com/进行比较:

using System;
using System.Security.Cryptography;
using System.Text;
using System.Web.Security;

namespace SecurepayPaymentGatewayIntegrationIssue
{
    class Program
    {
        static void Main(string[] args)
        {
            var text = @"ABC|password|1|Te‌​st Reference|1.00|20120912123421";
            Console.WriteLine(GetSHA1String(text));

            Console.WriteLine(FormsAuthentication.HashPasswordForStoringInConfigFile(text, "sha1").ToLower());

            Console.ReadKey();
        }

        private static string GetSHA1String(string text)
        {
            var UE = new UTF8Encoding();// ASCIIEncoding(); // UnicodeEncoding();
            var message = UE.GetBytes(text);

            var hashString = new SHA1Managed();
            var hex = string.Empty;

            var hashValue = hashString.ComputeHash(message);
            foreach (byte b in hashValue)
            {
                hex += String.Format("{0:x2}", b);
            }

            return hex;
        }
    }
}

答案 1 :(得分:0)

我有同样的问题,我用下面的方法解决了这个问题:

 private string GetSHA1HashData(string data)
        {   

            SHA1 sha1 = new SHA1CryptoServiceProvider();

            //convert the input text to array of bytes
            byte[] hashData = sha1.ComputeHash(Encoding.Default.GetBytes(data));

            //create new instance of StringBuilder to save hashed data
            StringBuilder returnValue = new StringBuilder();

            //loop for each byte and add it to StringBuilder
            for (int i = 0; i < hashData.Length; i++)
            {                
                returnValue.Append(hashData[i].ToString("x2"));
            }

            // return hexadecimal string
            return returnValue.ToString();
        }

在页面加载中我将此返回值转换为小写。因为上面的方法返回值大写

string VAL="ABC|password|1|Te‌​st Reference|1.00|20120912123421";
fingerPrint = GetSHA1HashData(VAL);

答案 2 :(得分:0)

将时间用作现在的UTC: string epsTimestamp = DateTime.UtcNow.ToString(@"yyyyMMddHHmmss");

还要确保已使用sha1进行加密:

string data = EpsMerchant + "|" + EpsPassword + "|" + EpsTxnType + "|" + 
EpsReferenceId + "|" + EpsAmount + "|" + epsTimestamp;
string epsFingerprint = GetSha1String(data);

下面是获取sha1的代码

string GetSha1String(string input)
        {
            StringBuilder stringBuilder = new StringBuilder();
            foreach (byte b in GetHash(input))
                stringBuilder.Append(b.ToString("X2"));
            return stringBuilder.ToString();
        }

    public static byte[] GetHash(string inputString)
    {
        HashAlgorithm obj = SHA1.Create();
        return obj.ComputeHash(Encoding.UTF8.GetBytes(inputString));
    }
相关问题