Clickbank CBC-AES-256解密在c# [英] Clickbank CBC-AES-256 decryption in c#

查看:323
本文介绍了Clickbank CBC-AES-256解密在c#的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是这个加密和解密技术的新手。



我正在处理Clickbank的任务即时通知系统。我从Clickbank获取加密的值,我想解密通知。我的问题非常类似于 this 线程,但它不为我工作。它抛出以下错误:


指定的初始化向量(IV)与此算法的块大小不匹配。


下面是我的解密代码。

 的Page_Load(对象发件人,EventArgs五)
{
//样本响应
串sContent ={\notification\:\18XR9s5fwkbhvfriqYS6jDJERf ++ jshcTDQX4NuUoUHtS + YzfMCNiEvmIVNxkbT5My2xWLFPB9mb\\\
EjwpHd3A6b9WJDYiXc0nufTxhXDAL1JzyYryEZAq7Bogj7mHjxUfFhc419wDmQteoSEz4H0IsKha \\\
IoxSfA5znd6WZKCSY9Dxx0wbZ8jLNL8SOYxi7pbFdKgMgKULKEh4EPKaWAvhE5UjWtzuHvMX37NI\\\
OvApkBoYEDE2mrde / SjLigE38X2wsGB4M6pYVkfzEE6rbYfVNxadkNHmri1xlaa + Grudy6vt6wzq\\\
PUfroEb6uRlxj2e6dmKZE4kynJFmRosMJ4ZRC + SYW + DyvkbdSY2dl1ZMNPhP + yhcMkbU8HQKUipw\\\
d7FUpb6utfiDB8YL5z7pJMnjHP01PsIvG + eSj0Lfj1gmbtVJt6TOJ4BCZxZdfdPRlJtPdOUiMRRk\\\
Q3Wn5g9VuvzNYg2ostZ + / HE778M6lZ264KbpMZSqEj4cTPCGFFNt7VCz9fXVoDLa7oI7KGY6rgxb\\\
BLWXdX058RSd0gSzC8otkCx9b6p8FZ5XxAX4qbU814batcbxw3V3GGVf97VLSVysdrHc + PEFdocl\\\
qaRarCHG5e2ZpEgQLoCtRhA99qkuS9Uc9 + Hm1KT4kD2HIrPSclJWzUMoKuAG4n95EG0Q5ca0WZQx\\\
aLNhdPyJmSLNwjV / SNPxYdyy81ENZtLbwJOYENCnpd41z73HF91 / R1hrxQ0rCZsb6BBRGUeowEzE\\\
SKPSbWjDCQ6hLZTjObsOt6eTAmn8TrzjyqdwUfxHhLEtIQIOr4gPXxXqwGHYcNkRFezkwMScl2Hr\\ \\ nmJ + Zm1xCqs9 + fOOiO6TtZYKS + 9D1 / JevMfGdbcPw8 / 5F7 + ZVAkCcDS8OGaVv\,\iv\:\NDVEM0M4ODZGMzE4OTVENA == \};


使用(var reader = new StreamReader(Request.InputStream))
{
//使用以下两行进行实时测试
// JavaScriptSerializer js = new JavaScriptSerializer();
// sContent = reader.ReadToEnd();

JObject jObject = JObject.Parse(sContent);

JToken notification = jObject [notification];
JToken i = jObject [iv];


使用(StreamWriter _testData = new StreamWriter(Server.MapPath(〜/ data.txt),true))
{
_testData.WriteLine =======================通知); //写文件。
_testData.WriteLine(notification.ToString());
_testData.WriteLine(===================== IV); //写文件。
_testData.WriteLine(i.ToString());

string n = notification.ToString();
string v = i.ToString();

string sk =我的秘密钥匙;


byte [] key = Encoding.UTF8.GetBytes(sk);
byte [] iv = Encoding.UTF8.GetBytes(v);

try
{
using(var rijndaelManaged =
new RijndaelManaged {Key = key,IV = iv,Mode = CipherMode.CBC})
(var memoryStream =
new MemoryStream(Convert.FromBase64String(n)))
使用(var cryptoStream =
new CryptoStream(memoryStream,
rijndaelManaged.CreateDecryptor(key,iv)
CryptoStreamMode.Read))
{
var dString = new StreamReader(cryptoStream).ReadToEnd();
}
}
catch(CryptographicException ex)
{
Console.WriteLine(A Cryptographic error occurred:{0},ex.Message);

}
}
}
}

请让我知道我错了什么?



感谢

解决方案>

我刚刚完成敲我的头这个完全相同的问题,所以这里是我的解决方案。要记住的事情是密码/ secretkey由sha1哈希,并且前32个十六进制字符用作关键字节。抛弃我的部分是字符是UFT16字符在C#和实际上是2个字节,但不是源加密使用。所以我做的是将字符串转换为具有正确的字节数组的ASCII字符串。不是最干净的代码,但它的功能。

  public static string DecryptClickBankNotification(string cipherText,string passPhrase,string initVector)
{
string decryptptedString =空值;

byte [] inputBytes = Encoding.UTF8.GetBytes(passPhrase);

SHA1 sha1 = SHA1.Create();
byte [] key = sha1.ComputeHash(inputBytes);

StringBuilder hex = new StringBuilder(key.Length * 2);
foreach(key中的字节b)
hex.AppendFormat({0:x2},b);

string secondPhaseKey = hex.ToString()。Substring(0,32);

ASCIIEncoding asciiEncoding = new ASCIIEncoding();

byte [] keyBytes = asciiEncoding.GetBytes(secondPhaseKey);
byte [] iv = Convert.FromBase64String(initVector);

try
{
使用(RijndaelManaged rijndaelManaged = new RijndaelManaged
{
Key = keyBytes,
IV = iv,
模式= CipherMode.CBC,
Padding = PaddingMode.PKCS7})
使用(Stream memoryStream = new MemoryStream(Convert.FromBase64String(cipherText)))
using(CryptoStream cryptoStream =
new CryptoStream(memoryStream,
rijndaelManaged.CreateDecryptor(keyBytes,iv),
CryptoStreamMode.Read))
{
decryptedString = new StreamReader(cryptoStream).ReadToEnd();
}
}
catch(Exception ex)
{
Trace.WriteLine(new TraceData(TraceCategoryEnum.Errors,Error decrypting message:+ ex.Message) );
}

return decryptedString;
}


I'm new with this encryption and decryption techniques.

I'm working on a task with Clickbank Instant Notification system. I'm getting the encrypted values from Clickbank and I want to decrypt the notification. My problem is very similar to this thread but it is not working for me. It is throwing following error:

Specified initialization vector (IV) does not match the block size for this algorithm.

Below is my code for decryption.

