سؤال

I've searched a lot before asking, but none of the ideas I found work in my problem so here's my problem :

  1. In C# the code (which I cannot change because it is from another application) for encryption is detailed after.
  2. I have to decrypt the encrypted token in Java but nothing works so far, can anyone help ?

For 1. C# code :

    static public string Encrypt3DES(string toEncrypt, string SecKey, string IV){
    byte[] keyArray;
    try
    {
        byte[] toEncryptArray = UTF8Encoding.UTF8.GetBytes(toEncrypt);

    MD5CryptoServiceProvider hashmd5 = new MD5CryptoServiceProvider();
        keyArray = hashmd5.ComputeHash(UTF8Encoding.UTF8.GetBytes(SecKey));
        hashmd5.Clear();      

        TripleDESCryptoServiceProvider tdes = new TripleDESCryptoServiceProvider();

        tdes.Key = keyArray;      
        tdes.Mode = CipherMode.CBC;
    tdes.Padding = PaddingMode.PKCS7;
        tdes.IV = UTF8Encoding.UTF8.GetBytes(IV);

        ICryptoTransform cTransform = tdes.CreateEncryptor();

        //transform the specified region of bytes array to resultArray
        byte[] resultArray = cTransform.TransformFinalBlock(toEncryptArray, 0, toEncryptArray.Length);
    tdes.Clear();

    //Return the encrypted data into unreadable string format
        return Convert.ToBase64String(resultArray, 0, resultArray.Length);
    }
    catch (Exception e) { return string.Empty; }
    }

For 2 Java code that does not work :

    public class TripleDesTest {

private KeySpec keySpec;
private SecretKey key;
private IvParameterSpec iv;

public TripleDesTest() {
    String keyString = "THE_KEY";
    String ivString = "THE_IV";

    try {
        final MessageDigest md = MessageDigest.getInstance("md5");
        final byte[] digestOfPassword = md.digest(Base64.decodeBase64(keyString.getBytes("UTF-8")));            
        final byte[] keyBytes = Arrays.copyOf(digestOfPassword, 24);
        for (int j = 0, k = 16; j < 8;) {
            keyBytes[k++] = keyBytes[j++];
        }

        keySpec = new DESedeKeySpec(keyBytes);

        key = SecretKeyFactory.getInstance("DESede").generateSecret(keySpec);

        iv = new IvParameterSpec(ivString.getBytes("UTF-8"));
    } catch (Exception e) {
        e.printStackTrace();
    }

}


public String decrypt(String value) {

    try {
        Cipher dcipher = Cipher.getInstance("DESede/CBC/PKCS5Padding", "SunJCE");
        dcipher.init(Cipher.DECRYPT_MODE, key, iv);

        if (value == null)
            return null;

        // Decode base64 to get bytes
        byte[] dec = Base64.decodeBase64(value.getBytes("UTF-8"));

        // Decrypt
        byte[] utf8 = dcipher.doFinal(dec);

        // Decode using UTF-8
        return new String(utf8, "UTF-8");
    } catch (Exception e) {
        e.printStackTrace();
    }
    return null;

}
    }
هل كانت مفيدة؟

المحلول

Here is the solution to the problem (I finally was able to solve this myself) :

In Java, replace

final byte[] digestOfPassword = md.digest(Base64.decodeBase64(keyString.getBytes("UTF-8")));`

with :

final byte[] digestOfPassword = md.digest(keyString.getBytes("UTF-8"));

Because on the C# side, no Base64 is used for the key :

keyArray = hashmd5.ComputeHash(UTF8Encoding.UTF8.GetBytes(SecKey));
مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top