EncriptorJS: Secure Text Encryption and Decryption in JavaScript
Tags: javascript, encriptorjs
Published: 2024-04-06 02:28:19


### Introducing EncriptorJS: Secure Text Encryption and Decryption in JavaScript

{% github https://github.com/SH20RAJ/EncriptorJS/ %}

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](https://github.com/sh20raj/EncriptorJS).
2. Move `encriptor.js` into your project directory.
3. Import EncriptorJS in your JavaScript file:
   
   ```javascript
   import Encriptor from './encriptor.module.js';
   ```

#### Option 2: NPM Installation
Alternatively, if you're using npm:
   
   ```bash
   npm i encriptorjs
   ```

   Then, import EncriptorJS in your JavaScript file:
   
   ```javascript
   import Encriptor from 'encriptorjs';
   ```

### For HTML Files

1. Download `encriptor.js` from the [GitHub repository](https://github.com/sh20raj/EncriptorJS) or [NPMJS](https://www.npmjs.com/package/encriptorjs).
2. Move `encriptor.js` into your project directory.
3. Include EncriptorJS in your HTML file:

   ```html
   <script src="encriptor.js"></script>
   ```
   or
   ```html
   <script src="https://cdn.jsdelivr.net/gh/SH20RAJ/EncriptorJS@main/encriptor.min.js"></script>
   ```

## 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:

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

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

### Decrypting Text

To decrypt the encrypted text, utilize the `decrypt` method:

```javascript
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'
```

### String Shuffling

EncriptorJS also provides a useful utility function `shuffleString`:

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

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

```javascript
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 }
```

### 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](https://replit.com/@SH20RAJ/EncriptorJS) 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](./LICENSE).

---

See Demo :-
{% codesandbox encriptor-demo-jh9c5x %}

### References

- [EncriptorJS GitHub Repository](https://github.com/sh20raj/EncriptorJS)
- [npm: EncriptorJS](https://www.npmjs.com/package/encriptorjs)
- [Creating a Custom JavaScript Function to Shuffle Strings with a Key Parameter](https://dev.to/sh20raj/creating-a-custom-javascript-function-to-shuffle-strings-with-a-key-parameter-3c8a)