'Run Java Triple DES Code in .NET 6 MVC Application

I have a triple DES Encryption & Decryption Code written in JAVA language and I want to run it using my.NET Core MVC Application, how can I do that. How can I integrate JAVA Code in my .NET Core MVC Application? Can someone please help me with this? I researched somewhat and found a library ikvm but it was also a dead end. Thank you

JAVA CODE

    import javax.crypto.Cipher;
    import javax.crypto.SecretKey;
    import javax.crypto.SecretKeyFactory;
    import javax.crypto.spec.DESedeKeySpec;
    import javax.crypto.spec.IvParameterSpec;
//  import org.apache.commons.codec.binary.Hex;
    import java.math.BigInteger;
//    import sun.misc.BASE64Encoder;
    
    public class TripleDES {
        private DESedeKeySpec desKeySpec;
        
        public TripleDES(String key) {
            try {
                byte[] keyBytes = { (byte) 0x02, (byte) 0x02, (byte) 0x02, (byte) 0x02,
                        (byte) 0x02, (byte) 0x02, (byte) 0x02, (byte) 0x02,
                        (byte) 0x02, (byte) 0x02, (byte) 0x02, (byte) 0x02,
                        (byte) 0x02, (byte) 0x02, (byte) 0x02, (byte) 0x02,
                        (byte) 0x02, (byte) 0x02, (byte) 0x02, (byte) 0x02,
                        (byte) 0x02, (byte) 0x02, (byte) 0x02, (byte) 0x02};
                

                
                this.desKeySpec = new DESedeKeySpec(keyBytes);

            } catch (Exception e) {
                e.printStackTrace();
            }
        }
        
        public byte[] encrypt(byte[] origData) {
            try {
                SecretKeyFactory factory = SecretKeyFactory.getInstance("DESede");
                SecretKey key = factory.generateSecret(this.desKeySpec);
                Cipher cipher = Cipher.getInstance("DESede/ECB/NoPadding");
                cipher.init(Cipher.ENCRYPT_MODE, key);
                return cipher.doFinal(origData);
            }  catch (Exception e) {
                e.printStackTrace();
            }
            return null;
        }
        
        public byte[] decrypt(byte[] crypted) {
            try {
                SecretKeyFactory factory = SecretKeyFactory.getInstance("DESede");
                SecretKey key = factory.generateSecret(this.desKeySpec);
                Cipher cipher = Cipher.getInstance("DESede/ECB/NoPadding");  //DESede/CBC/PKCS5Padding
                cipher.init(Cipher.DECRYPT_MODE, key);
                return cipher.doFinal(crypted);
            }  catch (Exception e) {
                e.printStackTrace();
            }
            return null;
        }
        

        public static void main(String[] args) throws Exception {
            TripleDES des = new TripleDES("");
            
          byte[] data = { (byte)0x04, (byte)0x12, (byte)0x05, (byte)0xFF, (byte)0xFB, (byte)0xA6, (byte)0x66, (byte)0xCF};
          //byte[] data = { (byte)0x04, (byte)0x12, (byte)0x15, (byte)0xAF, (byte)0xFD, (byte)0xD8, (byte)0x88, (byte)0xBB};  


    //-----------------Edited-----------------
    String text = new BigInteger(1, data).toString(16);
    System.out.println("Before encryption = " +text);             
        byte[] crypted = des.encrypt(data);
    String text1 = new BigInteger(1, crypted).toString(16);
        System.out.println("Encrypted = " +text1);
    byte[] decrypted = des.decrypt(crypted);
    String text2 = new BigInteger(1, decrypted).toString(16);
        System.out.println("Decrypted = " +text2);
        }

    }


Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source