next-crypt v1.1.0
next-crypt
Read in Spanish/Leer en Español
With next-crypt you can encrypt or decrypt messages with RSA encryption very easy.
Installation with NPM
If you are using NodeJS, you can install next-crypt with npm.
$ npm i next-crypt
Then you can include it in your code:
const nextCrypt = require('next-crypt');
or in TypeScript:
import * as nextCrypt from 'next-crypt';
Web Installation
If you want to add next-crypt to your web page, can do it using this code:
<!-- Original -->
<script src="https://nextgendl.github.io/next-crypt/next-crypt.js"></script>
<!-- Minified -->
<script src="https://nextgendl.github.io/next-crypt/next-crypt.min.js"></script>
Usage
Parse to bigInteger
If you want to parse to bigInteger type, can use this function.
const nextCrypt = require('next-crypt');
// This return the bigInteger object for the zero.
nextCrypt.toBigInteger('0');
Generate a random prime number
Generate a random prime number with a specific bits length.
Important You must be careful with the size of the prime number. The bad use can slow down your computer or server. It is preferable use it with automatically size detection. See Encrypt a message with keys generated automatically.
const nextCrypt = require('next-crypt');
// Default is 256 bits
nextCrypt.generatePrime();
// Generate a prime number of 512 bits
nextCrypt.generatePrime(512);
Generate RSA keys
Generate the RSA keys for encrypt a message and then decrypt it.
Important You can only decrypt messages if have the private and public key.
const nextCrypt = require('next-crypt');
// Default is 256 bits.
nextCrypt.generateKeys();
// Generate keys of 512 bits.
nextCrypt.generateKeys(512);
/*
return: {
constant: '65537',
public: string,
private: string
}
*/
Calculate the message bits size
If you do not know how much bits are necessaries to encrypt a message, can use this function.
const nextCrypt = require('next-crypt');
// This return the bits size (number)
nextCrypt.calculateBitsSize('Hello world!');
Encrypt a message using a public key
If you have a public key or you generate one with Generate RSA keys, you can use this function for encrypt an message.
Important If the public key bits size is lower than the message bits required, the message encrypted will result bad. See Calculate the bits size and Encrypt a message with keys generated automatically.
const nextCrypt = require('next-crypt');
let keys = nextCrypt.generateKeys();
// This return the message encrypted in string
nextCrypt.encrypt('hello, world!', keys.public);
Encrypt a message with keys generated automatically
If you do not know the necessary bits size to encrypt a message and you do not have a public key, can use this function. It get the bit sizes required to encrypt automatically.
const nextCrypt = require('next-crypt');
// This return the message encrypted in string format and the public and private key for decrypt
nextCrypt.encrypt('hello, world!');
/*
return: {
message: string,
private_key: string
public_key: string,
}
*/
Decrypt a message
Decrypt a message encrypted with its private and public key or the message encrypted returned in Encrypt a message with keys generated automatically.
const nextCrypt = require('next-crypt');
// This return 'hello, world!'
nextCrypt.decrypt(
// Message encrypted
'10822687603567848147816614000017764111753413728165991223378507724570364542',
// Private key
'7897771323319488987193887130981059586410042071900727978689462220194975963',
// Public key
'17259248710928469956275555807969646121698938289079986145359306080779006829'
);
// This return 'hello, world'
nextCrypt.decrypt(nextCrypt.encrypt('hello, world!'));
Testing
If you had clone this repository, can test it with JavaScript using npm test
or TypeScript using npm run test-ts
.
Contributors
License
This project is licensed under the MIT License. See LICENSE file for details.