Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Symmetric encryption issue between a .Net application and a Java application

I am integrating with a .Net application which uses Symmetric encryption. My application is on Java. Following is the .Net code which was shared by the team which uses the Symmetric encryption;

public static string SymmetricEncrypt<T>(string value, string PrivateKey, string SALT_STRING) where T : SymmetricAlgorithm, new()
    {

 PasswordDeriveBytes rgb = 
 new PasswordDeriveBytes(PrivateKey ,      
Encoding.Unicode.GetBytes(SALT_STRING));

        SymmetricAlgorithm algorithm = new T();

        byte[] rgbKey = rgb.GetBytes(algorithm.KeySize >> 3);
        byte[] rgbIV = rgb.GetBytes(algorithm.BlockSize >> 3);

        ICryptoTransform transform = algorithm.CreateEncryptor(rgbKey, rgbIV);

        using (MemoryStream buffer = new MemoryStream())
        {
  using (CryptoStream stream = 
                new CryptoStream(buffer, transform, CryptoStreamMode.Write))
            {
using (StreamWriter writer = new StreamWriter(stream, Encoding.Unicode))
                {
                    writer.Write(value);
                }
            }

            return Convert.ToBase64String(buffer.ToArray());
        }
    }

Looking at the .Net documentation, I could see that SymmetricEncrypt class by default uses Rijndael encryption. And also I have found a class similar to "PasswordDeriveBytes" implementation in bouncy castle which implements the "PKCS5S1" algorithm for key generation using the salt with an iteration count of 100. But still I am unable to generate the exact encryption to what is required by the .Net application. Following code is what i have tried so far;

 import java.security.InvalidAlgorithmParameterException;
import java.security.InvalidKeyException;
import java.security.NoSuchAlgorithmException;

import javax.crypto.BadPaddingException;
import javax.crypto.Cipher;
import javax.crypto.IllegalBlockSizeException;
import javax.crypto.NoSuchPaddingException;
import javax.crypto.spec.IvParameterSpec;
import javax.crypto.spec.SecretKeySpec;

import mtnsa.sorb.handler.PKCS5Test.PasswordDeriveBytes;

import org.bouncycastle.crypto.digests.SHA1Digest;
import org.bouncycastle.crypto.generators.PKCS5S1ParametersGenerator;
import org.bouncycastle.crypto.params.KeyParameter;
import org.bouncycastle.util.encoders.Base64;

public class EncryptionHelper {



    public static void main(String[] args)throws Exception {
        encrypt("FTTH","HhN01vcEEtMmwdNFliM8QYg0Y89xzBOJJG7BHARC7g", "002400000480000094000000060200000024000052534131000400000100010085525e9438e9fae122f71ec7124" +
                "443bf2f9f57f5f3760b3704df168493004b9ef68413f500d54fa9fa3869b42b1e2365204826e54b618d56e7e575f2" +
                "7f675f0eae3ea8458a8ee1e92dc3f4bfc34fbe23851afa9d2c28fc8cd5b124f60a03a06bfb598bc3acbd8c4380ae" +
                "f02cc58bdf955d140390f740a7e115c59e3b3b5758ca");
    }

    public static String encrypt(final String valueToEncrpt, final String saltString, final String privateKey) throws NoSuchAlgorithmException, NoSuchPaddingException, InvalidKeyException, InvalidAlgorithmParameterException, IllegalBlockSizeException, BadPaddingException{
        String encrptedValue = null;

           Cipher cipher = Cipher.getInstance("AES/CBC/PKCS5Padding");

        byte[] password = privateKey.getBytes();
        byte[] salt = saltString.getBytes();

        PKCS5S1ParametersGenerator generator = new PasswordDeriveBytes(new SHA1Digest());
        generator.init(password, salt, 100);

        byte[] keyArr = ((KeyParameter)generator.generateDerivedParameters(128)).getKey();
        byte[] IvArr = ((KeyParameter)generator.generateDerivedParameters(128)).getKey();



        cipher.init(Cipher.ENCRYPT_MODE, new SecretKeySpec(keyArr,"AES"),new IvParameterSpec(IvArr));
        byte[]test  = cipher.doFinal(valueToEncrpt.getBytes());

        System.out.println(new String(Base64.encode(test)));

        return encrptedValue;
    }
}

In the Java code i have given the sample SALT key and the PRIVATE key used for testing purposes. But still I am unable to get the exact value as generated by the .Net application. Following is an example plain text and the encrypted value which i got from the .Net application which i have failed to replicate in my Java code.

Plain text value : FTTH

Encrypted value : MjgmbdT3Vg6RW/7K1BjQ/Q==

Any help on this is much appreciated as i am currently out of ideas.

like image 312
dinukadev Avatar asked Dec 14 '25 12:12

dinukadev


1 Answers

Thank you everyone for the valuable comments. I have requested the .Net module authors to change their implementation to use RFC2898DeriveBytes

like image 149
dinukadev Avatar answered Dec 16 '25 04:12

dinukadev



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!