[JS] Encrypt & Decrypt a string

I need a code where you can enter an input, and by the press of a button, the string becomes a jumbled mess. Basically, i need it to do the following things with the string:

  • Add “t” at the end of it
  • Reverse the string
  • Switch the first 2 characters together (i. e. if the original is AbNzm, the encrypted one is bANzm)

very interesting request :thinking:.
here is something I whipped up quickly.

var string = "text to encrypt" 
// Add "t"
one = string + "t"
// Reverse the string
two = one.split("").reverse().join('')
// Switch the first 2 characters together
split = two.split("")
three = split[1] + split[0] + split.slice(1).join("")
2 Likes

I need it to decrypt when a function is ran, can you also do that?

i tried it might be wrong. :stuck_out_tongue:

var string = "ttpyrcne ot txet" 
split = string.split("")
one = split[1] + split[0] + split.slice(1).join("")
two = one.split("").slice(0).join("")
three = two.split("").reverse().join('')
1 Like

It kinda works.

image

image

3 Likes

Ty guys so much! I’ll add more complexity to the system and implement it to my website :relaxed:

Actually,there are some extra "T"s at the end, can ya get rid of those?

While this is fun, if you plan to use this for real encryption, please dont!

1 Like

I mean, i wanna use it as an automated password generator for my website - so you cannot change your password… but all you do there is just post things, you cannot technically add any personal info there…

Sounds good, as long as you are hashing your passwords.

3 Likes

While this is fun to create, something like bcrypt is already available. Look it up, you may find it useful.

There is an npm package named ‘bcrypt-js’ for javascript users. Documentation is really simple to pickup.

1 Like

Encrypting should also be done on the server as a note.

4 Likes

On a side note, bcrypt is one-directional. You can encrypt passwords, but the only verification method bcrypt offers is the compare function, which accepts two strings. This way, the database admin only sees the encrypted password, and can not decrypt and view the original password.

If you want two-directional encrypting, you can use the built-in module nodejs crypto. For Hacktoberfest I did a little bit of encryption, which you can see here.

2 Likes

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.