【问题标题】:Encrypting Crypt field using asp.net webpages for SagePay Form Integration使用 asp.net 网页加密 Crypt 字段以进行 SagePay 表单集成
【发布时间】:2016-12-31 15:49:28
【问题描述】:

我总是得到一个错误:这是 Crypt 字段

 <form action="@SagePaySettings.FormPaymentUrl" method="POST" id="gopayment" name="gopayment">
        <input type="hidden" name="VPSProtocol" value="@SagePaySettings.ProtocolVersion.VersionString()">
        <input type="hidden" name="TxType" value="@SagePaySettings.DefaultTransactionType">
        <input type="hidden" name="Vendor" value="@SagePaySettings.VendorName">
        <input type="hidden" name="Crypt" value="@Crypt">

有人可以在 c# 中发送 asp.net 网页的加密例程吗?

sage pay 团队对此毫无帮助。

必须在 CBC 模式下使用 AES(块大小 128 位)加密,并使用提供的 PKCS#5 填充 密码作为密钥和初始化向量,并将结果编码为十六进制(确保字母为大写)。

【问题讨论】:

  • 您是否在使用 SagePay 提供的集成工具包?
  • 嗨@DavidG 我已经安装了工具包,但我从未使用过 asp.net webForms,这就是工具包的创建方式,所以我无法访问 Crypt 类。
  • 不,套件只是一个类库,不管是 WebForms、MVC 还是控制台应用程序。
  • 我从未使用过类,这就是问题所在。我不知道正确的称呼方式,提供信息
  • 那么在深入研究如此复杂的事物之前,您真的应该考虑学习如何使用 C#。

标签: c# asp.net encryption aes opayo


【解决方案1】:

嗨,经过大量搜索,我已经解决了这个问题,所以想显示答案,以防万一其他人需要这个。 我毫不怀疑这可以做得更好,因为我只是 asp 和 c# 的开始,但它有效。

using System;
using System.Linq;
using System.Security.Cryptography;
using System.Text;
using System.IO;

public static class EncryptionHelper
{
    private static byte[] keyAndIvBytes;

    static EncryptionHelper()
    {
        // You'll need a more secure way of storing this, I this isn't
        // a real key
        keyAndIvBytes = UTF8Encoding.UTF8.GetBytes("123123123123123b");
    }

    public static string ByteArrayToHexString(byte[] ba)
    {
        return BitConverter.ToString(ba).Replace("-", "");
    }

    public static byte[] StringToByteArray(string hex)
    {
        return Enumerable.Range(0, hex.Length)
                         .Where(x => x % 2 == 0)
                         .Select(x => Convert.ToByte(hex.Substring(x, 2), 16))
                         .ToArray();
    }

    public static string DecodeAndDecrypt(string cipherText)
    {
        string DecodeAndDecrypt = AesDecrypt(StringToByteArray(cipherText));
        return (DecodeAndDecrypt);
    }

    public static string EncryptAndEncode(string plaintext)
    {
        return ByteArrayToHexString(AesEncrypt(plaintext));
    }

    public static string AesDecrypt(Byte[] inputBytes)
    {
        Byte[] outputBytes = inputBytes;

        string plaintext = string.Empty;

        using (MemoryStream memoryStream = new MemoryStream(outputBytes))
        {
            using (CryptoStream cryptoStream = new CryptoStream(memoryStream, GetCryptoAlgorithm().CreateDecryptor(keyAndIvBytes, keyAndIvBytes), CryptoStreamMode.Read))
            {
                using (StreamReader srDecrypt = new StreamReader(cryptoStream))
                {
                    plaintext = srDecrypt.ReadToEnd();
                }
            }
        }

        return plaintext;
    }

    public static byte[] AesEncrypt(string inputText)
    {
        byte[] inputBytes = UTF8Encoding.UTF8.GetBytes(inputText);//AbHLlc5uLone0D1q

        byte[] result = null;
        using (MemoryStream memoryStream = new MemoryStream())
        {
            using (CryptoStream cryptoStream = new CryptoStream(memoryStream, GetCryptoAlgorithm().CreateEncryptor(keyAndIvBytes, keyAndIvBytes), CryptoStreamMode.Write))
            {
                cryptoStream.Write(inputBytes, 0, inputBytes.Length);
                cryptoStream.FlushFinalBlock();

                result = memoryStream.ToArray();
            }
        }

        return result;
    }


    private static RijndaelManaged GetCryptoAlgorithm()
    {
        RijndaelManaged algorithm = new RijndaelManaged();
        //set the mode, padding and block size
        algorithm.Padding = PaddingMode.PKCS7;
        algorithm.Mode = CipherMode.CBC;
        algorithm.KeySize = 128;
        algorithm.BlockSize = 128;
        return algorithm;
    }
}

我这样称呼这个类:-

string crypt = "blahblahblah";
string EncryptAndEncode = EncryptionHelper.EncryptAndEncode(crypt);
string DecodeAndDecrypt = EncryptionHelper.DecodeAndDecrypt(EncryptAndEncode);

【讨论】:

  • 我正在集成 Opayo,表单集成,但不确定这是否适用于 4.00 版,有人可以确认吗?
  • 我在注册付款时遇到了问题,但由于我有其他问题,现在无法确定是否是加密机制,因此无法确认上述工作是否有效,但我在 v4.0 中使用了类似的东西。 00:brightertools.com/post/…
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2013-11-18
  • 1970-01-01
  • 2013-11-08
  • 2013-05-20
  • 1970-01-01
  • 2013-11-11
  • 1970-01-01
相关资源
最近更新 更多