📜 ⬆️ ⬇️

JavaScript encryption using the pidCrypt library

pidCrypt is an open source library of cryptographic algorithms. Supports RSA and AES encryption / decryption, and calculation of MD5 and SHA hashes.

Base64


Base64
Base64 encoding can be useful if the HTTP environment uses information whose length can be accurately determined. Also, many applications need to encode binary data for ease of inclusion in URLs, hidden form fields, and here Base64 is convenient not only for a compact view, but also relative unreadable for an attempt by a casual observer to find out the nature of the data.
An example of using pidCrypt to work with Base64 ( demo )
var myString = "This is some text"; //encoding: var b64encoded = pidCryptUtil.encodeBase64(myString); //decoding: var b64decoded = pidCryptUtil.decodeBase64(b64encoded); 


AES


Advanced Encryption Standard (AES)
Advanced Encryption Standard (AES) is a symmetric block encryption algorithm (block size 128 bits, key 128/192/256 bits), adopted as the encryption standard by the US government according to the results of the AES competition. This algorithm is well analyzed and is now widely used, as it was with its predecessor DES. The US National Institute of Standards and Technology (Eng. National Institute of Standards and Technology, NIST) published the AES specification on November 26, 2001 after a five-year period during which 15 nominations were created and evaluated. On May 26, 2002, AES was declared an encryption standard. As of 2009, AES is one of the most common symmetric encryption algorithms.
AES symmetric encryption example ( demo )
 var aes = new pidCrypt.AES.CBC(); //   var crypted = aes.encryptText(" ", "password", {nBits: 256}); //   var decrypted = aes.decryptText(pidCryptUtil.stripLineFeeds(crypted),"password",{nBits:256}); 


RSA


RSA - public key cryptographic algorithm
RSA is a public-key cryptographic algorithm based on the computational complexity of the problem of factoring large integers. RSA cryptosystem became the first system, suitable for both encryption and digital signature.
Unfortunately, the library does not have a function for creating public and private keys. Therefore, they must be generated by some other means.
')
Encryption ( demo )
 //new instance var rsa = new pidCrypt.RSA(); //get the modulus and exponent from certificate (ASN1 parsing) //pem(Array of Bytes) var asn = pidCrypt.ASN1.decode(pem); var tree = asn.toHexTree(); //setting the public key for encryption with retrieved ASN.1 tree rsa.setPublicKeyFromASN(tree); //encrypt the plaintext and returns the encrypted text var crypted = rsa.encrypt(); 


Decoding
 //new instance var rsa = new pidCrypt.RSA(); //get the parameters from certificate (ASN1 parsing) //pem(Array of Bytes) var asn = pidCrypt.ASN1.decode(pem); var tree = asn.toHexTree(); //setting the private key for decryption with retrieved ASN.1 tree rsa.setPrivateKeyFromASN(tree); //decrypt the crypted text and returns the plaintext var plain = rsa.decrypt(); 


For some reason, my examples did not work with the version of the library that was linked to the “Current version” link and I downloaded all the scripts directly from the places from which they were connected in the examples

Note


I was looking for information about encryption in JavaScript for a long time when I was thinking about creating a chat with terminal encryption or a web service of notes with encryption of the text of notes on the client side. Actually I did the chat plugin for personal correspondence of users and now I think whether it needs a message encryption function.

Source: https://habr.com/ru/post/272507/


All Articles