Skip to main content
Cut 31 bytes
Source Link
Dave
  • 8.6k
  • 4
  • 20
  • 38

Node.js, 638638 607 bytes

R=require,P=process,s=R('net'),y=R('crypto'),W='write'w=0,C='create',S='toString',G='generateKeys'W='write',F=C+'DiffieHellman'D='data',B='hex',D='data'G=_=>a.generateKeys(B),Y=(t,m,g,f)=>g((c=y[C+t+'ipher']('aes192',w,k='')).on('readable',_=>k+=(c.read()||'').toString(m)).on('end',_=>f(k)))+c.end(),F=C+'DiffieHellman',X=s=>s.on(D,x=>x[S]x=>(x+'').split(GB).map(p=>p&&(w?Y('Dec','utf8',c=>c[W](p,B),console.log):(p=p.split(S),a||(a=y[F](p[1],B,p[2],B),s[W](a[G](B))),w=a.computeSecret(p[0],B),P.stdin.on(D,m=>Y('C',B,c=>c[W](m),r=>s[W](r+G)))))r+B)),a=w=0,Y=(t,m[p,gq,fr]=p.split(D)=>g,r&&s[W](G(c=y[C+t+'ipher']a=y[F]('aes192'q,wB,k='')).on('readable'r,_=>k+=(c.read()||''B)[S](m)),w=a.oncomputeSecret('end'p,_=>f(kB)))+c.end()));(R=P.argv)[3]?X(s=ss.Socket()).connect(R[3],R[2])+X(s):(s=s[C+'Server']s[C+'Server'](s=>s=>X(s,a=y[F](2<<9),X(s),s[W][W](a[G]G(B)+S+a+D+a.getPrime(B)+S+a+D+a.getGenerator(B)+G)+B)).listen(R[2])) 
R=require,P=process,s=R('net'),y=R('crypto'),W='write'w=0,C='create',S='toString'W='write',GD='data',B ='generateKeys'='hex',F=C+'DiffieHellman'G=_=>a.generateKeys(B),B='hex'Y=(t,D='data'm,g,f)=>g((c=y[C+t+'ipher']('aes192',w,k='')) .on('readable',_=>k+=(c.read()||'').toString(m)).on('end',_=>f(k)))+c.end(),F=C+ 'DiffieHellman',X=s=>s.on(D,x=>x[S]x=>(x+'').split  (GB).map(p=>p&&(w?Y('Dec','utf8',c=>c[W]c=>c[ W](p,B),console.log):(p=p.split(S),a||(a=y[ F](p[1],B,p[2],B),s[W](a[G](B))),w=a.computeSecret(p[0],B),P.stdin.on(D,m=>Y('C'  ,B,c=>c[W](m),r=>s[W](r+G)))))r+B)),a=w=0,Y=(t,m[p,gq,fr]=p .split(D)=>g,r&&s[W](G(c=y[C+t+'ipher']a=y[F]('aes192' q,wB,k='')).on('readable'r,_=>k+=(c.read()||''B)[S](m)),w=a.oncomputeSecret('end'p,_=>f(kB)))+c.end()));(  R=P.argv)[3]? X(s=ss.Socket()).connect(R[3],R[2])+X(s):(s=s[C+'Server']s[C+'Server'](s=>s=>X(s,a=y[F](  2<<9),X(s),s[W][W](a[G]G(B)+S+a+D+a.  getPrime(B)+S+a+D+a.getGenerator(B)+G)+B)).listen(R[2])) 
s=require('net'), y=require('crypto'), X=s=>sw=0, // Shared secret starts unknown Y=(t,m,g,f)=>g( // Helper for encryption & decryption (c=y['create'+t+'ipher']('aes192',w,k='')) .on('data''readable',x=>x_=>k+=(c.read()||'').toString(m)) .on('end',_=>f(k)))+c.end(); X=s=>s.on('data',x=>(x+'').split('TOKEN2').map(p=> p&&(w // Have we completed handshake? ?Y('Dec','utf8',c=>c.write(p,'hex'),console.log) // Decrypt + print messages :(  // Haven't completed handshake:   process.stdin.on('data',m=> p=p // Prepare to encrypt + send input Y('C','hex',c=>c.splitwrite('TOKEN1'm),r=>s.write(r+'TOKEN2')),(  [p,q,r]=p.split('TOKEN1'), // Split up DH data sent to us a||( r&& //  No keyGiven pairDH yetdetails? (client) a=y s.createDiffieHellmanwrite(  (a=y.createDiffieHellman(  // Compute onekey pair... p[1] q,'hex',p[2]r,'hex'),  // ...using the received params s.write(a ).generateKeys('hex'))),  // And send the public key   w=a.computeSecret(p[0]p,'hex'),  // Compute shared secret   //,console.log(w.toString('hex'));  // Print if you want to verify no MITM process.stdin.on('data',m=> // Encrypt + send input Y('C','hex',c=>c.write(m),r=>s.write(r+'TOKEN2'))) ) ) )), a=w=0, // Agent & Shared secret start unknown Y=(t,m,g,f)=>g( // Helper function for encryption & decryption  (c=y['create'+t+'ipher']('aes192',w,k='')) .on('readable',_=>k+=(c.read()||'').toString(m)) .on('end',_=>f(k)))+c.end(); (R=process.argv)[3] // Are we running as a client? ?X(s=ss.Socket()).connect(R[3],R[2])+X(s)  // Connect & start chat :(s=ss.createServer(s=>(  // Start server. On connection: a=y.createDiffieHellmanX(1024)s, // Calc DiffieHellman,  X(s),  // Start chat,  a=y.createDiffieHellman(1024)) // StartCalc chatDiffieHellman, s.write(  // Send public key & public DH details a.generateKeys('hex')+'TOKEN1'+ a.getPrime('hex')+'TOKEN1'+ a.getGenerator('hex')+'TOKEN2' ) )).listen(R[2]))  // Listen on requested port 

The only requirement for the tokens is that they contain at least one non-hex character, so in the minified code other string constants are used (toStringdata and generateKeyshex).

Node.js, 638 bytes

R=require,P=process,s=R('net'),y=R('crypto'),W='write',C='create',S='toString',G='generateKeys',F=C+'DiffieHellman',B='hex',D='data',X=s=>s.on(D,x=>x[S]().split(G).map(p=>p&&(w?Y('Dec','utf8',c=>c[W](p,B),console.log):(p=p.split(S),a||(a=y[F](p[1],B,p[2],B),s[W](a[G](B))),w=a.computeSecret(p[0],B),P.stdin.on(D,m=>Y('C',B,c=>c[W](m),r=>s[W](r+G))))))),a=w=0,Y=(t,m,g,f)=>g((c=y[C+t+'ipher']('aes192',w,k='')).on('readable',_=>k+=(c.read()||'')[S](m)).on('end',_=>f(k)))+c.end();(R=P.argv)[3]?(s=s.Socket()).connect(R[3],R[2])+X(s):(s=s[C+'Server'](s=>(a=y[F](2<<9),X(s),s[W](a[G](B)+S+a.getPrime(B)+S+a.getGenerator(B)+G))).listen(R[2])) 
R=require,P=process,s=R('net'),y=R('crypto'),W='write',C='create',S='toString',G ='generateKeys',F=C+'DiffieHellman',B='hex',D='data',X=s=>s.on(D,x=>x[S]().split  (G).map(p=>p&&(w?Y('Dec','utf8',c=>c[W](p,B),console.log):(p=p.split(S),a||(a=y[ F](p[1],B,p[2],B),s[W](a[G](B))),w=a.computeSecret(p[0],B),P.stdin.on(D,m=>Y('C'  ,B,c=>c[W](m),r=>s[W](r+G))))))),a=w=0,Y=(t,m,g,f)=>g((c=y[C+t+'ipher']('aes192' ,w,k='')).on('readable',_=>k+=(c.read()||'')[S](m)).on('end',_=>f(k)))+c.end();(  R=P.argv)[3]?(s=s.Socket()).connect(R[3],R[2])+X(s):(s=s[C+'Server'](s=>(a=y[F](  2<<9),X(s),s[W](a[G](B)+S+a.getPrime(B)+S+a.getGenerator(B)+G))).listen(R[2])) 
s=require('net'), y=require('crypto'), X=s=>s.on('data',x=>x.toString().split('TOKEN2').map(p=> p&&(w // Have we completed handshake? ?Y('Dec','utf8',c=>c.write(p,'hex'),console.log) // Decrypt + print messages :( // Haven't completed handshake: p=p.split('TOKEN1'), // Split up DH data sent to us a||( //  No key pair yet? (client) a=y.createDiffieHellman( // Compute one... p[1],'hex',p[2],'hex'), // ...using the received params s.write(a.generateKeys('hex'))), // And send the public key w=a.computeSecret(p[0],'hex'), // Compute shared secret //console.log(w.toString('hex'));  // Print if you want to verify no MITM process.stdin.on('data',m=> // Encrypt + send input Y('C','hex',c=>c.write(m),r=>s.write(r+'TOKEN2'))) ) ) )), a=w=0, // Agent & Shared secret start unknown Y=(t,m,g,f)=>g( // Helper function for encryption & decryption  (c=y['create'+t+'ipher']('aes192',w,k='')) .on('readable',_=>k+=(c.read()||'').toString(m)) .on('end',_=>f(k)))+c.end(); (R=process.argv)[3] // Are we running as a client? ?(s=s.Socket()).connect(R[3],R[2])+X(s) // Connect & start chat :(s=s.createServer(s=>( // Start server. On connection: a=y.createDiffieHellman(1024), // Calc DiffieHellman,  X(s), // Start chat, s.write( // Send public key & public DH details a.generateKeys('hex')+'TOKEN1'+ a.getPrime('hex')+'TOKEN1'+ a.getGenerator('hex')+'TOKEN2' ) )).listen(R[2])) // Listen on requested port 

The only requirement for the tokens is that they contain at least one non-hex character, so in the minified code other string constants are used (toString and generateKeys).

Node.js, 638 607 bytes

R=require,P=process,s=R('net'),y=R('crypto'),w=0,C='create',W='write',D='data',B='hex',G=_=>a.generateKeys(B),Y=(t,m,g,f)=>g((c=y[C+t+'ipher']('aes192',w,k='')).on('readable',_=>k+=(c.read()||'').toString(m)).on('end',_=>f(k)))+c.end(),F=C+'DiffieHellman',X=s=>s.on(D,x=>(x+'').split(B).map(p=>p&&(w?Y('Dec','utf8',c=>c[W](p,B),console.log):P.stdin.on(D,m=>Y('C',B,c=>c[W](m),r=>s[W](r+B)),([p,q,r]=p.split(D),r&&s[W](G(a=y[F](q,B,r,B))),w=a.computeSecret(p,B))))));(R=P.argv)[3]?X(s.Socket()).connect(R[3],R[2]):s[C+'Server'](s=>X(s,a=y[F](2<<9))[W](G()+D+a.getPrime(B)+D+a.getGenerator(B)+B)).listen(R[2]) 
R=require,P=process,s=R('net'),y=R('crypto'),w=0,C='create',W='write',D='data',B ='hex',G=_=>a.generateKeys(B),Y=(t,m,g,f)=>g((c=y[C+t+'ipher']('aes192',w,k='')) .on('readable',_=>k+=(c.read()||'').toString(m)).on('end',_=>f(k)))+c.end(),F=C+ 'DiffieHellman',X=s=>s.on(D,x=>(x+'').split(B).map(p=>p&&(w?Y('Dec','utf8',c=>c[ W](p,B),console.log):P.stdin.on(D,m=>Y('C',B,c=>c[W](m),r=>s[W](r+B)),([p,q,r]=p .split(D),r&&s[W](G(a=y[F](q,B,r,B))),w=a.computeSecret(p,B))))));(R=P.argv)[3]? X(s.Socket()).connect(R[3],R[2]):s[C+'Server'](s=>X(s,a=y[F](2<<9))[W](G()+D+a.  getPrime(B)+D+a.getGenerator(B)+B)).listen(R[2]) 
s=require('net'), y=require('crypto'), w=0, // Shared secret starts unknown Y=(t,m,g,f)=>g( // Helper for encryption & decryption (c=y['create'+t+'ipher']('aes192',w,k='')) .on('readable',_=>k+=(c.read()||'').toString(m)) .on('end',_=>f(k)))+c.end(); X=s=>s.on('data',x=>(x+'').split('TOKEN2').map(p=> p&&(w // Have we completed handshake? ?Y('Dec','utf8',c=>c.write(p,'hex'),console.log) // Decrypt + print messages :  // Haven't completed handshake:   process.stdin.on('data',m=>  // Prepare to encrypt + send input Y('C','hex',c=>c.write(m),r=>s.write(r+'TOKEN2')),(  [p,q,r]=p.split('TOKEN1'), // Split up DH data sent to us  r&& // Given DH details? (client)  s.write(  (a=y.createDiffieHellman(  // Compute key pair...  q,'hex',r,'hex')  // ...using the received params  ).generateKeys('hex')),  // And send the public key   w=a.computeSecret(p,'hex')  // Compute shared secret   //,console.log(w.toString('hex')) // Print if you want to verify no MITM ))))), (R=process.argv)[3] // Are we running as a client? ?X(s.Socket()).connect(R[3],R[2])  // Connect & start chat :s.createServer(s=>  // Start server. On connection: X(s,   // Start chat,  a=y.createDiffieHellman(1024)) // Calc DiffieHellman, .write(  // Send public key & public DH details a.generateKeys('hex')+'TOKEN1'+ a.getPrime('hex')+'TOKEN1'+ a.getGenerator('hex')+'TOKEN2') ).listen(R[2])  // Listen on requested port 

The only requirement for the tokens is that they contain at least one non-hex character, so in the minified code other string constants are used (data and hex).

Source Link
Dave
  • 8.6k
  • 4
  • 20
  • 38

Node.js, 638 bytes

Now that it's been well and truly beaten (and in the same language), here's my test answer:

R=require,P=process,s=R('net'),y=R('crypto'),W='write',C='create',S='toString',G='generateKeys',F=C+'DiffieHellman',B='hex',D='data',X=s=>s.on(D,x=>x[S]().split(G).map(p=>p&&(w?Y('Dec','utf8',c=>c[W](p,B),console.log):(p=p.split(S),a||(a=y[F](p[1],B,p[2],B),s[W](a[G](B))),w=a.computeSecret(p[0],B),P.stdin.on(D,m=>Y('C',B,c=>c[W](m),r=>s[W](r+G))))))),a=w=0,Y=(t,m,g,f)=>g((c=y[C+t+'ipher']('aes192',w,k='')).on('readable',_=>k+=(c.read()||'')[S](m)).on('end',_=>f(k)))+c.end();(R=P.argv)[3]?(s=s.Socket()).connect(R[3],R[2])+X(s):(s=s[C+'Server'](s=>(a=y[F](2<<9),X(s),s[W](a[G](B)+S+a.getPrime(B)+S+a.getGenerator(B)+G))).listen(R[2])) 

Or with wrapping:

R=require,P=process,s=R('net'),y=R('crypto'),W='write',C='create',S='toString',G ='generateKeys',F=C+'DiffieHellman',B='hex',D='data',X=s=>s.on(D,x=>x[S]().split (G).map(p=>p&&(w?Y('Dec','utf8',c=>c[W](p,B),console.log):(p=p.split(S),a||(a=y[ F](p[1],B,p[2],B),s[W](a[G](B))),w=a.computeSecret(p[0],B),P.stdin.on(D,m=>Y('C' ,B,c=>c[W](m),r=>s[W](r+G))))))),a=w=0,Y=(t,m,g,f)=>g((c=y[C+t+'ipher']('aes192' ,w,k='')).on('readable',_=>k+=(c.read()||'')[S](m)).on('end',_=>f(k)))+c.end();( R=P.argv)[3]?(s=s.Socket()).connect(R[3],R[2])+X(s):(s=s[C+'Server'](s=>(a=y[F]( 2<<9),X(s),s[W](a[G](B)+S+a.getPrime(B)+S+a.getGenerator(B)+G))).listen(R[2])) 

Usage

This is a server/client implementation; one instantiation will be the server, and the other the client. The server is launched with a specific port, then the client is pointed to the server's port. DH can take a few seconds to set-up if your machine is low on entropy, so the first messages may be delayed a little.

MACHINE 1 MACHINE 2 $ node e2e.js <port> : : $ node e2e.js <address> <port> $ hello : : : hello : $ hi : hi : 

Breakdown

s=require('net'), y=require('crypto'), X=s=>s.on('data',x=>x.toString().split('TOKEN2').map(p=> p&&(w // Have we completed handshake? ?Y('Dec','utf8',c=>c.write(p,'hex'),console.log) // Decrypt + print messages :( // Haven't completed handshake: p=p.split('TOKEN1'), // Split up DH data sent to us a||( // No key pair yet? (client) a=y.createDiffieHellman( // Compute one... p[1],'hex',p[2],'hex'), // ...using the received params s.write(a.generateKeys('hex'))), // And send the public key w=a.computeSecret(p[0],'hex'), // Compute shared secret //console.log(w.toString('hex')); // Print if you want to verify no MITM process.stdin.on('data',m=> // Encrypt + send input Y('C','hex',c=>c.write(m),r=>s.write(r+'TOKEN2'))) ) ) )), a=w=0, // Agent & Shared secret start unknown Y=(t,m,g,f)=>g( // Helper function for encryption & decryption (c=y['create'+t+'ipher']('aes192',w,k='')) .on('readable',_=>k+=(c.read()||'').toString(m)) .on('end',_=>f(k)))+c.end(); (R=process.argv)[3] // Are we running as a client? ?(s=s.Socket()).connect(R[3],R[2])+X(s) // Connect & start chat :(s=s.createServer(s=>( // Start server. On connection: a=y.createDiffieHellman(1024), // Calc DiffieHellman, X(s), // Start chat, s.write( // Send public key & public DH details a.generateKeys('hex')+'TOKEN1'+ a.getPrime('hex')+'TOKEN1'+ a.getGenerator('hex')+'TOKEN2' ) )).listen(R[2])) // Listen on requested port 

The only requirement for the tokens is that they contain at least one non-hex character, so in the minified code other string constants are used (toString and generateKeys).