Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //server sends to proxy
- public ByteBuffer decryptServerGame(ByteBuffer packet, boolean first) {
- try {
- if(first) { // first packet, server sends to proxy
- Cipher cii = Cipher.getInstance("Blowfish/CFB64/NoPadding");
- Key key = new SecretKeySpec(STATIC_KEY.getBytes("ASCII"), "Blowfish");
- cii.init(Cipher.DECRYPT_MODE, key, new IvParameterSpec(" ".getBytes()));
- ByteBuffer bb = ByteBuffer.wrap(cii.doFinal(packet.array())).order(ByteOrder.LITTLE_ENDIAN);
- int len = bb.getInt(11);
- int junkLen = bb.getInt(15);
- int index = 19 + junkLen;
- byte[] serverIV = new byte[bb.getInt(index)];
- index+=4;
- System.arraycopy(bb.array(), index, serverIV, 0, serverIV.length);
- index+=serverIV.length;
- byte[] clientIV = new byte[bb.getInt(index)];
- index+=4;
- System.arraycopy(bb.array(), index, clientIV, 0, clientIV.length);
- index+=clientIV.length;
- int strLen = bb.getInt(index);
- index+=4;
- String p = new String(bb.array(), index, strLen);
- index+=strLen;
- strLen = bb.getInt(index);
- index+=4;
- String g = new String(bb.array(), index, strLen);
- index+=strLen;
- strLen = bb.getInt(index);
- index+=4;
- String pubKey = new String(bb.array(), index, strLen);
- exchange.serverPubKey= new BigInteger(pubKey, 16);
- exchange.serverIV = serverIV;
- exchange.clientIV = clientIV;
- System.out.println("Client IV Len: " + clientIV.length);
- System.out.println("Server IV Len: " + serverIV.length);
- System.out.println("Key Len: " + pubKey.length());
- KeyPairGenerator keyGen = KeyPairGenerator.getInstance("DH");
- BigInteger p512 = new BigInteger(p, 16);
- BigInteger g512 = new BigInteger(g, 16);
- DHParameterSpec dhSpec = new DHParameterSpec(p512, g512);
- keyGen.initialize(dhSpec, new SecureRandom());
- KeyAgreement serverKeyAgree = KeyAgreement.getInstance("DH");
- KeyAgreement clientKeyAgree = KeyAgreement.getInstance("DH");
- keyGen = KeyPairGenerator.getInstance("DH"); // new instance, just incase.
- KeyPair serverKeyPair = keyGen.generateKeyPair();
- KeyPair clientKeyPair = keyGen.generateKeyPair();
- KeyFactory k = KeyFactory.getInstance("DH");
- serverKeyAgree.init(serverKeyPair.getPrivate());
- clientKeyAgree.init(clientKeyPair.getPrivate());
- serverKeyAgree.doPhase(new SecretKeySpec(exchange.serverPubKey.toByteArray(), "DH"), true);
- clientKeyAgree.doPhase(new SecretKeySpec(exchange.clientPubKey.toByteArray(), "DH"), true);
- exchange.serverSecretKey = serverKeyAgree.generateSecret("DH");
- exchange.clientSecretKey = clientKeyAgree.generateSecret("DH");
- cii.init(Cipher.ENCRYPT_MODE, key);
- return ByteBuffer.wrap(cii.doFinal(bb.array())); // encrypt it and send it back.
- } else {
- }
- } catch(Exception e) {
- e.printStackTrace();
- }
- return packet;
- }
Add Comment
Please, Sign In to add comment