Git Product home page Git Product logo

crypt's Introduction

Sample with concept explained on how to secure data when transfering from one party to another party and validate the sender (portion of End-to-End Encryption)

The sample code provided is based on nodejs and uses the in-built libraries (without any use of third party libraries)

The Concept

The basic idea is to encrypt the message from party 1 and sent to party 2 and the party 2 decrypts the messsage and verify they received message from the right party (ie. no man in middle attack is done)

For this we are going to choose RSA Key pairs (asymmetric Key Cryptography) to be generated by both parties for their identity. Refer OpenSSL for how to get RSA key pairs.

Each Party will own their Pubic and Private Key pairs.

As the name suggests Public key is the key indented to be shared for communication and the private key is to be kept secret with themselves.

Both parties shares their public keys to each other

Party two share their public key to Party one and Party one will share their public key to Party two (for this sample we are not going to consider how they communicate it securely, we can consider this for now as done offline)

Party 1 encrypts the secured message with Public key of Party 2

var msg = new Buffer('sample text');
var enc = crypto.publicEncrypt(public_party2, msg);

Party 1 signs the secured message with their Private key and get a signature

var signer = crypto.createSign('SHA256');
signer.update(msg.toString());
var sig = signer.sign(private_party1, 'hex');

Party 1 can transfer the encrypted message and signature

We don't care how we do it for now. Lets consider Party 2 receives it

Party 2 decrypts the message using their Private key

var clr = crypto.privateDecrypt(private_party2, enc);

Party 2 will get the clear text on decrypt. But still we aren't safe since we are not aware whether the Party 1 is sending the message. Since some man in middle who has Party 2's public key can still encrypt and send message by acting as Party 1.

Party 2 verifies the message using the Public key of Party 1 and the signature

 var verifier = crypto.createVerify('SHA256');
 verifier.update(clr.toString());
 var result = verifier.verify(public_party1, sig.toString(), 'hex');

Here the result will be true if Party 1 is the sender, since Party 1 alone owns their private key and the signature will be valid only for Party 1's public key pair.

Note

All the security concept expained above are considered secure until they don't share their private key. In any case the private key is stolen or predicted, the security can be compromised. So never ever share private keys and keep it more secured.

The sample code is for demonstration purpose only feel free to use it at your own risk.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.