JavaScript encryption using the pidCrypt library
Base64
var myString = "This is some text";
//encoding:
var b64encoded = pidCryptUtil.encodeBase64(myString);
//decoding:
var b64decoded = pidCryptUtil.decodeBase64(b64encoded);
AES
var aes = new pidCrypt.AES.CBC();
// Шифрует текст
var crypted = aes.encryptText("Шифруем текст", "password", {nBits: 256});
// Расшифрует текст
var decrypted = aes.decryptText(pidCryptUtil.stripLineFeeds(crypted),"password",{nBits:256});
RSA
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 didn’t work with the version of the library that is linked to the “Current version” link and I downloaded all the scripts directly from the places from where they were connected in the examples
Note
I was looking for information about JavaScript encryption for a long time when I was thinking about creating a chat with terminal encryption or a note web service with encryption of the note text on the client side. Actually, I made a chat plugin for personal correspondence of users and now I think whether the message encryption function is needed in it.
Only registered users can participate in the survey. Please come in.
Do I need chat message encryption
- 73.8% Need 48
- 6.1% not needed 4
- 20% I don’t care because I don’t plan to use this chat module in my projects 13