DEV Community

Sh Raj
Sh Raj

Posted on • Updated on • Originally published at github.com

EncriptorJS: Secure Text Encryption and Decryption in JavaScript

Introducing EncriptorJS: Secure Text Encryption and Decryption in JavaScript

GitHub logo SH20RAJ / EncriptorJS

EncriptorJS is a JavaScript text encryption library that allows you to securely encrypt and decrypt text. It provides a simple interface to convert your text into an encrypted form and optionally add a key for additional security. Only the correct key can be used to decrypt the text, ensuring that unauthorized access is prevented.

EncriptorJS

npm version Visitors

EncriptorJS is a JavaScript text encryption library that allows you to securely encrypt and decrypt text. It provides a simple interface to convert your text into an encrypted form and optionally add a key for additional security. Only the correct key can be used to decrypt the text, ensuring that unauthorized access is prevented.

Installation

You can use EncriptorJS by including the library in your JavaScript project or HTML file.

In a JavaScript project

  1. Download the EncriptorJS library file (encriptor.js) from the GitHub repository.
  2. Move the encriptor.js file into your project directory.
  3. In your JavaScript file, import the EncriptorJS library:
   import Encriptor from './encriptor.module.js';
Enter fullscreen mode Exit fullscreen mode

or Use

npm i encriptorjs
Enter fullscreen mode Exit fullscreen mode
   import Encriptor from 'encriptorjs';
Enter fullscreen mode Exit fullscreen mode

In an HTML file

  1. Download the EncriptorJS library file (encriptor.js) from the GitHub repository or NPMJS.

  2. Move the encriptor.js file into your project directory.

  3. In your HTML file…

EncriptorJS is a versatile JavaScript text encryption library designed to empower developers with an easy-to-use interface for secure text encryption and decryption. With EncriptorJS, you can safeguard sensitive information by converting plain text into encrypted form and decrypting it back when needed. This library offers the flexibility to add an optional key for enhanced security, ensuring that only authorized users with the correct key can decrypt the text.

Installation Made Simple

Getting started with EncriptorJS is straightforward, whether you're working on a JavaScript project or integrating it into an HTML file.

For JavaScript Projects

Option 1: Download from GitHub

  1. Download encriptor.js from the GitHub repository.
  2. Move encriptor.js into your project directory.
  3. Import EncriptorJS in your JavaScript file:
   import Encriptor from './encriptor.module.js';
Enter fullscreen mode Exit fullscreen mode

Option 2: NPM Installation

Alternatively, if you're using npm:

   npm i encriptorjs
Enter fullscreen mode Exit fullscreen mode

Then, import EncriptorJS in your JavaScript file:

   import Encriptor from 'encriptorjs';
Enter fullscreen mode Exit fullscreen mode

For HTML Files

  1. Download encriptor.js from the GitHub repository or NPMJS.
  2. Move encriptor.js into your project directory.
  3. Include EncriptorJS in your HTML file:
   <script src="encriptor.js"></script>
Enter fullscreen mode Exit fullscreen mode

or

   <script src="https://cdn.jsdelivr.net/gh/SH20RAJ/EncriptorJS@main/encriptor.min.js"></script>
Enter fullscreen mode Exit fullscreen mode

Easy Encryption and Decryption

Once EncriptorJS is part of your project, encrypting and decrypting text becomes a breeze.

Encrypting Text

To encrypt text, utilize the encrypt method of the Encriptor object:

const text = 'My confidential message';
const key = 'mySecretKey123';

const encryptedText = Encriptor.encrypt(text, key);
console.log(encryptedText); // Example output: 'Kcdew9zdYidBf'
Enter fullscreen mode Exit fullscreen mode

Decrypting Text

To decrypt the encrypted text, utilize the decrypt method:

const encryptedText = 'Kcdew9zdYidBf'; // Replace with your encrypted text
const key = 'mySecretKey123';
const decryptedText = Encriptor.decrypt(encryptedText, key);
console.log(decryptedText); // Example output: 'My confidential message'
Enter fullscreen mode Exit fullscreen mode

String Shuffling

EncriptorJS also provides a useful utility function shuffleString:

const text = 'Hello, World!';
const shuffledText = Encriptor.shuffleString(text, 42);
console.log(shuffledText); // Outputs a shuffled version of the input string
Enter fullscreen mode Exit fullscreen mode

The shuffleString function shuffles the provided text using a key. Repeating the same key will produce the same shuffled result. Note that this function is useful for shuffling strings but is not designed for strong encryption.

Comparing EncriptorJS with JWT

Here's a comparison between EncriptorJS and JWT (JSON Web Tokens) for text encryption and authentication:

JWT (JSON Web Tokens)

  • Purpose: JWT is specifically designed for securely transmitting information between parties as a JSON object.
  • Usage: Typically used for authentication purposes. When a user logs in, they receive a JWT, which they include in subsequent requests to prove their identity.
  • Security: JWTs are digitally signed, which means they can be verified and trusted.
  • Payload: JWTs have a payload (claims) that can contain user information such as user ID, role, or permissions.
  • Stateless: JWTs are stateless, meaning the server does not need to store the token, which can be beneficial for scalability.

EncriptorJS

  • Purpose: EncriptorJS is designed for encrypting and decrypting text data, not specifically for authentication tokens.
  • Usage: It can be used to encrypt sensitive information such as passwords, messages, or any text data that needs to be stored or transmitted securely.
  • Security: The security of EncriptorJS depends on the strength of the encryption algorithm and the key management.
  • Flexibility: EncriptorJS allows you to have more control over the encryption process compared to JWT, which has a predefined structure and usage pattern.
  • Stateful: Since EncriptorJS encrypts and decrypts data, it is stateful, meaning the server needs to maintain the key securely.

Sample JWT Code

const jwt = require('jsonwebtoken');

// Create a JWT token
const payload = { user_id: 123 };
const secretKey = 'mySecretKey';
const token = jwt.sign(payload, secretKey);

console.log(token); // Example JWT token

// Verify and decode JWT token
const decoded = jwt.verify(token, secretKey);
console.log(decoded); // { user_id: 123 }
Enter fullscreen mode Exit fullscreen mode

Considerations

  • Use Case: JWT is well-suited for authentication and authorization scenarios, while EncriptorJS is better for encrypting sensitive data.
  • Security: Both JWT and EncriptorJS can be secure when implemented correctly. Ensure best practices for key management and encryption algorithms.
  • Complexity: JWT is straightforward for authentication. EncriptorJS requires more manual handling of encryption and decryption.
  • State: JWT is stateless, EncriptorJS is stateful, so key management is crucial.
  • Regeneration: JWTs have an expiration time and can be regenerated easily. EncriptorJS requires key rotation and data re-encryption if keys change.

Examples and More

Explore various examples in the examples directory of the repository to see EncriptorJS in action.

Stay Secure with EncriptorJS

Protect your data with EncriptorJS, a lightweight yet powerful JavaScript encryption library. Whether you're encrypting passwords, sensitive messages, or any other text data, EncriptorJS offers an intuitive solution with an added layer of security.

License

This project is licensed under the MIT License.


See Demo :-

References

Top comments (2)

Some comments may only be visible to logged-in visitors. Sign in to view all comments.