Skip to content
← Back to Community
How to encrypt and decrypt in node?
Profile icon
ChezCoder

Is there maybe a built in function or like a module that can help me?

Voters
Profile icon
AlbertPeterson1
Profile icon
bobchewmango
Profile icon
StealthHydra179
Profile icon
ChezCoder
Comments
hotnewtop
Profile icon
teymour

Maybe the built in crypto module would do the trick?

Profile icon
ChezCoder

@CaptainAnon @CaptainAnon yeah, i checked it but the examples didnt help. I didn't get it

Profile icon
teymour

@ChezCoder Cryptography is a pretty big and complicated field. What are you trying to do?

Profile icon
ChezCoder

@CaptainAnon a basic password encrypt and decryption

Profile icon
teymour

@ChezCoder To store passwords, it's normal to use one way encryption (hashing). This means that you can encrypt the data, but it's impossible to decrypt it. However, whenever you want to log a user in you can just find the hash for the value of their password and then check that against the stored hash. You can do this pretty easily with the Stanford JS Crypto Module. Try an algorithm like sha-256 or sha-512.
You can also use the built in crypto module's createHash function.

Profile icon
ChezCoder

@CaptainAnon thank you, though I am aware of that method. I want to know how to "unhash" or decrypt an encrypted value using ciphers

Profile icon
teymour

@ChezCoder You can't "unhash" a hash. For two way encryption you could use something like RSA (for sending messages between parties).

Profile icon
ChezCoder

@CaptainAnon yes, i know. which is why I want to learn how to use ciphers.

Profile icon
teymour

Try the builtin crypto module.

Profile icon
ChezCoder

@CaptainAnon ok. its a bit confusing though, can you maybe get me a snippet? Thanks :P

Profile icon
AmazingMech2418

@ChezCoder @CaptainAnon You can "unhash" a hash using brute force. However, there is some overlap between original values and hashes due to predefined hash lengths.

Profile icon
ChezCoder

@AmazingMech2418 yes but this would be really hard when dealing with things like bios or long strings of text that involve special chars etc

Profile icon
AmazingMech2418

@ChezCoder Yes. If you are encoding stuff like that, I'd recommend using the Diffie-Hellman Key Exchange or RSA. Both are in my Cryptography 101 post if you want to learn more.

Profile icon
ChezCoder

@AmazingMech2418 ok... or I can just use my encryption library in python: https://repl.it/@ChezCoder/aCrypt
https://pypi.org/project/aCrypt/

Profile icon
AmazingMech2418

@ChezCoder Well, not if you are using Node...

Profile icon
ChezCoder

@AmazingMech2418 yes but I was thinking about passing info to python then getting the encrypted code / decrypted code back

Profile icon
AmazingMech2418

@ChezCoder By the way, what algorithm did you use in your aCrypt library? It looks like maybe a modified Caesar Cipher?

Profile icon
ChezCoder
Profile icon
SixBeeps

What encryption algorithm are you looking for? Node runs off of JS, so theoretically any JS library would work.

Profile icon
ChezCoder

@SixBeeps im really new to encryption and decryption. Never done it before