Coder Social home page Coder Social logo

node-crypto-examples's Introduction

node-crypto-examples

This repository is a collection of various scripts that illustrate the use of crypto with nodejs. The goal of this repository is to ease the start and to enable a quick start for developers.

Currently the collections includes:

References:

Contributing

  1. Fork it ( https://github.com/chris-rock/node-encryption-examples/fork )
  2. Create your feature branch (git checkout -b my-new-feature)
  3. Commit your changes (git commit -am 'Add some feature')
  4. Push to the branch (git push origin my-new-feature)
  5. Create new Pull Request

Author

Licence

MIT

node-crypto-examples's People

Contributors

chris-rock avatar utsav2 avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar

node-crypto-examples's Issues

Just a thank you!

Sorry for the "issue," but Github doesn't provide a means for positive feedback. Your article and sample code were really helpful and made getting secure, node-based encryption into my app take only a couple of minutes! So, thanks and keep up the great work!

Not a string or buffer

I am following your blog (http://lollyrock.com/articles/nodejs-encryption/) and wrote a small example to test it out.

const crypto = require('crypto');
const algorithm = 'aes192';

var cipher = crypto.createCipher(algorithm,'neeraj');
var crypted = cipher.update(text,'utf8','hex');
crypted += cipher.final('hex');
winston.log('crypted', crypted);

However it gives me "Not a string or buffer" error. What am I doing wrong?

Unknown Cipher

While calling the decrypt(text) function, it's resulting into error:

TypeError: Cipher type must be a string

Here's the code:

function decryptText(text) {
    var decipher = crypto.createDecipher(ENCRYPTION_ALGO, ENCRYPTION_KEY)
    var dec = decipher.update(text,'hex','utf8')
    dec += decipher.final('utf8');
    console.log(dec);
    return dec;
}

I made sure that typeof text here is already string

Uncaught Error: EMFILE: too many open files, open

Hello,

     I am trying to encrypt almost 10000+ file in a row using following function.
     It gives error something like:
     ## Uncaught Error: EMFILE: too many open files, open 'bun.ogg' 

Source code is given bellow.

    function copyRecursiveSync(src, dest) {
var exists = fs.existsSync(src);
var stats = exists && fs.statSync(src);
var unSelectedArray = getUnSelectedOptionArray();
var isDirectory = exists && stats.isDirectory();
if (exists && isDirectory) {
    var destExist = fs.existsSync(dest);
    if (!destExist)
        fs.mkdirSync(dest);
    fs.readdirSync(src).forEach(function (childItemName) {
        copyRecursiveSync(path.join(src, childItemName),
            path.join(dest, childItemName));
    });
} else {

    var splitArray = src.split(".");

    if (isFileEncrypt(splitArray[1], unSelectedArray) === true) {
        // input file
        var r = fs.createReadStream(src);
        // zip content
        var zip = zlib.createGzip();
        // encrypt content
        var encrypt = crypto.createCipher(algorithm, password);
        // decrypt content
        var decrypt = crypto.createDecipher(algorithm, password)
            // unzip content
            var unzip = zlib.createGunzip();
        // write file
        var w = fs.createWriteStream(dest);

        // start pipe
        r.pipe(zip).pipe(encrypt).pipe(w);
        //.pipe(decrypt).pipe(unzip)
    } else {
        fs.linkSync(src, dest);
    }
}

}

Please help me to solve this. I am very new in this technology.
     Any Other solution for doing this is also excepted. 

crypto-gcm.js: TypeError: Object #<Cipheriv> has no method 'getAuthTag'

Running the unmodified sample code for crypto-gcm.js I get an error:

/home/sop/switch/test/cr.js:15
  var tag = cipher.getAuthTag();
               ^
TypeError: Object #<Cipheriv> has no method 'getAuthTag'
    at encrypt (/home/sop/switch/test/cr.js:15:20)
    at Object.<anonymous> (/home/sop/switch/test/cr.js:30:10)
    at Module._compile (module.js:456:26)
    at Object.Module._extensions..js (module.js:474:10)
    at Module.load (module.js:356:32)
    at Function.Module._load (module.js:312:12)
    at Function.Module.runMain (module.js:497:10)
    at startup (node.js:119:16)
    at node.js:906:3

Nonce for AES encryption

Your Node.js aes encryption examples appeared very high in Google search results, and I found them very helpful โ€“ thanks!

However, playing around with it I noticed that the same ciphertext is generated each time, which is certainly not best practice...

Going through the Node crypto documentation, I noticed they say:

The implementation of crypto.createCipher() derives keys using the OpenSSL function EVP_BytesToKey with the digest algorithm set to MD5, one iteration, and no salt. The lack of salt allows dictionary attacks as the same password always creates the same key. The low iteration count and non-cryptographically secure hash algorithm allow passwords to be tested very rapidly.

In line with OpenSSL's recommendation to use pbkdf2 instead of EVP_BytesToKey it is recommended that developers derive a key and IV on their own using crypto.pbkdf2 and to use [crypto.createCipheriv()][] to create the Cipher object.

Deriving IVs seems rather involved. Do you have any examples of how to do this?

Best approach

Hi Chris,

I loved the examples you put up. I appreciate it. I however fail to understand how to use any of this in my application. My Node.Js app does queries to mysql database. The query needs username password to query the db. As of now, I have the db username password stored as clear text in my index.js script. However, I cannot pass this on to anyone else, cause they can see the credentials. If i used any of the functions you described, it looks like I can encrypt my username and password, but then to decrypt it I still to pass on "password = 'd6F3Efeq';" to decrypt it. Since this nodejs app, the javascript is open and anyone who can access "password = 'd6F3Efeq';" can decrypt those credentials right? How can we prevent that?

I cant really use hash as it is one way encryption and thats quite useless in my case. Can you suggest if I misunderstood your post?

Regards,
Chandy

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.