Java equivalent C# encryption

Asked

Viewed 138 times

3

I have the method below in C# and I need to do something equivalent in JAVA, but I’m not getting it. Could you help me?

private const string chave = "eMbARaLhaDo";

public static string Descriptografar(string Message)
{
    byte[] Results;
    System.Text.UTF8Encoding UTF8 = new System.Text.UTF8Encoding();
    MD5CryptoServiceProvider HashProvider = new MD5CryptoServiceProvider();
    byte[] TDESKey = HashProvider.ComputeHash(UTF8.GetBytes(chave));
    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);
}

I tried the code below, but it didn’t work:

public static String decodificar(String key, String text) throws Exception {
    MessageDigest md = MessageDigest.getInstance("MD5");
    byte[] encData = new BASE64Decoder().decodeBuffer(text);
    Cipher decipher = Cipher.getInstance("AES/ECB/NoPadding");
    byte[] tdesKeyData = md.digest(key.getBytes("UTF-8"));
    SecretKeySpec myKey = new SecretKeySpec(tdesKeyData, "AES");
    decipher.init(Cipher.DECRYPT_MODE, myKey);
    byte[] plainText = decipher.doFinal(encData);
    return new String(plainText);
}
  • That solution that I adopted in my project, when I had this problem.

  • Fernando, thank you for the answer! I don’t understand what the parameters are: byte[] salt, int iterations, int outputBytes

  • El Diablo, I updated the response there, with an explanation of the parameters and with an example of use for each language.

  • Wonder Fernando! I kind of took the tram walking here and passed me this method in C#, to do an equivalent in java, that is, I don’t have this information of salt, iterations and nor outputBytes. It is possible to discover in the fingernail?

  • El Diablo, sorry I didn’t answer before. In the nail I find it difficult (or maybe impossible). So first you have to study what the C# method uses for Cryptography (from what I saw there is MD5). And how everything is done, researching the classes and methods that are used, and looking for equivalences in Java. This would be the only way if you have to maintain compatibility with the C#implementation. Only one observation your C# method is called "Decrypt" but MD5 (not only is it not an encryption but a Hash) is irreversible, so there could not be a method to reverse an MD5 hash.

  • 1

    Fernando, thanks for the help. After all, I managed to solve, I will post the method here.

Show 2 more comments
No answers

Browser other questions tagged

You are not signed in. Login or sign up in order to post.