JavaScript encryption using the pidCrypt library

    pidCrypt is an open source cryptographic algorithm library. 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 precisely 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 compact presentation, but also in relative illegibility for trying to find out the nature of the data by a random observer.
    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 contest. This algorithm is well analyzed and is now widely used, as was the case with its predecessor DES. The National Institute of Standards and Technology (NIST) published the AES specification on November 26, 2001, after a five-year period during which 15 candidates were created and evaluated. May 26, 2002 AES was announced as 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 factorization of large integers. The RSA cryptosystem was the first system suitable for both encryption and digital signature.
    Unfortunately, the library lacks the function of 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 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

    Also popular now: