Java TripleDES PKCS5Padding解密到C# - 错误的数据/填充错误

时间:2017-08-14 20:57:21

标签: encryption cryptography tripledes

我正在尝试为以下Java代码编写C#等价物:

protected static final String DES_ECB_PKCS5PADDING = "DESede/ECB/PKCS5Padding";

 public static String decryptValueDirect(String value, String key)
            throws NoSuchAlgorithmException, NoSuchPaddingException,
            GeneralSecurityException, IllegalBlockSizeException,
            BadPaddingException {
        byte[] bytes = Base64.decodeBase64(value);
        Cipher cipher = Cipher.getInstance(DES_ECB_PKCS5PADDING);
        cipher.init(Cipher.DECRYPT_MODE, convertSecretKey(key.getBytes()));
        byte[] decryptedValue = cipher.doFinal(bytes);

        String nstr =  new String(decryptedValue);
        return nstr;
    }
protected static SecretKey convertSecretKey(byte[] encryptionKey) throws GeneralSecurityException {
        if (encryptionKey == null || encryptionKey.length == 0)
            throw new IllegalArgumentException("Encryption key must be specified");

        SecretKeyFactory keyFactory = SecretKeyFactory.getInstance(TRIPLEDES);
        KeySpec keySpec = new DESedeKeySpec(encryptionKey);
        return keyFactory.generateSecret(keySpec);
    }

源文本是base64编码,然后加密,然后base64编码,以便在兔子队列上传输。我们处理加密的供应商提供了上述解密功能,但不了解C#。

加密端的唯一输入是密钥,随机字符串。我们在开发环境中使用相同的字符串进行加密/解密012345678901234567890123456789。这是唯一的输入,没有盐,散列(我看到)或pw迭代。唯一的要求是它至少有24个字符。

我的C#代码在下面,我的尝试fiddle就在这里。

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

public class Program
{
    public static void Main()
    {
        //Message Data value
        //We are using encrypted multibyte.     
        string myData = @"ROE8oYeV7B6faUsvfIx0Xe55vSs9IR5DlWGRbSM+lmKmLcaJsA13VudwWlAEYtLUD8+nMXShky0grSxsk0Z9cQe5V45XnAIfUhnyzI9a0jtMFC8XnIZ5dbclPO/V73QnieIZDkbNV5cPo3BM+l79ai96KB/gkF3xuerFPxvWejtPyWbOyO+FfNyFps4gAYDITsYIAEH39VP4eipmQ5zc18BA39lajQ3UaVewSxz7H+x3Ooe2SzJT/TQWRkioJSEFwexqzkHiLOQ0MOCIVD9xTWpLYnsL3LMwyF6H8f0PY4Fc57LVGhvUZ7dsB9NWUAnmG3uqbsonNFVhuXyvJTWNyFOHwFzOMx6XDLJJFHGZhaHg2VrescfnpUtonQY08RgojBngyJNRqK8URAvI3bqKq8Y7F/9HmEtMIIQe6KuuTmU=";
        string myKey = "012345678901234567890123456789";//Development Env Key.
        Console.WriteLine("Decrypt1:");
        string s = Decrypt1(myData, myKey);
        Console.ReadLine();
    }

    public static string Decrypt1(string value, string decryptionKey)
    {
        string decryptString = "";
        TripleDESCryptoServiceProvider tDESalg = new TripleDESCryptoServiceProvider();
        MD5CryptoServiceProvider hashMD5Provider = new MD5CryptoServiceProvider();
        try
        {
            byte[] decodedData = Convert.FromBase64String(value);
            tDESalg.Mode = CipherMode.ECB;
            tDESalg.Padding = PaddingMode.PKCS7;//According to MS, same as PKCS5PADDING

            byte[] Key = hashMD5Provider.ComputeHash(Encoding.UTF8.GetBytes(decryptionKey));
            //byte[] IV = tDESalg.IV;
            byte[] IV = new byte[tDESalg.BlockSize / 8]; //The size of the IV property must be the same as the BlockSize property divided by 8

            var memoryStream = new MemoryStream(decodedData);
            var cryptoStream = new CryptoStream(memoryStream, tDESalg.CreateDecryptor(Key, IV), CryptoStreamMode.Read);
            var reader = new StreamReader(cryptoStream);
            decryptString = reader.ReadToEnd();
            byte[] decryptData = Convert.FromBase64String(decryptString);
        }
        catch (Exception e)
        {
            Console.WriteLine("A Cryptographic error occurred: {0}", e.Message + e.StackTrace);
            return null;
        }

        return decryptString;
    }

}

搜索似乎指向相同的答案,密钥,编码,...都必须相同。我只是不知道提供的Java源代码是什么。 :)任何建议都会有所帮助。

1 个答案:

答案 0 :(得分:0)

MD5具有16字节输出,Triple DES(3DES)需要24字节密钥。密钥大小不匹配。

C#和Java密钥派生有很大不同:

  

C#:
  byte[] Key = hashMD5Provider.ComputeHash(Encoding.UTF8.GetBytes(decryptionKey));
  返回16个字节。

     

爪哇:
  SecretKeyFactory.getInstance(TRIPLEDES)
  返回24个字节。

有一个关键选项(2TDEA),其中使用了一个16字节的密钥,并且将复制前8个字节以创建最后的8个字节。 NIST已弃用此选项。

某些实现将接受16字节密钥并将密钥扩展为24字节,某些密钥不会。您应该向3DES提供所有24个字节,不要依赖实现来创建24字节密钥。

  

注意:问题已更新,因此不清楚实际的加密密钥是否已派生。