简体   繁体   English

从 Java 转换为 C# - 使用 PKCS5Padding 解密 CBC-AES-256

[英]Convert from Java to C# - Decrypt CBC-AES-256 with PKCS5Padding

I'm trying to decrypt 'Instant Notifications' from ClickBank, they have code samples for decoding in several languages but not C# so I'm trying to translate the Java code: https://support.clickbank.com/hc/en-us/articles/220376507-Instant-Notification-Service-INS-#Code%20Samples我正在尝试从 ClickBank 解密“即时通知”,他们有用于解码多种语言的代码示例,但不是 C#,所以我正在尝试翻译 Java 代码: https : //support.clickbank.com/hc/en- us/articles/220376507-Instant-Notification-Service-INS-#Code%20Samples

I've tried several different ways over the last few days but no luck!在过去的几天里,我尝试了几种不同的方法,但没有运气! Help pls SO, what am I missing?请帮助,我错过了什么?

Java:爪哇:

final StringBuilder buffer = new StringBuilder();
final String secretKey = "YOUR SECRET KEY";


String line;
final BufferedReader reader = theRequest.getReader();
while(null != (line = reader.readLine())) {
   buffer.append(line);
}


   final JSONParser parser = new JSONParser();
   final JSONObject obj = (JSONObject) parser.parse(buffer.toString()); 

   final String initializationVector = (String) obj.get("iv");
   final String encryptedNotification = (String) obj.get("notification"); 
   final MessageDigest digest = MessageDigest.getInstance("SHA-1");
   digest.reset();
   digest.update(secretKey.getBytes("UTF-8"));
   final String key = new String(Hex.encodeHex(digest.digest())).substring(0, 32);


   final IvParameterSpec iv = new IvParameterSpec(DatatypeConverter.parseBase64Binary(initializationVector));
   final SecretKeySpec keySpec = new SecretKeySpec(key.getBytes(), "AES");
   final Cipher cipher = Cipher.getInstance("AES/CBC/PKCS5Padding");
   cipher.init(Cipher.DECRYPT_MODE, keySpec, iv);

   final JSONObject notification = (JSONObject) parser.parse(
   new String(cipher.doFinal(DatatypeConverter.parseBase64Binary(encryptedNotification)),
                 "ISO-8859-1"));`

C# C#

        const string secretKey = "YOUR SECRET KEY";
        string sContent = "";
        using (System.IO.Stream receiveStream = Request.InputStream)
        {
            receiveStream.Position = 0;
            using (System.IO.StreamReader readStream =
                   new System.IO.StreamReader(receiveStream, Encoding.UTF8))
            {
                sContent = readStream.ReadToEnd();
            }
        }
        dynamic json = System.Web.Helpers.Json.Decode(sContent);
        string initializationVector = json.iv;
        string encryptedNotification = json.notification;

        //turn the key into a fixed length string via SHA-1 hash
        SHA1 sha1 = SHA1Managed.Create();
        byte[] hash = sha1.ComputeHash(Encoding.UTF8.GetBytes(secretKey));
        var key = Org.BouncyCastle.Utilities.Encoders.Hex.Encode(hash);
        var keyString = Convert.ToBase64String(key).Substring(0, 32);
        //var key = BitConverter.ToString(hash).Substring(0, 32);

        var iv = Convert.FromBase64String(initializationVector);
        byte[] keyspec = Encoding.UTF8.GetBytes(keyString);

         using (var rijndaelManaged =
               new RijndaelManaged { Key = keyspec, IV = iv, Mode = CipherMode.CBC, Padding = PaddingMode.PKCS7 })
        using (var memoryStream =
               new MemoryStream(Convert.FromBase64String(encryptedNotification)))
        using (var cryptoStream =
               new CryptoStream(memoryStream,
                   rijndaelManaged.CreateDecryptor(keyspec, iv),
                   CryptoStreamMode.Read))
        {
            var result = new StreamReader(cryptoStream).ReadToEnd();
        }

gives the error - 'Padding is invalid and cannot be removed.'给出错误 - “填充无效,无法删除。”

So I'm guessing I must be creating the key wrong, I've tried different ways of hashing and hex encoding but none seem to work.所以我猜我一定是创建了错误的密钥,我尝试了不同的散列和十六进制编码方式,但似乎都不起作用。 Please advise guys!请各位大侠指教! Thanks for your time谢谢你的时间

Edit: removing useless extra code编辑:删除无用的额外代码

Oh dear, a bank that uses CBC to send messages.哦,天哪,一家使用 CBC 发送消息的银行。 This Java code sample looks like "my first attempt at crypto", and coming from a bank, that's not a bank that you should trust.这个 Java 代码示例看起来像“我第一次尝试加密”,并且来自银行,这不是您应该信任的银行。

The key is indeed wrong, you should try key = Hex.ToHexString() and then convert the sub string of the result to ASCII using Encoding.ASCII.GetBytes(key) .密钥确实是错误的,您应该尝试key = Hex.ToHexString() ,然后使用Encoding.ASCII.GetBytes(key)将结果的子字符串转换为 ASCII。 The additional base 64 or bit conversion is not present in the Java code and should not be used. Java 代码中不存在额外的 base 64 或位转换,不应使用。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM