Rails 需要调用 ChinaPay 银联接口,但它没有提供 Ruby on Rails 的加密库

goody9807 · 2015年05月05日 · 最后由 liwen_zhang 回复于 2016年07月30日 · 2733 次阅读

谁能提供一个库,我看到 chinapay 有提供 Red hat Linux 的 库 so 的,不知道 ruby 怎么调用 或者调用 dll 库也行

我研究了一下库的源码,主要就是一个 buildkey 的方法,还有就是 sign 的方法

public bool buildKey(string MerID, int KeyUsage, string KeyFile)
{
    byte[] array = new byte[8];
    bool result = false;
    StreamReader streamReader = new StreamReader(KeyFile);
    try
    {
        string text = streamReader.ReadLine();
        if (text.CompareTo("[SecureLink]") == 0 || text.CompareTo("[NetPayClient]") == 0)
        {
            text = streamReader.ReadLine();
            int num = text.IndexOf("=");
            string text2 = text.Substring(0, num);
            text = text.Substring(num + 1);
            if (text.CompareTo(MerID) == 0)
            {
                if (KeyUsage == 0)
                {
                    text = streamReader.ReadLine();
                    text = text.Substring(8);
                }
                else
                {
                    text = streamReader.ReadLine();
                    text = streamReader.ReadLine();
                    text = text.Substring(8);
                }
                byte[] array2 = new byte[712];
                if (text.Length > 0)
                {
                    array2 = NetPay.Asc2Hex(text);
                }
                if (text2.CompareTo("PGID") != 0)
                {
                    byte[] array3 = new byte[64];
                    byte[] array4 = new byte[64];
                    byte[] array5 = new byte[64];
                    byte[] array6 = new byte[64];
                    byte[] array7 = new byte[64];
                    this.memset(array, 0, array.Length);
                    this.des.cbc_encrypt(array2, 424u, 64u, array3, 0u, array, false);
                    this.memset(array, 0, array.Length);
                    this.des.cbc_encrypt(array2, 488u, 64u, array4, 0u, array, false);
                    this.memset(array, 0, array.Length);
                    this.des.cbc_encrypt(array2, 552u, 64u, array5, 0u, array, false);
                    this.memset(array, 0, array.Length);
                    this.des.cbc_encrypt(array2, 616u, 64u, array6, 0u, array, false);
                    this.memset(array, 0, array.Length);
                    this.des.cbc_encrypt(array2, 680u, 64u, array7, 0u, array, false);
                    this.rsaPrivateKey = new RSAPrivateKey();
                    this.rsaPrivateKey.p = new BigInteger(array3);
                    this.rsaPrivateKey.q = new BigInteger(array4);
                    this.rsaPrivateKey.u = new BigInteger(array7);
                    this.rsaPrivateKey.dP = new BigInteger(array5);
                    this.rsaPrivateKey.dQ = new BigInteger(array6);
                    this.PrivateKeyFlag = true;
                    result = true;
                }
                else
                {
                    byte[] array8 = new byte[128];
                    byte[] array9 = new byte[128];
                    Array.ConstrainedCopy(array2, 24, array8, 0, 128);
                    Array.ConstrainedCopy(array2, 152, array9, 0, 128);
                    this.rsaPublicKey = new RSAPublicKey();
                    this.rsaPublicKey.n = new BigInteger(array8);
                    this.rsaPublicKey.e = new BigInteger(array9);
                    this.PublicKeyFlag = true;
                    result = true;
                }
            }
        }
    }
    catch (Exception)
    {
        return result;
    }
    finally
    {
        streamReader.Close();
    }
    return result;
}

我想如果用 ruby 改写下,以后 netpayclient 升级,我还需要修改,不如可以调用它的库方便,求大神指点下,谢谢了!

#1 楼 @rei 我看过这个,谢谢,貌似不太能用

@goody9807 你可以参考 这个 gem https://github.com/Yohansun/unionpay_app 这个是 前一段时间写的

国内的各家支付平台各网银是各种奇葩,曾碰到某银行 SDK 只支持 32 位 Windows server 2003 ....

我们的经验是 多几台服务器 把各家比较稳定容易维护和使用的 SDK 在自己支持的 OS 上包成 Service,再有一个单独的 Service 去调用这些 Service 来和业务系统对接。

楼主,你最终有用 ruby 改写吗?我现在也遇到这个困难

需要 登录 后方可回复, 如果你还没有账号请 注册新账号