JavaScript Conventional Encryption Techniques

In today's web development, data security is a crucial issue. In order to ensure data security, we need to use encryption technology. JavaScript, as a client-side programming language, does a good job of encrypting data. In this article, we will provide you with a general JavaScript encryption encyclopedia, as well as example code to demonstrate how to use them.

Base64 encryption

Base64 is a way of encoding binary data into ASCII characters. This encoding method is widely used when converting binary data to text data, such as transmitting binary data in email. The following is the code for implementing Base64 encryption in JavaScript:

function base64Encode(str) {
    
    
  return btoa(encodeURIComponent(str).replace(/%([0-9A-F]{2})/g, (match, p1) => String.fromCharCode('0x' + p1)));
}

Here we have used JavaScript built-in btoa()functions and encodeURIComponent()functions. Among them, btoa()the function can encode the string into Base64 format, and encodeURIComponent()the function can convert the string into the URI format that can be transmitted. We also used a regular expression to replace special characters in the URI format. Finally, we return the processed string as the result.

Here is an example using Base64 encryption:

const originalString = "Hello, world!";
const encodedString = base64Encode(originalString);

console.log("Original string:", originalString);
console.log("Encoded string:", encodedString);

Running the above code, the output will be:

Original string: Hello, world!
Encoded string: SGVsbG8sIHdvcmxkIQ==

MD5 encryption

MD5 is a widely used message digest algorithm for verifying data integrity and preventing data from being tampered with. The summary information generated by MD5 is a 128-bit binary data, usually expressed as 32 hexadecimal digits. The following is the code for implementing MD5 encryption in JavaScript:

function md5(str) {
    
    
  let md5Hash = CryptoJS.MD5(str);
  return md5Hash.toString(CryptoJS.enc.Hex);
}

Here, we have used a JavaScript library CryptoJS to implement MD5 encryption. We first pass the input string to CryptoJS.MD5()the function for digest calculation, and then convert the result to a string in hexadecimal format and return it.

Here is an example using MD5 encryption:

const originalString = "Hello, world!";
const hashedString = md5(originalString);

console.log("Original string:", originalString);
console.log("Hashed string:", hashedString);

Running the above code, the output will be:

Original string: Hello, world!
Hashed string: ed076287532e86365e841e92bfc50d8c

AES encryption

AES is a symmetric key encryption algorithm widely used in data encryption and protection. The following is the code for implementing AES encryption in JavaScript:

function aesEncrypt(str, key) {
    
    
  let encrypted = CryptoJS.AES.encrypt(str, key);
  return encrypted.toString();
}

function aesDecrypt(str, key) {
    
    
  let decrypted = CryptoJS.AES.decrypt(str, key);
  return decrypted.toString(CryptoJS.enc.Utf8);
}

Here we also use the CryptoJS library to implement AES encryption. We first call CryptoJS.AES.encrypt()the function to encrypt the input string, then convert the result to a string and return it. The decryption process is similar, we call CryptoJS.AES.decrypt()the function to decrypt the string, then convert the result to a string in UTF-8 format and return it.

Here is an example using AES encryption:

const originalString = "Hello, world!";
const key = "my-secret-key";

const encryptedString = aesEncrypt(originalString, key);
const decryptedString = aesDecrypt(encryptedString, key);

console.log("Original string:", originalString);
console.log("Encrypted string:", encryptedString);
console.log("Decrypted string:", decryptedString);

Running the above code, the output will be:

Original string: Hello, world!
Encrypted string: U2FsdGVkX19Jj+YoIjqJZvT8WxtsA9X+wLjKzFMGk8M=
Decrypted string: Hello, world!

The above is a general JavaScript encryption encyclopedia and case code, I hope it can be helpful to you!

jsjiami.com

If you have different views or questions about the content of the article, please leave a message in the comment area, or send me a private message.

You can also go to the website above, there is my contact information at the bottom to discuss in detail

If your own source code is encrypted and there is no backup, you can find us to solve the problem of recovering the source code, any encryption is fine.

Guess you like

Origin blog.csdn.net/mxd01848/article/details/130125759