C# 使用asp.net网页加密加密密码字段以实现SagePay表单集成

C# 使用asp.net网页加密加密密码字段以实现SagePay表单集成,c#,asp.net,encryption,aes,opayo,C#,Asp.net,Encryption,Aes,Opayo,我总是得到一个错误:它是Crypt字段 <form action="@SagePaySettings.FormPaymentUrl" method="POST" id="gopayment" name="gopayment"> <input type="hidden" name="VPSProtocol" value="@SagePaySettings.ProtocolVersion.VersionString()"> <input

我总是得到一个错误:它是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位)加密,并使用提供的
密码作为密钥和初始化向量,并以十六进制编码结果(确保字母为大写)。

嗨,在分配搜索后,我已经处理好了这一点,因此希望显示答案,以防其他人需要它。 我毫无疑问,这可以做得更好,因为我只是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);

嗨,在分配了搜索之后,我已经处理了这个问题,所以我想给出答案,以防其他人需要这个。 我毫无疑问,这可以做得更好,因为我只是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);

你在使用SagePay提供的集成工具包吗?嗨@DavidG我已经安装了该工具包,但我从未使用过asp.net webForms,这是创建该工具包的地方,因此我在访问Crypt类时遇到问题。不,该工具包只是一个类库,无论是webForms、MVC还是控制台应用程序。我从未使用过类,这就是问题所在。我不知道调用它的正确方法,为它提供信息,那么你真的应该考虑在进入这个复杂的东西之前学习如何使用C.*。你在使用SavePayi提供的集成工具包吗?嗨,DavidG,我已经安装了这个工具包,但是我从来没有使用过ASP.NET WebFrand,这就是工具包的创建过程。所以我在访问Crypt类时遇到了问题。不,工具包只是一个类库,不管它是WebForms、MVC还是控制台应用程序。我从未使用过类,这就是问题所在。我不知道调用它的正确方法,为它提供信息,那么你真的应该考虑在进入这个复杂的东西之前学习如何使用C.*。我正在整合Opayo,形式整合,并且不确定这是否在版本4上工作,有人可以确认吗?我有一个注册付款的问题,但由于我有其他问题,无法判断是否是加密机制,因此无法确认上述功能是否有效,但我在v4.00中使用了类似的功能:我正在集成Opayo,表单集成,不确定这是否适用于4.00版,有人能确认吗?我在注册付款方面有问题,但是,由于我有其他问题,无法判断现在是否是加密机制,因此无法确认上述功能是否有效,但我在v4.00中使用了类似的功能: