AES解密问题-从SagePay返回密码
本文关键字:SagePay 返回 密码 解密 问题 AES | 更新日期: 2023-09-27 18:11:28
我正试图解密从SagePay表单发回的字符串,并在解密后获得垃圾。我不知道为什么。我的解密码是:
public string AESdecrypt(string input)
{
return Encoding.UTF8.GetString(AESdecrypt(Convert.FromBase64String(input)));
}
public byte[] AESdecrypt(byte[] input)
{
//PasswordDeriveBytes pdb = new PasswordDeriveBytes("hjiweykaksd", new byte[] { 0x43, 0x87, 0x23, 0x72, 0x45, 0x56, 0x68, 0x14, 0x62, 0x84 });
MemoryStream ms = new MemoryStream();
Aes aes = new AesManaged();
aes.Padding = PaddingMode.PKCS7;
aes.Mode = CipherMode.CBC;
aes.KeySize = 128;
aes.BlockSize = 128;
aes.Key = Encoding.UTF8.GetBytes(conf["ENCRYPTPWD"]);
aes.IV = Encoding.UTF8.GetBytes(conf["ENCRYPTPWD"]);
CryptoStream cs = new CryptoStream(ms, aes.CreateDecryptor(), CryptoStreamMode.Write);
cs.Write(input, 0, input.Length);
return ms.ToArray();
}
对加密的调用是:
uncrypt = _sagePay.AESdecrypt(Request.QueryString["crypt"].Replace("@"," "));
解密后得到的基本上是垃圾
��z%3A %25�H��%0E�nS�%0A�M%01��)f%1F%13��D'����3l�%7F����2���%1F�%07gR�%0BoJ%05���c� %25Fm�hL��ݕz|�e�q���%3F���%24m�w%10�l*I����P%03%1F>�>`�ڸQ�%00I�����%0D�{%0D%0C�|%0D�p����)�9�%23�W4���wQo%3A�����.�V1�%0Dc��%3B%1F%15�M�a`o�B%0F%06�S�7H�%0D%15�H�A P)%26�ߵ4%17���C�}�g�7X��W'%11�H�Y�%2C��%08�xV%7FJ"��*%0A(2ޓ�%01%2F0��'�xcy��c�<�"%24%0AaX6���Y�%2Ci��O؇���UF%3F%14i��%3D'z%3B��{R<h8%1B%3A}��y�t5���Z�r�%00%07Z%3A�8�Ch�T��%11U�������Lv�]1j�E�ҳҖ{ �cq��4��%09T��as%1CO���B�M%3D�e�0%19|�%0C�.õ%0B�U�%14X��y���h%23%05!k�*����k%03��%10�C%1A�Z%25 %18%0Ci�%3F%0F�%12��l�̓�%2F��%0E%05�%02�Pc%02-����tAa%1C*%03�X>W�%3D%1C�%3Blg���8�1z�E�%2F�|�%17%06�%19N��aO%3F%12c�A�%40�%3Bt�%00�uɈ�2�%1F
我假定要么我编码/解码错误,要么我在解密过程中丢失了一些东西。
下面是一些c#代码,用于解密SagePay规范文档中A1.2节给出的示例。这些代码大部分是你的代码。
using System;
using System.IO;
using System.Security.Cryptography;
using System.Text;
using System.Runtime.Remoting.Metadata.W3cXsd2001;
namespace SagePayExample
{
class SagePayExample
{
static String CipherHex = "@2DCD27338114D4C39A14A855702FBAB2EF40BCAC2D76A3ABC0F660A07E9C1C921C2C755BA9B59C39F882FBF6DFED114F23141D94E50A01A665B1E31A86C07CA1CD1BB8EF5B6CF2C23D495CD79F9C0F678D61773E7A1AA30AA5B23D56503FC0B52AC0694A8C341263D2C5FE1BAD93BDB94726761E155E900448F644AF1F67BE1AC77E852B9D90809A44F258EE9478B6D8C1C4ED58759263E7DBF8871C6592287C0358F36F4EEC326CEDDD440DA2FED8AB35F1B630A5C6FA671E4D78CC8CACECF9DFDC31D6C5EC8270FB21E297E2C2E14F99A04223EFFD4F00062D440E78A3D2C7140EC8F123D247B75E7482AE98858DA34D37EDE6D7C69AA74391F559305CF675ADB3615244A107ABBB6AF26E29A2FFA059B12688D90FE09E0DE069325BFF3587A695F5DA36E4B809B69CC9A37034F166B63B5A62B986F4DA34E9AC9516AFDE70642EC7DAD1AEBA93A1F347D6AC7046E967DCBFE7ACFCEE5DAFC0B29F1765032B3060EBE565CBD57D092075D15CF12725199C6881605B2E0F105698CE3ADD04361CA9D620C187B90E3F9849445B5C3C0FDF1768BFFD61F97E51316826F4F10E0E3E668F0A9F5ED9CCDA6F2C7CC957F12DB48F9041482E3D035E7A91852C404BFA325FED947E71F57B871DFAC6AF4FF29F4513A4A80B2D7ECC9D19D47ED04FA99CDFC881DFA771E1EA4F3F9B2C5AC673EF3DA2699A309CC8522993A63CB8D45D3CDF09B1DFDC573CD19679B250AD6721450B5042F201670B464505DCAEF59E2C67ABACC9AE2EEE793CE191FEBF66B8FAF4204EFFB359246B9C99FB52805C46375FF35140F74707FBC73C7731A28A2C883A";
static String ENCRYPTPWD = "55a51621a6648525";
public byte [] HexDecode(String HexString) {
HexString = HexString.Replace ("@", "");
SoapHexBinary converter = SoapHexBinary.Parse (HexString);
return converter.Value;
}
public string AESdecrypt(string input)
{
return Encoding.UTF8.GetString(AESdecrypt(HexDecode (input)));
}
public byte[] AESdecrypt(byte[] input)
{
MemoryStream ms = new MemoryStream();
Aes aes = new AesManaged();
aes.Padding = PaddingMode.PKCS7;
aes.Mode = CipherMode.CBC;
aes.KeySize = 128;
aes.BlockSize = 128;
aes.Key = Encoding.UTF8.GetBytes(ENCRYPTPWD);
aes.IV = Encoding.UTF8.GetBytes(ENCRYPTPWD);
CryptoStream cs = new CryptoStream(ms, aes.CreateDecryptor(), CryptoStreamMode.Write);
cs.Write(input, 0, input.Length);
cs.FlushFinalBlock ();
return ms.ToArray();
}
public static void Main (string[] args)
{
var example = new SagePayExample ();
var Decrypted = example.AESdecrypt (CipherHex);
Console.WriteLine (Decrypted);
}
}
}