Diffie Hellman Shared Secret between a Java server on Ubuntu and a C++ client on Windows [closed]-Collection of common programming errors
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 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.
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.
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.
My code thus far below.
N.B. I am thus far just trying to get it to work when both apps are on Windows.
I cannot get my Java application to accept the public key generated by the C++ dll.
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.
This is the Java output and error I get:
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: 65
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
my input copied from c# console app output
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
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 }
Exception in thread “main” 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)
Java Console Application code (acting as server)
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.math.BigInteger;
import java.security.AlgorithmParameterGenerator;
import java.security.AlgorithmParameters;
import java.security.InvalidAlgorithmParameterException;
import java.security.InvalidKeyException;
import java.security.KeyFactory;
import java.security.KeyPair;
import java.security.KeyPairGenerator;
import java.security.NoSuchAlgorithmException;
import java.security.PrivateKey;
import java.security.PublicKey;
import java.security.spec.InvalidKeySpecException;
import java.security.spec.InvalidParameterSpecException;
import java.security.spec.X509EncodedKeySpec;
import javax.crypto.KeyAgreement;
import javax.crypto.SecretKey;
import javax.crypto.spec.DHParameterSpec;
import javax.xml.bind.DatatypeConverter;
public class DiffieHellman {
private static byte[] _P = { 0x00, (byte) 0xFC, 0x77, (byte) 0xA6, (byte) 0x97, 0x20, (byte) 0xB0, 0x31, (byte) 0x9E, (byte) 0xB0, 0x31,
(byte) 0xF8, 0x26, 0x58, (byte) 0xA3, 0x36, 0x2E, 0x22, (byte) 0xBA, 0x0A, (byte) 0xA5, 0x73, 0x41, 0x2E, 0x0D, (byte) 0xE8, (byte) 0xEA,
0x46, (byte) 0xA3, 0x59, (byte) 0xA7, (byte) 0x8C, (byte) 0xBC, 0x2D, 0x69, 0x51, (byte) 0xFC, (byte) 0x8D, 0x3C, (byte) 0xE2,
(byte) 0xA1, 0x0E, (byte) 0xB7, (byte) 0xB7, 0x14, 0x4E, (byte) 0xA8, (byte) 0xFC, 0x6B, (byte) 0xF8, (byte) 0xE8, 0x3F, 0x43, 0x16,
(byte) 0xB8, 0x63, 0x36, 0x69, 0x36, 0x38, (byte) 0xB7, 0x1E, 0x5A, (byte) 0xB4, 0x6B };
private static byte[] _G = { 0x00, (byte) 0xCA, 0x4D, (byte) 0x99, (byte) 0xAB, 0x1F, (byte) 0xEB, 0x40, (byte) 0xAD, 0x08, 0x36, (byte) 0x97,
0x67, (byte) 0xF6, (byte) 0x8B, 0x2A, 0x10, (byte) 0xEA, 0x0D, 0x5F, 0x5E, (byte) 0x98, (byte) 0xFA, 0x21, (byte) 0x95, (byte) 0x89,
0x1E, (byte) 0xE9, (byte) 0xC6, 0x24, 0x2A, (byte) 0xCA, (byte) 0xA3, (byte) 0xF6, 0x06, 0x58, 0x19, 0x23, (byte) 0xD3, 0x0C, 0x37, 0x16,
0x39, (byte) 0x87, (byte) 0xE5, (byte) 0xC8, 0x69, (byte) 0x8C, 0x59, 0x61, (byte) 0xC0, (byte) 0xBF, 0x70, 0x05, 0x04, (byte) 0x91,
0x6E, (byte) 0x85, 0x6E, (byte) 0xAB, 0x3F, (byte) 0xE6, 0x2B, (byte) 0x80, (byte) 0xA8 };
private static BigInteger P = new BigInteger(_P);
private static BigInteger G = new BigInteger(_G);
private PrivateKey privateKey;
private SecretKey secretKey;
public byte[] startHandshake() throws InvalidAlgorithmParameterException, NoSuchAlgorithmException {
KeyPairGenerator keyGen = KeyPairGenerator.getInstance("DH");
DHParameterSpec dhSpec = new DHParameterSpec(P, G);
keyGen.initialize(dhSpec);
KeyPair keypair = keyGen.generateKeyPair();
// Get the generated public and private keys
privateKey = keypair.getPrivate();
PublicKey publicKey = keypair.getPublic();
return publicKey.getEncoded();
}
public void endHandshake(final byte[] alicePublicKey) throws InvalidKeySpecException, NoSuchAlgorithmException, InvalidKeyException,
IllegalStateException {
X509EncodedKeySpec x509KeySpec = new X509EncodedKeySpec(alicePublicKey);
KeyFactory keyFact = KeyFactory.getInstance("DH");
PublicKey publicKey = keyFact.generatePublic(x509KeySpec);
KeyAgreement ka = KeyAgreement.getInstance("DH");
ka.init(privateKey);
ka.doPhase(publicKey, true);
String algorithm = "DES";
secretKey = ka.generateSecret(algorithm);
}
public byte[] getSecret() {
return secretKey.getEncoded();
}
public static void main(String[] args) throws InvalidAlgorithmParameterException, NoSuchAlgorithmException, InvalidKeyException,
InvalidKeySpecException, IllegalStateException, IOException {
final DiffieHellman diffieHellman = new DiffieHellman();
System.out.println("P: " + bytesToHexString(diffieHellman.getP().toByteArray()));
System.out.println("P: " + diffieHellman.getP().toByteArray().length);
System.out.println("G: " + bytesToHexString(diffieHellman.getG().toByteArray()));
System.out.println("G: " + diffieHellman.getG().toByteArray().length);
System.out.println();
final byte[] myPublicKey = diffieHellman.startHandshake();
System.out.println("myPublicKey: " + bytesToHexString(myPublicKey));
System.out.println("myPublicKey: " + myPublicKey.length);
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
final String inputByteString = br.readLine();
final byte[] otherPublicKey = hexStringToByteArray(inputByteString);
System.out.println("otherPublicKey: " + bytesToHexString(otherPublicKey));
diffieHellman.endHandshake(otherPublicKey);
System.out.println("secret: " + bytesToHexString(diffieHellman.getSecret()));
}
public static DHParameterSpec genDhParams() {
try {
// Create the parameter generator for a 1024-bit DH key pair
final AlgorithmParameterGenerator paramGen = AlgorithmParameterGenerator.getInstance("DH");
paramGen.init(512);
// Generate the parameters
final AlgorithmParameters params = paramGen.generateParameters();
final DHParameterSpec dhSpec = (DHParameterSpec) params.getParameterSpec(DHParameterSpec.class);
return dhSpec;
} catch (NoSuchAlgorithmException e) {
} catch (InvalidParameterSpecException e) {
}
return null;
}
public static String bytesToHexString(byte[] bytes) {
final String[] hexArray = { "00", "01", "02", "03", "04", "05", "06", "07", "08", "09", "0A", "0B", "0C", "0D", "0E", "0F", "10", "11", "12",
"13", "14", "15", "16", "17", "18", "19", "1A", "1B", "1C", "1D", "1E", "1F", "20", "21", "22", "23", "24", "25", "26", "27", "28",
"29", "2A", "2B", "2C", "2D", "2E", "2F", "30", "31", "32", "33", "34", "35", "36", "37", "38", "39", "3A", "3B", "3C", "3D", "3E",
"3F", "40", "41", "42", "43", "44", "45", "46", "47", "48", "49", "4A", "4B", "4C", "4D", "4E", "4F", "50", "51", "52", "53", "54",
"55", "56", "57", "58", "59", "5A", "5B", "5C", "5D", "5E", "5F", "60", "61", "62", "63", "64", "65", "66", "67", "68", "69", "6A",
"6B", "6C", "6D", "6E", "6F", "70", "71", "72", "73", "74", "75", "76", "77", "78", "79", "7A", "7B", "7C", "7D", "7E", "7F", "80",
"81", "82", "83", "84", "85", "86", "87", "88", "89", "8A", "8B", "8C", "8D", "8E", "8F", "90", "91", "92", "93", "94", "95", "96",
"97", "98", "99", "9A", "9B", "9C", "9D", "9E", "9F", "A0", "A1", "A2", "A3", "A4", "A5", "A6", "A7", "A8", "A9", "AA", "AB", "AC",
"AD", "AE", "AF", "B0", "B1", "B2", "B3", "B4", "B5", "B6", "B7", "B8", "B9", "BA", "BB", "BC", "BD", "BE", "BF", "C0", "C1", "C2",
"C3", "C4", "C5", "C6", "C7", "C8", "C9", "CA", "CB", "CC", "CD", "CE", "CF", "D0", "D1", "D2", "D3", "D4", "D5", "D6", "D7", "D8",
"D9", "DA", "DB", "DC", "DD", "DE", "DF", "E0", "E1", "E2", "E3", "E4", "E5", "E6", "E7", "E8", "E9", "EA", "EB", "EC", "ED", "EE",
"EF", "F0", "F1", "F2", "F3", "F4", "F5", "F6", "F7", "F8", "F9", "FA", "FB", "FC", "FD", "FE", "FF" };
final StringBuilder hexString = new StringBuilder();
hexString.append("{");
for (int i = 0; i < bytes.length; i++) {
hexString.append(" 0x").append(hexArray[0xFF & bytes[i]]).append(",");
}
String result = hexString.toString();
result = result.substring(0, result.length() - 1);
return result += " }";
}
public static byte[] hexStringToByteArray(String s) {
s = s.replaceAll("0x", "").replaceAll(",", "").replaceAll(" ", "");
int len = s.length();
byte[] data = new byte[len / 2];
for (int i = 0; i < len; i += 2) {
data[i / 2] = (byte) ((Character.digit(s.charAt(i), 16) > 1];
for (int i = 0; i < hex.Length >> 1; ++i)
{
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, >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, 0x02, 0x02, 0x01, 0x80, 0x03, 0x44, 0x00, 0x02, 0x41, 0x00, 0xF6, >0xCC, 0x6E, 0x78, 0x97, 0x04, 0xCE, 0x0A, 0x7B, 0x7D, 0x26, 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0x00, 0x00 , 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 , 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 , 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 , 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 , 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00
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
C++ Code without c++ cli wrapper (as all it does is wrap and it works)
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.
UIUtils.h
#define UIUTILS_API __declspec(dllexport)
#include
#include "DiffieHellmanKeysExchanger.h"
#include
#include
#include
#include
using namespace std;
namespace UIUtils {
class UIUtilsClass {
public:
UIUTILS_API static std::vector doAlice(void);
UIUTILS_API static std::vector doBob(std::vector alicePublicKey);
UIUTILS_API static std::vector doAliceAgain(std::vector bobPublicKey);
UIUTILS_API static std::vector getBob(void);
};
}
UIUtils.cpp
#include "stdafx.h"
#include "UIUtils.h"
namespace UIUtils {
const size_t PModuleLength = 228;
unsigned char buffer[PModuleLength] = { 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 };
std::vector cryptoPModule((char*)buffer, (char*)buffer + PModuleLength);
const size_t GModuleLength = 65;
unsigned char gBuffer[GModuleLength] = { 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 };
unsigned long cryptoGModule = (unsigned long) gBuffer;
DiffieHellmanLib::DiffieHellmanKeysExchanger aliceExchanger(cryptoPModule, cryptoGModule);
DiffieHellmanLib::DiffieHellmanKeysExchanger bobExchanger(cryptoPModule, cryptoGModule);
std::vector bobSharedSecret;
std::vector UIUtilsClass::doAlice(void){
std::vector alicePublicKey;
aliceExchanger.GenerateExchangeData(alicePublicKey);
return alicePublicKey;
}
std::vector UIUtilsClass::doBob(std::vector alicePublicKey){
std::vector bobPublicKey;
bobExchanger.GenerateExchangeData(bobPublicKey);
bobExchanger.CompleteExchangeData(alicePublicKey, bobSharedSecret);
return bobPublicKey;
}
std::vector UIUtilsClass::doAliceAgain(std::vector bobPublicKey){
std::vector aliceSharedSecret;
aliceExchanger.CompleteExchangeData(bobPublicKey, aliceSharedSecret);
return aliceSharedSecret;
}
std::vector UIUtilsClass::getBob(void){
return bobSharedSecret;
}
}
Originally posted 2013-11-16 16:09:05.