简体   繁体   English

3DES(DESede) - 在C#中解密加密文本(由JAVA完成)

[英]3DES (DESede)- Decrypt encrypted text (done by JAVA) in C#

The encrypted text is done in JAVA (which we have no JAVA background at all) 加密文本在JAVA中完成(我们根本没有JAVA背景)

The decryption will be in C#, and here is the code 解密将在C#中,这是代码

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

    MD5CryptoServiceProvider HashProvider = new MD5CryptoServiceProvider();
    byte[] TDESKey = HashProvider.ComputeHash(UTF8.GetBytes(Passphrase));
    // byte[] TDESKey = UTF8.GetBytes(Passphrase);
    TripleDESCryptoServiceProvider TDESAlgorithm = new TripleDESCryptoServiceProvider();
    TDESAlgorithm.Key = TDESKey;
    // TDESAlgorithm.Mode = CipherMode.CTS;
    TDESAlgorithm.Padding = PaddingMode.Zeros;

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

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

Encrypted Java code is 加密的Java代码是

public  String encryptData(String privateKey, String rawData)  
{

    Cipher cipher = null;
    try 
    {
        cipher = Cipher.getInstance(DESEDE_ENCRYPTION_SCHEME);
        cipher.init(Cipher.ENCRYPT_MODE, getSecretKey(privateKey));
        byte[] plainText = rawData.getBytes(UNICODE_FORMAT);
        byte[] encryptedText = cipher.doFinal(plainText);
        return new String(Base64.encodeBase64(encryptedText));
    } 
}

However, when tried to decrypt, got the error message: BAD DATA 但是,当试图解密时,收到错误消息:BAD DATA

Where am I missing here? 我在哪里错过了?

You are not using MD5 in Java, so you should not be using it in your .NET for computing the hash. 您没有在Java中使用MD5,因此您不应该在.NET中使用它来计算哈希值。

Your key should have been generated using a specific encoding and same you should use in .NET. 您的密钥应该使用特定的编码生成,并且应该在.NET中使用。

Please note, there is some fundamental difference in java KeySpec and the Key being used for TripleDESCryptoServiceProvider . 请注意,java KeySpec和用于TripleDESCryptoServiceProvider的Key存在一些根本区别。 As mentioned by Microsfot https://msdn.microsoft.com/en-us/library/system.security.cryptography.tripledescryptoserviceprovider.aspx 如Microsfot所述https://msdn.microsoft.com/en-us/library/system.security.cryptography.tripledescryptoserviceprovider.aspx

Triple DES only supports "key lengths from 128 bits to 192 bits in increments of 64 bits" 三重DES仅支持“密钥长度从128位到192位,以64位为增量”

So you need to convert your key appropriately before assigning. 因此,您需要在分配之前适当地转换密钥。 To do this you can use the Array.Resize method as following. 为此,您可以使用Array.Resize方法,如下所示。

byte[] TDESKey = Encoding.UTF8.GetBytes(Passphrase);
System.Array.Resize(ref TDESKey , 192 / 8);

Hope this will help. 希望这会有所帮助。

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

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