{"id":1413,"date":"2022-08-30T15:16:19","date_gmt":"2022-08-30T15:16:19","guid":{"rendered":"https:\/\/unknownerror.org\/index.php\/2013\/11\/16\/diffie-hellman-shared-secret-between-a-java-server-on-ubuntu-and-a-c-client-on-windows-closed-collection-of-common-programming-errors\/"},"modified":"2022-08-30T15:16:19","modified_gmt":"2022-08-30T15:16:19","slug":"diffie-hellman-shared-secret-between-a-java-server-on-ubuntu-and-a-c-client-on-windows-closed-collection-of-common-programming-errors","status":"publish","type":"post","link":"https:\/\/unknownerror.org\/index.php\/2022\/08\/30\/diffie-hellman-shared-secret-between-a-java-server-on-ubuntu-and-a-c-client-on-windows-closed-collection-of-common-programming-errors\/","title":{"rendered":"Diffie Hellman Shared Secret between a Java server on Ubuntu and a C++ client on Windows [closed]-Collection of common programming errors"},"content":{"rendered":"<p><strong>BTW people should not minus this question. It is the ONLY such question I can find online. It IS complicated but it has to be.<\/strong><\/p>\n<p>I have a windows C# application which I am trying to protect from decompilation. Therefore I have created a c++ dll containing all my http posting code, some core program logic and I am trying to set up a Diffie Hellman key exchange between my Java server (which is on a Ubuntu VPS) and this c++ dll.<\/p>\n<p>To further complicate things (and so the windows c# app can call the dll) I have wrapped the c++ dll in a c++ cli wrapper.<\/p>\n<p>To test my theory I made a c# console app and java console app that both read in bytes from the command line and simulate a key exchange as I would be doing via http.<\/p>\n<p>My code thus far below.<\/p>\n<p>N.B. I am thus far just trying to get it to work when both apps are on Windows.<\/p>\n<p>I cannot get my Java application to accept the public key generated by the C++ dll.<\/p>\n<p><strong>I have tried to piece together a whole lot of stuff I have found and altered online but I am really on struggle street at the moment and would appreciate any help to fix my code or suggestions for other ways to handle authentication encryption using my technology stack. Thanks.<\/strong><\/p>\n<h2>This is the Java output and error I get:<\/h2>\n<p>P: { 0x00, 0xFC, 0x77, 0xA6, 0x97, 0x20, 0xB0, 0x31, 0x9E, 0xB0, 0x31, 0xF8, 0x26, 0x58, 0xA3, 0x36, 0x2E, 0x22, 0xBA, 0x0A, 0xA5, 0x73, 0x41, 0x2E, 0x0D, 0xE8, 0xEA, 0x46, 0xA3, 0x59, 0xA7, 0x8C, 0xBC, 0x2D, 0x69, 0x51, 0xFC, 0x8D, 0x3C, 0xE2, 0xA1, 0x0E, 0xB7, 0xB7, 0x14, 0x4E, 0xA8, 0xFC, 0x6B, 0xF8, 0xE8, 0x3F, 0x43, 0x16, 0xB8, 0x63, 0x36, 0x69, 0x36, 0x38, 0xB7, 0x1E, 0x5A, 0xB4, 0x6B }<\/p>\n<p>P: 65<\/p>\n<p>G: { 0x00, 0xCA, 0x4D, 0x99, 0xAB, 0x1F, 0xEB, 0x40, 0xAD, 0x08, 0x36, 0x97, 0x67, 0xF6, 0x8B, 0x2A, 0x10, 0xEA, 0x0D, 0x5F, 0x5E, 0x98, 0xFA, 0x21, 0x95, 0x89, 0x1E, 0xE9, 0xC6, 0x24, 0x2A, 0xCA, 0xA3, 0xF6, 0x06, 0x58, 0x19, 0x23, 0xD3, 0x0C, 0x37, 0x16, 0x39, 0x87, 0xE5, 0xC8, 0x69, 0x8C, 0x59, 0x61, 0xC0, 0xBF, 0x70, 0x05, 0x04, 0x91, 0x6E, 0x85, 0x6E, 0xAB, 0x3F, 0xE6, 0x2B, 0x80, 0xA8 } G: 65<\/p>\n<blockquote>\n<p><strong>my input copied from c# console app output<\/strong><\/p>\n<p>0x00, 0x40, 0x83, 0xC7, 0x24, 0x63, 0xBC, 0xA5, 0x21, 0xA7, 0x10, 0x0A, 0xCB, 0xBE, 0x3D, &gt;0xA5, 0x5C, 0xFD, 0x58, 0x8F, 0x45, 0x9B, 0x4F, 0xF9, 0x71, 0xA8, 0xF1, 0xB3, 0xB3, 0xDA, &gt;0x6C, 0xE5, 0x02, 0x8C, 0x98, 0xA7, 0xB4, 0x87, 0xA9, 0x8B, 0xD8, 0x36, 0xF9, 0xEB, 0x04, &gt;0xFA, 0xEA, 0xEC, 0x0C, 0xBD, 0x36, 0xFC, 0x5D, 0x37, 0x3D, 0x17, 0xD4, 0x9F, 0xBD, 0x91, &gt;0x18, 0x8A, 0x18, 0x5C, 0x44, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, &gt;0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, &gt;0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, &gt;0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, &gt;0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, &gt;0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, &gt;0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, &gt;0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, &gt;0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, &gt;0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, &gt;0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, &gt;0x00, 0x00, 0x00<\/p>\n<\/blockquote>\n<p>otherPublicKey: { 0x00, 0x40, 0x83, 0xC7, 0x24, 0x63, 0xBC, 0xA5, 0x21, 0xA7, 0x10, 0x0A, 0xCB, 0xBE, 0x3D, 0xA5, 0x5C, 0xFD, 0x58, 0x8F, 0x45, 0x9B, 0x4F, 0xF9, 0x71, 0xA8, 0xF1, 0xB3, 0xB3, 0xDA, 0x6C, 0xE5, 0x02, 0x8C, 0x98, 0xA7, 0xB4, 0x87, 0xA9, 0x8B, 0xD8, 0x36, 0xF9, 0xEB, 0x04, 0xFA, 0xEA, 0xEC, 0x0C, 0xBD, 0x36, 0xFC, 0x5D, 0x37, 0x3D, 0x17, 0xD4, 0x9F, 0xBD, 0x91, 0x18, 0x8A, 0x18, 0x5C, 0x44, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 }<\/p>\n<p>Exception in thread &#8220;main&#8221; java.security.spec.InvalidKeySpecException: Inappropriate key specification at com.sun.crypto.provider.DHKeyFactory.engineGeneratePublic(DashoA13*..) at java.security.KeyFactory.generatePublic(Unknown Source) at com.socialpointsbot.security.DiffieHellman.endHandshake(DiffieHellman.java:111) at com.socialpointsbot.security.DiffieHellman.main(DiffieHellman.java:147)<\/p>\n<h2>Java Console Application code (acting as server)<\/h2>\n<pre><code>import java.io.BufferedReader;\nimport java.io.IOException;\nimport java.io.InputStreamReader;\nimport java.math.BigInteger;\nimport java.security.AlgorithmParameterGenerator;\nimport java.security.AlgorithmParameters;\nimport java.security.InvalidAlgorithmParameterException;\nimport java.security.InvalidKeyException;\nimport java.security.KeyFactory;\nimport java.security.KeyPair;\nimport java.security.KeyPairGenerator;\nimport java.security.NoSuchAlgorithmException;\nimport java.security.PrivateKey;\nimport java.security.PublicKey;\nimport java.security.spec.InvalidKeySpecException;\nimport java.security.spec.InvalidParameterSpecException;\nimport java.security.spec.X509EncodedKeySpec;\n\nimport javax.crypto.KeyAgreement;\nimport javax.crypto.SecretKey;\nimport javax.crypto.spec.DHParameterSpec;\nimport javax.xml.bind.DatatypeConverter;\n\npublic class DiffieHellman {\n    private static byte[] _P = { 0x00, (byte) 0xFC, 0x77, (byte) 0xA6, (byte) 0x97, 0x20, (byte) 0xB0, 0x31, (byte) 0x9E, (byte) 0xB0, 0x31,\n            (byte) 0xF8, 0x26, 0x58, (byte) 0xA3, 0x36, 0x2E, 0x22, (byte) 0xBA, 0x0A, (byte) 0xA5, 0x73, 0x41, 0x2E, 0x0D, (byte) 0xE8, (byte) 0xEA,\n            0x46, (byte) 0xA3, 0x59, (byte) 0xA7, (byte) 0x8C, (byte) 0xBC, 0x2D, 0x69, 0x51, (byte) 0xFC, (byte) 0x8D, 0x3C, (byte) 0xE2,\n            (byte) 0xA1, 0x0E, (byte) 0xB7, (byte) 0xB7, 0x14, 0x4E, (byte) 0xA8, (byte) 0xFC, 0x6B, (byte) 0xF8, (byte) 0xE8, 0x3F, 0x43, 0x16,\n            (byte) 0xB8, 0x63, 0x36, 0x69, 0x36, 0x38, (byte) 0xB7, 0x1E, 0x5A, (byte) 0xB4, 0x6B };\n    private static byte[] _G = { 0x00, (byte) 0xCA, 0x4D, (byte) 0x99, (byte) 0xAB, 0x1F, (byte) 0xEB, 0x40, (byte) 0xAD, 0x08, 0x36, (byte) 0x97,\n            0x67, (byte) 0xF6, (byte) 0x8B, 0x2A, 0x10, (byte) 0xEA, 0x0D, 0x5F, 0x5E, (byte) 0x98, (byte) 0xFA, 0x21, (byte) 0x95, (byte) 0x89,\n            0x1E, (byte) 0xE9, (byte) 0xC6, 0x24, 0x2A, (byte) 0xCA, (byte) 0xA3, (byte) 0xF6, 0x06, 0x58, 0x19, 0x23, (byte) 0xD3, 0x0C, 0x37, 0x16,\n            0x39, (byte) 0x87, (byte) 0xE5, (byte) 0xC8, 0x69, (byte) 0x8C, 0x59, 0x61, (byte) 0xC0, (byte) 0xBF, 0x70, 0x05, 0x04, (byte) 0x91,\n            0x6E, (byte) 0x85, 0x6E, (byte) 0xAB, 0x3F, (byte) 0xE6, 0x2B, (byte) 0x80, (byte) 0xA8 };\n\n    private static BigInteger P = new BigInteger(_P);\n    private static BigInteger G = new BigInteger(_G);\n\n    private PrivateKey privateKey;\n\n    private SecretKey secretKey;\n\n    public byte[] startHandshake() throws InvalidAlgorithmParameterException, NoSuchAlgorithmException {\n        KeyPairGenerator keyGen = KeyPairGenerator.getInstance(\"DH\");\n        DHParameterSpec dhSpec = new DHParameterSpec(P, G);\n        keyGen.initialize(dhSpec);\n        KeyPair keypair = keyGen.generateKeyPair();\n\n        \/\/ Get the generated public and private keys\n        privateKey = keypair.getPrivate();\n        PublicKey publicKey = keypair.getPublic();\n\n        return publicKey.getEncoded();\n    }\n\n    public void endHandshake(final byte[] alicePublicKey) throws InvalidKeySpecException, NoSuchAlgorithmException, InvalidKeyException,\n            IllegalStateException {\n        X509EncodedKeySpec x509KeySpec = new X509EncodedKeySpec(alicePublicKey);\n        KeyFactory keyFact = KeyFactory.getInstance(\"DH\");\n        PublicKey publicKey = keyFact.generatePublic(x509KeySpec);\n\n        KeyAgreement ka = KeyAgreement.getInstance(\"DH\");\n        ka.init(privateKey);\n        ka.doPhase(publicKey, true);\n\n        String algorithm = \"DES\";\n\n        secretKey = ka.generateSecret(algorithm);\n    }\n\n    public byte[] getSecret() {\n        return secretKey.getEncoded();\n    }\n\n    public static void main(String[] args) throws InvalidAlgorithmParameterException, NoSuchAlgorithmException, InvalidKeyException,\n            InvalidKeySpecException, IllegalStateException, IOException {\n        final DiffieHellman diffieHellman = new DiffieHellman();\n        System.out.println(\"P: \" + bytesToHexString(diffieHellman.getP().toByteArray()));\n        System.out.println(\"P: \" + diffieHellman.getP().toByteArray().length);\n        System.out.println(\"G: \" + bytesToHexString(diffieHellman.getG().toByteArray()));\n        System.out.println(\"G: \" + diffieHellman.getG().toByteArray().length);\n\n        System.out.println();\n\n        final byte[] myPublicKey = diffieHellman.startHandshake();\n        System.out.println(\"myPublicKey: \" + bytesToHexString(myPublicKey));\n        System.out.println(\"myPublicKey: \" + myPublicKey.length);\n\n        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));\n        final String inputByteString = br.readLine();\n        final byte[] otherPublicKey = hexStringToByteArray(inputByteString);\n        System.out.println(\"otherPublicKey: \" + bytesToHexString(otherPublicKey));\n\n        diffieHellman.endHandshake(otherPublicKey);\n        System.out.println(\"secret: \" + bytesToHexString(diffieHellman.getSecret()));\n    }\n\n    public static DHParameterSpec genDhParams() {\n        try {\n            \/\/ Create the parameter generator for a 1024-bit DH key pair\n            final AlgorithmParameterGenerator paramGen = AlgorithmParameterGenerator.getInstance(\"DH\");\n            paramGen.init(512);\n\n            \/\/ Generate the parameters\n            final AlgorithmParameters params = paramGen.generateParameters();\n            final DHParameterSpec dhSpec = (DHParameterSpec) params.getParameterSpec(DHParameterSpec.class);\n            return dhSpec;\n        } catch (NoSuchAlgorithmException e) {\n        } catch (InvalidParameterSpecException e) {\n        }\n        return null;\n    }\n\n    public static String bytesToHexString(byte[] bytes) {\n        final String[] hexArray = { \"00\", \"01\", \"02\", \"03\", \"04\", \"05\", \"06\", \"07\", \"08\", \"09\", \"0A\", \"0B\", \"0C\", \"0D\", \"0E\", \"0F\", \"10\", \"11\", \"12\",\n                \"13\", \"14\", \"15\", \"16\", \"17\", \"18\", \"19\", \"1A\", \"1B\", \"1C\", \"1D\", \"1E\", \"1F\", \"20\", \"21\", \"22\", \"23\", \"24\", \"25\", \"26\", \"27\", \"28\",\n                \"29\", \"2A\", \"2B\", \"2C\", \"2D\", \"2E\", \"2F\", \"30\", \"31\", \"32\", \"33\", \"34\", \"35\", \"36\", \"37\", \"38\", \"39\", \"3A\", \"3B\", \"3C\", \"3D\", \"3E\",\n                \"3F\", \"40\", \"41\", \"42\", \"43\", \"44\", \"45\", \"46\", \"47\", \"48\", \"49\", \"4A\", \"4B\", \"4C\", \"4D\", \"4E\", \"4F\", \"50\", \"51\", \"52\", \"53\", \"54\",\n                \"55\", \"56\", \"57\", \"58\", \"59\", \"5A\", \"5B\", \"5C\", \"5D\", \"5E\", \"5F\", \"60\", \"61\", \"62\", \"63\", \"64\", \"65\", \"66\", \"67\", \"68\", \"69\", \"6A\",\n                \"6B\", \"6C\", \"6D\", \"6E\", \"6F\", \"70\", \"71\", \"72\", \"73\", \"74\", \"75\", \"76\", \"77\", \"78\", \"79\", \"7A\", \"7B\", \"7C\", \"7D\", \"7E\", \"7F\", \"80\",\n                \"81\", \"82\", \"83\", \"84\", \"85\", \"86\", \"87\", \"88\", \"89\", \"8A\", \"8B\", \"8C\", \"8D\", \"8E\", \"8F\", \"90\", \"91\", \"92\", \"93\", \"94\", \"95\", \"96\",\n                \"97\", \"98\", \"99\", \"9A\", \"9B\", \"9C\", \"9D\", \"9E\", \"9F\", \"A0\", \"A1\", \"A2\", \"A3\", \"A4\", \"A5\", \"A6\", \"A7\", \"A8\", \"A9\", \"AA\", \"AB\", \"AC\",\n                \"AD\", \"AE\", \"AF\", \"B0\", \"B1\", \"B2\", \"B3\", \"B4\", \"B5\", \"B6\", \"B7\", \"B8\", \"B9\", \"BA\", \"BB\", \"BC\", \"BD\", \"BE\", \"BF\", \"C0\", \"C1\", \"C2\",\n                \"C3\", \"C4\", \"C5\", \"C6\", \"C7\", \"C8\", \"C9\", \"CA\", \"CB\", \"CC\", \"CD\", \"CE\", \"CF\", \"D0\", \"D1\", \"D2\", \"D3\", \"D4\", \"D5\", \"D6\", \"D7\", \"D8\",\n                \"D9\", \"DA\", \"DB\", \"DC\", \"DD\", \"DE\", \"DF\", \"E0\", \"E1\", \"E2\", \"E3\", \"E4\", \"E5\", \"E6\", \"E7\", \"E8\", \"E9\", \"EA\", \"EB\", \"EC\", \"ED\", \"EE\",\n                \"EF\", \"F0\", \"F1\", \"F2\", \"F3\", \"F4\", \"F5\", \"F6\", \"F7\", \"F8\", \"F9\", \"FA\", \"FB\", \"FC\", \"FD\", \"FE\", \"FF\" };\n\n        final StringBuilder hexString = new StringBuilder();\n        hexString.append(\"{\");\n        for (int i = 0; i &lt; bytes.length; i++) {\n            hexString.append(\" 0x\").append(hexArray[0xFF &amp; bytes[i]]).append(\",\");\n\n        }\n        String result = hexString.toString();\n        result = result.substring(0, result.length() - 1);\n        return result += \" }\";\n    }\n\n    public static byte[] hexStringToByteArray(String s) {\n        s = s.replaceAll(\"0x\", \"\").replaceAll(\",\", \"\").replaceAll(\" \", \"\");\n        int len = s.length();\n        byte[] data = new byte[len \/ 2];\n        for (int i = 0; i &lt; len; i += 2) {\n            data[i \/ 2] = (byte) ((Character.digit(s.charAt(i), 16) &gt; 1];\n\n            for (int i = 0; i &lt; hex.Length &gt;&gt; 1; ++i)\n            {\n                arr[i] = (byte)((GetHexVal(hex[i 0x16, 0xB8, 0x63, 0x36, 0x69, 0x36, 0x38, 0xB7, 0x1E, 0x5A, 0xB4, 0x6B, 0x02, 0x41, 0x00, 0xCA, 0x4D, 0x99, 0xAB, 0x1F, 0xEB, 0x40, 0xAD, 0x08, 0x36, 0x97, 0x67, 0xF6, 0x8B, 0x2A, &gt;0x10, 0xEA, 0x0D, 0x5F, 0x5E, 0x98, 0xFA, 0x21, 0x95, 0x89, 0x1E, 0xE9, 0xC6, 0x24, 0x2A, 0xCA, 0xA3, 0xF6, 0x06, 0x58, 0x19, 0x23, 0xD3, 0x0C, 0x37, 0x16, 0x39, 0x87, 0xE5, 0xC8, &gt;0x69, 0x8C, 0x59, 0x61, 0xC0, 0xBF, 0x70, 0x05, 0x04, 0x91, 0x6E, 0x85, 0x6E, 0xAB, 0x3F, 0xE6, 0x2B, 0x80, 0xA8, 0x02, 0x02, 0x01, 0x80, 0x03, 0x44, 0x00, 0x02, 0x41, 0x00, 0xF6, &gt;0xCC, 0x6E, 0x78, 0x97, 0x04, 0xCE, 0x0A, 0x7B, 0x7D, 0x26, 0x0F, 0x0B, 0x5F, 0xEB, 0xFB, 0xAB, 0x45, 0x76, 0x75, 0xEE, 0xA7, 0xFD, 0x55, 0x39, 0x7F, 0xA3, 0x42, 0x57, 0x0F, 0xE6, &gt;0xE7, 0x31, 0x55, 0x7A, 0xD5, 0x72, 0x9D, 0xC5, 0x45, 0xA8, 0xA4, 0x67, 0x70, 0x58, 0x67, 0x37, 0x1E, 0x6D, 0x55, 0x7B, 0x6E, 0xFB, 0xB3, 0x43, 0x57, 0x9E, 0xB8, 0x4F, 0x98, 0x61, &gt;0x5B, 0x85, 0x7E<br \/><br \/>\n\n<br \/>AlicePub: 0x30, 0x81, 0xE1, 0x30, 0x81, 0x98, 0x06, 0x09, 0x2A, 0x86, 0x48, 0x86, 0xF7, 0x0D, 0x01, 0x03, 0x01, 0x30, 0x81, 0x8A, 0x02, 0x41, 0x00, 0xFC, 0x77, 0xA6, 0x97, 0x20, 0x B0, 0x31, 0x9E, 0xB0, 0x31, 0xF8, 0x26, 0x58, 0xA3, 0x36, 0x2E, 0x22, 0xBA, 0x0A, 0xA5, 0x73, 0x41, 0x2E, 0x0D, 0xE8, 0xEA, 0x46, 0xA3, 0x59, 0xA7, 0x8C, 0xBC, 0x2D, 0x69, 0x51, 0x FC, 0x8D, 0x3C, 0xE2, 0xA1, 0x0E, 0xB7, 0xB7, 0x14, 0x4E, 0xA8, 0xFC, 0x6B, 0xF8, 0xE8, 0x3F, 0x43, 0x16, 0xB8, 0x63, 0x36, 0x69, 0x36, 0x38, 0xB7, 0x1E, 0x5A, 0xB4, 0x6B, 0x02, 0x 41, 0x00, 0xCA, 0x4D, 0x99, 0xAB, 0x1F, 0xEB, 0x40, 0xAD, 0x08, 0x36, 0x97, 0x67, 0xF6, 0x8B, 0x2A, 0x10, 0xEA, 0x0D, 0x5F, 0x5E, 0x98, 0xFA, 0x21, 0x95, 0x89, 0x1E, 0xE9, 0xC6, 0x 24, 0x2A, 0xCA, 0xA3, 0xF6, 0x06, 0x58, 0x19, 0x23, 0xD3, 0x0C, 0x37, 0x16, 0x39, 0x87, 0xE5, 0xC8, 0x69, 0x8C, 0x59, 0x61, 0xC0, 0xBF, 0x70, 0x05, 0x04, 0x91, 0x6E, 0x85, 0x6E, 0x AB, 0x3F, 0xE6, 0x2B, 0x80, 0xA8, 0x02, 0x02, 0x01, 0x80, 0x03, 0x44, 0x00, 0x02, 0x41, 0x00, 0xF6, 0xCC, 0x6E, 0x78, 0x97, 0x04, 0xCE, 0x0A, 0x7B, 0x7D, 0x26, 0x0F, 0x0B, 0x5F, 0x EB, 0xFB, 0xAB, 0x45, 0x76, 0x75, 0xEE, 0xA7, 0xFD, 0x55, 0x39, 0x7F, 0xA3, 0x42, 0x57, 0x0F, 0xE6, 0xE7, 0x31, 0x55, 0x7A, 0xD5, 0x72, 0x9D, 0xC5, 0x45, 0xA8, 0xA4, 0x67, 0x70, 0x 58, 0x67, 0x37, 0x1E, 0x6D, 0x55, 0x7B, 0x6E, 0xFB, 0xB3, 0x43, 0x57, 0x9E, 0xB8, 0x4F, 0x98, 0x61, 0x5B, 0x85, 0x7E<br \/><br \/>\n<br \/>BobPub: 0x00, 0x40, 0x83, 0xC7, 0x24, 0x63, 0xBC, 0xA5, 0x21, 0xA7, 0x10, 0x0A, 0xCB, 0xBE, 0x3D, 0xA5, 0x5C, 0xFD, 0x58, 0x8F, 0x45, 0x9B, 0x4F, 0xF9, 0x71, 0xA8, 0xF1, 0xB3, 0xB3 , 0xDA, 0x6C, 0xE5, 0x02, 0x8C, 0x98, 0xA7, 0xB4, 0x87, 0xA9, 0x8B, 0xD8, 0x36, 0xF9, 0xEB, 0x04, 0xFA, 0xEA, 0xEC, 0x0C, 0xBD, 0x36, 0xFC, 0x5D, 0x37, 0x3D, 0x17, 0xD4, 0x9F, 0xBD , 0x91, 0x18, 0x8A, 0x18, 0x5C, 0x44, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 , 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 , 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 , 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 , 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 , 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00<br \/><br \/>\n<br \/>Secret: 0x00, 0xC0, 0x4A, 0xEE, 0xEA, 0x2C, 0x50, 0x9A, 0x10, 0xD0, 0x6C, 0x76, 0x23, 0xBF, 0x00, 0xCB, 0x2A, 0x40, 0x05, 0x27, 0x31, 0x91, 0x34, 0xFD, 0x5C, 0xF0, 0x92, 0x83, 0x44 , 0xFF, 0xC1, 0xF8, 0x13, 0xC6, 0xE9, 0xB6, 0x66, 0x06, 0xE5, 0x06, 0x28, 0xA0, 0x21, 0xEA, 0xC1, 0x8E, 0x81, 0x55, 0x1B, 0xB6, 0x16, 0x35, 0x7B, 0x1E, 0x6C, 0xEA, 0x14, 0x00, 0x6D , 0xA8, 0x81, 0xEF, 0x2C, 0xE5, 0x36, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 , 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 , 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 , 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 , 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 , 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00<br \/><br \/>\n<\/code><\/pre>\n<h2><code>C++ Code without c++ cli wrapper (as all it does is wrap and it works)<\/code><\/h2>\n<pre>\n<br \/><strong>N.B. I set const size_t PModuleLength = 228; in a failed attempt to get the public key size output by the c++ to be the same length as the java public key. It was originally 129. as you can see all this does is add a whole lot of 0x00 to the end of the output.<\/strong>\n<\/pre>\n<h3><code>UIUtils.h<\/code><\/h3>\n<pre>\n<\/pre>\n<pre><code><code>#define UIUTILS_API __declspec(dllexport) \n\n#include \n#include \"DiffieHellmanKeysExchanger.h\"\n#include \n#include \n#include \n#include \n\nusing namespace std;\n\nnamespace UIUtils {\n    class UIUtilsClass {\n        public: \n            UIUTILS_API static std::vector doAlice(void);\n            UIUTILS_API static std::vector doBob(std::vector alicePublicKey);\n            UIUTILS_API static std::vector doAliceAgain(std::vector bobPublicKey);\n            UIUTILS_API static std::vector getBob(void);\n    };\n}\n<\/code><\/code><\/pre>\n<pre>\n<\/pre>\n<h3>UIUtils.cpp<\/h3>\n<pre>\n<\/pre>\n<pre><code>#include \"stdafx.h\"\n#include \"UIUtils.h\"\n\nnamespace UIUtils {\n\n    const size_t PModuleLength = 228;\n\n    unsigned char buffer[PModuleLength] = { 0x00, 0xFC, 0x77, 0xA6, 0x97, 0x20, 0xB0, 0x31, 0x9E, 0xB0, 0x31, \n        0xF8, 0x26, 0x58, 0xA3, 0x36, 0x2E, 0x22, 0xBA, 0x0A, 0xA5, 0x73, 0x41, 0x2E, 0x0D, 0xE8, 0xEA, 0x46,\n        0xA3, 0x59, 0xA7, 0x8C, 0xBC, 0x2D, 0x69, 0x51, 0xFC, 0x8D, 0x3C, 0xE2, 0xA1, 0x0E, 0xB7, 0xB7, 0x14, \n        0x4E, 0xA8, 0xFC, 0x6B, 0xF8, 0xE8, 0x3F, 0x43, 0x16, 0xB8, 0x63, 0x36, 0x69, 0x36, 0x38, 0xB7, 0x1E,\n        0x5A, 0xB4, 0x6B };\n\n    std::vector cryptoPModule((char*)buffer, (char*)buffer + PModuleLength);\n\n    const size_t GModuleLength = 65;\n\n    unsigned char gBuffer[GModuleLength] = { 0x00, 0xCA, 0x4D, 0x99, 0xAB, 0x1F, 0xEB, 0x40, 0xAD, 0x08, 0x36,\n        0x97, 0x67, 0xF6, 0x8B, 0x2A, 0x10, 0xEA, 0x0D, 0x5F, 0x5E, 0x98, 0xFA, 0x21, 0x95, 0x89, 0x1E, 0xE9, \n        0xC6, 0x24, 0x2A, 0xCA, 0xA3, 0xF6, 0x06, 0x58, 0x19, 0x23, 0xD3, 0x0C, 0x37, 0x16, 0x39, 0x87, 0xE5, \n        0xC8, 0x69, 0x8C, 0x59, 0x61, 0xC0, 0xBF, 0x70, 0x05, 0x04, 0x91, 0x6E, 0x85, 0x6E, 0xAB, 0x3F, 0xE6,\n        0x2B, 0x80, 0xA8 };\n\n    unsigned long cryptoGModule = (unsigned long) gBuffer;\n\n    DiffieHellmanLib::DiffieHellmanKeysExchanger aliceExchanger(cryptoPModule, cryptoGModule);\n    DiffieHellmanLib::DiffieHellmanKeysExchanger bobExchanger(cryptoPModule, cryptoGModule);\n\n    std::vector bobSharedSecret;\n\n    std::vector UIUtilsClass::doAlice(void){\n        std::vector alicePublicKey;\n        aliceExchanger.GenerateExchangeData(alicePublicKey);\n        return alicePublicKey;\n    }\n\n    std::vector UIUtilsClass::doBob(std::vector alicePublicKey){\n        std::vector bobPublicKey;\n        bobExchanger.GenerateExchangeData(bobPublicKey);\n        bobExchanger.CompleteExchangeData(alicePublicKey, bobSharedSecret);\n        return bobPublicKey;\n    }\n\n    std::vector UIUtilsClass::doAliceAgain(std::vector bobPublicKey){\n        std::vector aliceSharedSecret;\n        aliceExchanger.CompleteExchangeData(bobPublicKey, aliceSharedSecret);\n        return aliceSharedSecret;\n    }\n\n    std::vector UIUtilsClass::getBob(void){\n        return bobSharedSecret;\n    }\n}\n<\/code><\/pre>\n<p id=\"rop\"><small>Originally posted 2013-11-16 16:09:05. <\/small><\/p>","protected":false},"excerpt":{"rendered":"<p>BTW people should not minus this question. It is the ONLY such question I can find online. It IS complicated but it has to be. I have a windows C# application which I am trying to protect from decompilation. Therefore I have created a c++ dll containing all my http posting code, some core program [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[],"class_list":["post-1413","post","type-post","status-publish","format-standard","hentry","category-uncategorized"],"_links":{"self":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/posts\/1413","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/comments?post=1413"}],"version-history":[{"count":0,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/posts\/1413\/revisions"}],"wp:attachment":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/media?parent=1413"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/categories?post=1413"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/tags?post=1413"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}