protected void Page_Load(object sender, EventArgs e)
{
    //Sample response
    string sContent = "{\"notification\":\"18XR9s5fwkbhvfriqYS6jDJERf++jshcTDQX4NuUoUHtS+YzfMCNiEvmIVNxkbT5My2xWLFPB9mb\nEjwpHd3A6b9WJDYiXc0nufTxhXDAL1JzyYryEZAq7Bogj7mHjxUfFhc419wDmQteoSEz4H0IsKha\nIoxSfA5znd6WZKCSY9Dxx0wbZ8jLNL8SOYxi7pbFdKgMgKULKEh4EPKaWAvhE5UjWtzuHvMX37NI\nOvApkBoYEDE2mrde/SjLigE38X2wsGB4M6pYVkfzEE6rbYfVNxadkNHmri1xlaa+Grudy6vt6wzq\nPUfroEb6uRlxj2e6dmKZE4kynJFmRosMJ4ZRC+sYW+DyvkbdSY2dl1ZMNPhP+yhcMkbU8HQKUipw\nd7FUpb6utfiDB8YL5z7pJMnjHP01PsIvG+eSj0Lfj1gmbtVJt6TOJ4BCZxZdfdPRlJtPdOUiMRRk\nQ3Wn5g9VuvzNYg2ostZ+/HE778M6lZ264KbpMZSqEj4cTPCGFFNt7VCz9fXVoDLa7oI7KGY6rgxb\nBLWXdX058RSd0gSzC8otkCx9b6p8FZ5XxAX4qbU814batcbxw3V3GGVf97VLSVysdrHc+PEFdocl\nqaRarCHG5e2ZpEgQLoCtRhA99qkuS9Uc9+Hm1KT4kD2HIrPSclJWzUMoKuAG4n95EG0Q5ca0WZQx\naLNhdPyJmSLNwjV/SNPxYdyy81ENZtLbwJOYENCnpd41z73HF91/R1hrxQ0rCZsb6BBRGUeowEzE\nSKPSbWjDCQ6hLZTjObsOt6eTAmn8TrzjyqdwUfxHhLEtIQIOr4gPXxXqwGHYcNkRFezkwMScl2Hr\nmJ+Zm1xCqs9+fOOiO6TtZYKS+9Dl/JevMfGdbcPw8/5F7+ZVAkCcDS8OGaVv\",\"iv\":\"NDVEM0M4ODZGMzE4OTVENA==\"}";


    using (var reader = new StreamReader(Request.InputStream))
    {
        //Using below two lines for live testing
        //JavaScriptSerializer js = new JavaScriptSerializer();
        //sContent = reader.ReadToEnd();

        JObject jObject = JObject.Parse(sContent);

        JToken notification = jObject["notification"];
        JToken i = jObject["iv"];


        using (StreamWriter _testData = new StreamWriter(Server.MapPath("~/data.txt"), true))
        {
            _testData.WriteLine("=======================Notification"); // Write the file. 
            _testData.WriteLine(notification.ToString());
            _testData.WriteLine("=======================IV"); // Write the file. 
            _testData.WriteLine(i.ToString());

            string n = notification.ToString();
            string v = i.ToString();

            string sk = "MY SECRET KEY";


            byte[] key = Encoding.UTF8.GetBytes(sk);
            byte[] iv = Encoding.UTF8.GetBytes(v);

            try
            {
                using (var rijndaelManaged =
                       new RijndaelManaged { Key = key, IV = iv, Mode = CipherMode.CBC })
                using (var memoryStream =
                       new MemoryStream(Convert.FromBase64String(n)))
                using (var cryptoStream =
                       new CryptoStream(memoryStream,
                           rijndaelManaged.CreateDecryptor(key, iv),
                           CryptoStreamMode.Read))
                {
                    var dString = new StreamReader(cryptoStream).ReadToEnd();
                }
            }
            catch (CryptographicException ex)
            {
                Console.WriteLine("A Cryptographic error occurred: {0}", ex.Message);

            }
        }
    }
}

Please let me know where I'm wrong?

Thanks

解决方案

I just finished banging my head on this exact same problem, so here is my solution. The thing to keep in mind is the passphrase/secretkey gets hashed by sha1 and the first 32 hex characters are used as the key bytes. The part that was throwing me off was the characters are UFT16 characters in c# and were actually 2 bytes each but not what the source encryption used. So what I did was convert the string to a ASCII string which had the correct byte array. Not the cleanest code but it's functional.

    public static string DecryptClickBankNotification(string cipherText, string passPhrase, string initVector)
    {
        string decryptedString = null;

        byte[] inputBytes = Encoding.UTF8.GetBytes(passPhrase);

        SHA1 sha1 = SHA1.Create();
        byte[] key = sha1.ComputeHash(inputBytes);

        StringBuilder hex = new StringBuilder(key.Length * 2);
        foreach (byte b in key)
            hex.AppendFormat("{0:x2}", b);

        string secondPhaseKey = hex.ToString().Substring(0,32);

        ASCIIEncoding asciiEncoding = new ASCIIEncoding();

        byte[] keyBytes = asciiEncoding.GetBytes(secondPhaseKey);
        byte[] iv = Convert.FromBase64String(initVector);

        try
        {
            using (RijndaelManaged rijndaelManaged = new RijndaelManaged
            { 
                Key = keyBytes,
                IV = iv,
                Mode = CipherMode.CBC, 
                Padding = PaddingMode.PKCS7})
                using (Stream memoryStream = new MemoryStream(Convert.FromBase64String(cipherText)))
                using (CryptoStream cryptoStream =
                   new CryptoStream(memoryStream,
                       rijndaelManaged.CreateDecryptor(keyBytes, iv),
                       CryptoStreamMode.Read))
            {
                decryptedString = new StreamReader(cryptoStream).ReadToEnd();
            }
        }
        catch (Exception ex)
        {
            Trace.WriteLine(new TraceData(TraceCategoryEnum.Errors, "Error decrypting message: " + ex.Message));
        }

        return decryptedString;
    }

这篇关于Clickbank CBC-AES-256解密在c#的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