Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Unknown echo protocol identifier #6

Open
imobdevtech opened this issue Jan 4, 2019 · 0 comments
Open

Unknown echo protocol identifier #6

imobdevtech opened this issue Jan 4, 2019 · 0 comments

Comments

@imobdevtech
Copy link

imobdevtech commented Jan 4, 2019

Hello,

I am planning to implement Noise protocol in My Android Application, I have tried to create demo from the existing Java code and Noise-C code, but every time when I send message through socket to my Noise-Server I get Unknown echo protocol identifier error. Below is my code, will you please help me to find the issue in it.

public String bytesToHex(byte[] bytes) {
char[] hexArray = "0123456789ABCDEF".toCharArray();
char[] hexChars = new char[bytes.length * 2];
for (int j = 0; j < bytes.length; j++) {
int v = bytes[j] & 0xFF;
hexChars[j * 2] = hexArray[v >>> 4];
hexChars[j * 2 + 1] = hexArray[v & 0x0F];
}
return new String(hexChars);
}

public NoiseManager(final Context context) {

    Observable.fromCallable(new Callable<Void>() {
        @RequiresApi(api = Build.VERSION_CODES.KITKAT)
        @Override
        public Void call() throws Exception {

            try {
                HandshakeState initiator = new HandshakeState("Noise_XX_25519_ChaChaPoly_SHA256", INITIATOR);
            
                String init_prologue = "";
             
                TestMessage testMessage = new TestMessage();
                testMessage.ciphertext = bytesToHex("\\n".getBytes());
                testMessage.payload = bytesToHex("\\n".getBytes());

                if (initiator.needsLocalKeyPair()) {
                    StringBuilder clientKey = new StringBuilder();

                    String host = "localhost";
                    int port = 7000;
                    InetAddress address = InetAddress.getByName(host);
                    Socket socket = new Socket(address, port);

                    BufferedReader reader = null, reader1 = null;
                    try {
                        reader = new BufferedReader(
                                new InputStreamReader(context.getAssets().open("client_key_25519"),
                                        "UTF-8"));

                        String line;
                        while ((line = reader.readLine()) != null) {
                            clientKey.append(line);
                        }

                        initiator.getLocalKeyPair().setPrivateKey(clientKey.toString().getBytes()
                                , 0);

                        initiator.start();

                        byte[] message = new byte[128];
                        byte[] plaintext = new byte[128];

                        int len = 0;

                        while (true) {
                            if (initiator.getAction() == WRITE_MESSAGE) {
                                len = initiator.writeMessage(message, 0,
                                        testMessage.payload.getBytes(), 0,
                                        testMessage.payload.getBytes().length);

                                OutputStreamWriter osw = new OutputStreamWriter(socket.getOutputStream());
                                BufferedWriter bw = new BufferedWriter(osw);
                                String strMessage = new String(message, "UTF-8");
                                bw.write(strMessage);
                                bw.flush();

                                Log.e(NoiseManager.class.getSimpleName(),
                                        "Message sent to the server : " + strMessage);
                            }  else {
                                break;
                            }
                        }
                    } catch (IOException e) {
                        e.printStackTrace();
                    } finally {
                        if (reader != null) {
                            try {
                                reader.close();
                                //socket.close();
                            } catch (IOException e) {
                                e.printStackTrace();
                            }
                        }
                    }

                }
            } catch (NoSuchAlgorithmException e) {
                e.printStackTrace();
            } catch (UnknownHostException e) {
                e.printStackTrace();
            } catch (IOException e) {
                e.printStackTrace();
            }

            return null;
        }
    }).observeOn(AndroidSchedulers.mainThread())
            .subscribeOn(Schedulers.io())
            .subscribe(new Subscriber<Void>() {
                @Override
                public void onCompleted() {
                    Log.e(NoiseManager.class.getSimpleName(), "onCompleted");
                }

                @Override
                public void onError(Throwable e) {
                    e.printStackTrace();
                    Log.e(NoiseManager.class.getSimpleName(), "onError");
                }

                @Override
                public void onNext(Void o) {
                    Log.e(NoiseManager.class.getSimpleName(), "onNext");
                }
            });
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant