-2

我们有一个 Java 客户端,它向我们发送一些加密数据 1. 使用 RSA 和我们离线提供的公钥对随机字符串进行加密。2. 使用在第 1 步中生成的密钥,他们使用 alg_tripleDES_CBC = http://www.w3.org/2001/04/xmlenc#tripledes-cbc加密数据

我能够像这样从第一步解密密钥......这是有效的。

public static string DecryptKey(string encryptedKey)
    {
        X509Certificate2 cert = new X509Certificate2("c:\\test.pfx", "test");
        RSACryptoServiceProvider privateKeyProvider = (RSACryptoServiceProvider)cert.PrivateKey;

        string decryptedKey = System.Text.Encoding.UTF8.GetString(privateKeyProvider.Decrypt(Convert.FromBase64String(encryptedKey), false));

        return decryptedKey;

    }

我有这段代码可以使用第一步生成的密钥解密数据。

 public static string DecryptString(string Message, string Passphrase)
    {
        byte[] Results;
        System.Text.UTF8Encoding UTF8 = new System.Text.UTF8Encoding();

        MD5CryptoServiceProvider HashProvider = new MD5CryptoServiceProvider();
        byte[] TDESKey = HashProvider.ComputeHash(UTF8.GetBytes(Passphrase));

        TripleDESCryptoServiceProvider TDESAlgorithm = new TripleDESCryptoServiceProvider();

        TDESAlgorithm.Key = TDESKey;
        TDESAlgorithm.Mode = CipherMode.ECB;
        TDESAlgorithm.Padding = PaddingMode.PKCS7;

        byte[] DataToDecrypt = Convert.FromBase64String(Message);

        try
        {
            ICryptoTransform Decryptor = TDESAlgorithm.CreateDecryptor();
            Results = Decryptor.TransformFinalBlock(DataToDecrypt, 0, DataToDecrypt.Length);
        }
        finally
        {
            TDESAlgorithm.Clear();
            HashProvider.Clear();
        }

        return UTF8.GetString(Results);
    }

第二步因此异常而失败。

   System.Security.Cryptography.CryptographicException: Bad Data.

   at System.Security.Cryptography.CryptographicException.ThrowCryptographicException(Int32 hr)
   at System.Security.Cryptography.Utils._DecryptData(SafeKeyHandle hKey, Byte[] data, Int32 ib, Int32 cb, Byte[]& outputBuffer, Int32 outputOffset, PaddingMode PaddingMode, Boolean fDone)
   at System.Security.Cryptography.CryptoAPITransform.TransformFinalBlock(Byte[] inputBuffer, Int32 inputOffset, Int32 inputCount)
   at ConsoleApplication3.Program.DecryptString(String Message, String Passphrase) in C:\Documents and Settings\rjaladi\Desktop\ConsoleApplication3\ConsoleApplication3\Program.cs:line 66
   at ConsoleApplication3.Program.Main(String[] args) in C:\Documents and Settings\rjaladi\Desktop\ConsoleApplication3\ConsoleApplication3\Program.cs:line 22 

我需要与我们的客户核实什么?我知道我们传递给 TDES 的参数有问题。有什么帮助吗?

编辑:加密消息的相应 Java 代码。

public String encryptText(String plainText) throws Exception{

  byte[] plaintext = plainText.getBytes();
  byte[] tdesKeyData = key;
  byte[] myIV = initializationVector;

  Cipher c3des = Cipher.getInstance(""DESede/CBC/NoPadding"");
  SecretKeySpec    myKey = new SecretKeySpec(tdesKeyData, "DESede");
  IvParameterSpec ivspec = new IvParameterSpec(myIV);
     c3des.init(Cipher.ENCRYPT_MODE, myKey, ivspec);
  byte[] cipherText = c3des.doFinal(plaintext);
  sun.misc.BASE64Encoder obj64=new sun.misc.BASE64Encoder();
  return obj64.encode(cipherText);
 }
4

2 回答 2

2

可能这会失败:

TDESAlgorithm.Mode = CipherMode.ECB;

如果您使用 CBC 加密 Java,则应使用 CBC 密码模式。无论如何也不建议使用ECB,因为它存在一些安全漏洞。

于 2011-12-19T20:18:55.840 回答
1

您的异常被抛出_DecryptData(...),我注意到其中包含一个PaddingMode参数。将检查最后一个块末尾的填充,如果发现不正确的填充,则会引发错误。我建议您与发送数据的人核实,以了解他们使用的填充模式。解密时需要使用相同的填充模式。

正如@klartrex 所说,你不应该使用 ECB 模式,它会泄露信息;请参阅此处以获取(文字)插图。如果您可以说服另一端这样做,请改用 CBC 或 CTR 模式。

于 2011-12-19T20:39:15.620 回答