Encryption/Decryption using cryptr

Now the next hurdle was to load the data in such a way that it should be available to any person who is visiting the link of annotated site but only the users who are authorized and created that site are able to save the site data.
So for that I am using the encryption and decryption of the userId’s. 


Encryption is a security method in which information is encoded in such a way that only authorized user can read it. It uses encryption algorithm to generate ciphertext that can only be read if decrypted.

Types of Encryption

There are two types of encryptions schemes as listed below:

  • Symmetric Key encryption
  • Public Key encryption


Symmetric key encryption algorithm uses same cryptographic keys for both encryption and decryption of cipher text.



Public key encryption algorithm uses pair of keys, one of which is a secret key and one of which is public. These two keys are mathematically linked with each other.



Decryption is the process of taking encoded or encrypted text or other data and converting it back into text that you or the computer can read and understand. This term could be used to describe a method of un-encrypting the data manually or with un-encrypting the data using the proper codes or keys.

Data may be encrypted to make it difficult for someone to steal the information. Some companies also encrypt data for general protection of company data and trade secrets. If this data needs to be viewable, it may require decryption. If a decryption passcode or key is not available, special software may be needed to decrypt the data using algorithms to crack the decryption and make the data readable.

So for using the encryption and decryption in my application, I am using cryptr npm package.


cryptr is a simple encrypt and decrypt module for node.js

It is for doing simple encryption of values UTF-8 strings that need to be decrypted at a later time.

If you require anything more than that you probably want to use something more advanced or crypto directly.

The Cryptr constructor takes 1 required and 1 optional argument.

Cryptr(secret[, algorithm])

If an algorithm is not provided it defaults to aes-256-ctr.


Passwords should be a one way hash. Use bcrypt for that.


npm install cryptr
How I am using this library is on the user creation I am storing the encryption of user inside another collection crypted users.

// on the creation of user
Accounts.onCreateUser((options,user) => {
console.log(“user created “,user._id,user);
if(user._id) {
const encryptedString = cryptr.encrypt(user._id),
decryptedString = cryptr.decrypt(encryptedString);

console.log(encryptedString,”encrypte”); // 8db06003a73db491e9a27ae9e10d106d29
console.log(decryptedString,user._id,”decrypted”); // bacon
if(encryptedString && decryptedString) {
const cryptId = CryptedUsers.insert({userId : user._id, encryptedUserId: encryptedString, date : new Date()})
return user;
return user;

Storing the encrypted form of users and then using that encrypted user inside the mongo collection for loading of data.


Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s