[erlang-questions] Jinterface to RSA problem

charles brainshampoo@REDACTED
Sun Oct 1 14:37:14 CEST 2006


Hello,
I am new to Erlang with some Java background. To put my mind at rest
about some missing features I am trying to connect to the Java side
using Jinterface. The Java side seems to be working well. I am also able
to get the RSA public key string with a flush() in the shell of the Erlang
side node, but I can't find a way to get it displayed in an .erl module.
How can get the result back on the Erlang side? 
As I understand, I should spawn the remote module, 
some function inside it, and get it's Pid and receive from that Pid.
But how can this be done on code that was started with the java command? 
Thank you for bearing with me.

(The code for every Java and Erlang file I used follows.)

I compiled the Java code with the following command:

javac -cp .:/usr/local/lib/erlang/lib/jinterface-1.3/priv/OtpErlang.jar gurka3.java

and started the Java node like this:

java -DOtpConnection.trace=4 -classpath .:/usr/local/lib/erlang/lib/jinterface-1.3/priv/OtpErlang.jar gurka3
-> PUBLISH (r4) gurka@REDACTED port=49937
<- OK

I then started another node:

erl -sname node1
Erlang (BEAM) emulator version 5.5 [source] [async-threads:0] [hipe]

Eshell V5.5  (abort with ^G)
(node1@REDACTED)1> net_adm:ping(gurka@REDACTED).
pong

where I was able to ping the Java node (gurka@REDACTED) and confirm
both nodes are up with epmd -names:
epmd: up and running on port 4369 with data:
name node1 at port 49940
name gurka at port 49937

I can also send a message with the required Key length message:

(node1@REDACTED)2> {echo, gurka@REDACTED} ! {self(), 1024}.
{<0.38.0>,1024}
(node1@REDACTED)3> 

It gets answered on the Java node:

<- REG_SEND {6,#Pid<node1@REDACTED>,',echo}
   {#Pid<node1@REDACTED>,1024}
1024
Sun RSA private CRT key, 1024 bits
  modulus:          11698592641286154................
-> SEND {2,',#Pid<node1@REDACTED>}
[83,117,110,32,82,83,65,32,112,117,98,108,105,99,32,.........

I can also flush the result sent to the erlang node in the shell:

(node1@REDACTED)3> flush().
Shell got "Sun RSA public key, 1024 bitsn  modulus: 116985926412861549643428351537401442068945395315880329380936347783710839103091897221317948145702186219833676989450017983327760320285286206568121060246142214498882101528329498904155872540711412245495224009570070210033560284058357681558690299799836757921261113112027577663369501534610442608629632562973393140053n  public exponent: 65537"
ok

!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
My question is: How do I get the result inside an Erlang module?

As I understand, I should spawn the remote module, some function
inside it, and get it's Pid and receive from that Pid. 
But how can this be done on code that was started with the java
command?

My ignorance of Erlang shows in the code below:

%% jinterfaceclient.erl

-module(jinterfaceclient).
-export([start/1, getKey/1]).

start(L) ->
	spawn(fun() -> getKey(L) end).

getKey(Length) ->
	{echo, gurka@REDACTED} ! {self(), Length},
	c:flush(),
	receive
		{_,_,_,L} ->
			[L]
	end.
!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!

My Java code follows:

# RSAStuff2.java ---------------------------------------------------------

import java.security.*;

public class RSAStuff2 {

	public static String getPublicKey(int length) {
	// public static void main(String[] args) {
		PublicKey publicKey = null;
		try {
			KeyPairGenerator keyGen = KeyPairGenerator.getInstance("RSA");
			keyGen.initialize(length);
			KeyPair keypair = keyGen.genKeyPair();
			PrivateKey privateKey = keypair.getPrivate();
			System.out.println(privateKey);
			publicKey = keypair.getPublic();
			// System.out.println(publicKey);
			} catch (java.security.NoSuchAlgorithmException e) {
			}
			return publicKey.toString();
		}
}

# gurka3.java ---------------------------------------------------------

import com.ericsson.otp.erlang.*;

public class gurka3 {

	public static void main(String[] args) {
		OtpNode self = null;
		OtpMbox mbox = null;
		try {
			self = new OtpNode("gurka");
			mbox = self.createMbox("echo");
		} catch (Exception e) {
			System.out.println("" + e);
		}
		OtpErlangObject o;
		OtpErlangTuple msg;
		OtpErlangPid from;

		while (true) {
			try {
				o = mbox.receive();
				if (o instanceof OtpErlangTuple) {
					msg = (OtpErlangTuple)o;
					from = (OtpErlangPid)(msg.elementAt(0));
					OtpErlangLong length = (OtpErlangLong)(msg.elementAt(1));
					System.out.println(length.intValue());
					int l = length.intValue();
					mbox.send(from,new OtpErlangList(RSAStuff2.getPublicKey(l)));
				}
				} catch (Exception e) {
					System.out.println("" + e);
				}
		}
	}
	
}

------------------------------------------------------------------------------------
_________________________________________________________
Post sent from http://www.trapexit.org



More information about the erlang-questions mailing list