SoFunction
Updated on 2025-03-01

Detailed explanation of JavaScript implementation of encryption and decryption

At present, native JS does not seem to provide MD5 calculation-related function methods, and can only be implemented by yourself or written by a master of the predecessor.

1. Use the library to encrypt

GitHub/brix/crypto-jsDownload the js on it.

It can introduce js of the required encryption method alone; it can also introduce a file, which is equivalent to introducing all encryption methods.

usage:

<script type="text/javascript" src="path-to/bower_components/crypto-js/"></script>
<script type="text/javascript">
    var encrypted = (...);
    var encrypted = CryptoJS.SHA256(...);
</script>

1. Give an example:

Hash encryption:

//Encryption instance one  ("Encryption result 1 MD5:"+CryptoJS.MD5("Hello")); 
  
//Encryption instance 2var pwd="passwor";
("Encryption result 2 Hmac-MD5: "+CryptoJS.HmacMD5("Hello",pwd));

AES symmetric encryption:

Normal text encryption

// Encryptionvar ciphertext = ('my message', 'secret key 123').toString();

// Decryptvar bytes  = (ciphertext, 'secret key 123');
var originalText = (.Utf8);

(originalText); // 'my message'

Object encryption

var data = [{id: 1}, {id: 2}]

// Encryptionvar ciphertext = ((data), 'secret key 123').toString();

// Decryptvar bytes  = (ciphertext, 'secret key 123');
var decryptedData = ((.Utf8));

(decryptedData); // [{id: 1}, {id: 2}]

Custom Key and IV

var key = .('AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA'); //Keyvar iv  = .('1234567812345678');
// Encryptionvar encrypted = ("Message", key, { iv: iv });

// Decryptvar decrypted =(encrypted,key,
        {
            iv:iv,
            mode:,
            padding:.Pkcs7
        });
var originalText = (.Utf8); 

Block Modes and Padding

var key  = .('AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA');  //Keyvar iv   = .('1234567812345678');
var encrypted =("Message", ,key,
        {
            iv:iv,
            mode:,
            padding:.Pkcs7
        }); //The return isbase64ciphertext in format

2、API

See: /docs/

Coding tools:

var words = .("SGVsbG8sIFdvcmxkIQ==");
​var base64 = .(words);
​
var words = ("48656c6c6f2c20576f726c6421");
​var hex = (words);
​
var words = .("");
​var utf8 = .(words);

2. Encryption using JavaScript-MD5 library

GitHub/blueimp/JavaScript-MD5Download the js on it.

For example:

Calculate the (hex-encoded) MD5 hash of a given string value:

var hash = md5('value') // "2063c1608d6e0baf80249c42e2be5804"
var v1= md5('{"name":"Hehehehehehehehehehehehehehehehehehehehehehehehe","age":22}'); //56b21847ed32d2d96cf74077b22342eb

Calculate the (hex-encoded) HMAC-MD5 hash of a given string value and key:

var hash = md5('value', 'key') // "01433efd5f16327ea4b31144572c67f6"

Calculate the raw MD5 hash of a given string value:

var hash = md5('value', null, true)

Calculate the raw HMAC-MD5 hash of a given string value and key:

var hash = md5('value', 'key', true)

This is all about this article about the detailed explanation of JavaScript implementation of encryption and decryption. I hope it will be helpful to everyone's learning and I hope everyone will support me more.