JavaScript realizes the front-end AES encryption and decryption function __java

Source: Internet
Author: User
Tags base64 decrypt pkcs7

JS front-end AES Encryption

Recently, because of the project needs to do a mitm, suddenly even if the use of HTTPS, can not guarantee the security of the data transfer process. Through a man-in-the-middle attack, you can get all the content of the HTTP protocol directly. So I began to try to do some simple encryption, to a certain extent, to ensure security.

AES encrypts the data, so the client and server use the same secret key. (Only as a demo, the official environment is recommended to use RSA)

First to prepare a plaintext password and encryption using the key

var Source = "ABCDEFG"; var aeskey = "8NONwyJtHesysWpM";

JS encryption padding and mode need to correspond to the server, otherwise it cannot decrypt

Key is a string type and needs to be processed to use//Note: Pkcs5padding and pkcs7padding are the same.

The encrypteddata shown in the previous paragraph is the result of the encryption.

JS Decryption Code

EncryptedData for encrypted data,//directly through JS encrypted data is an object, can not be directly decrypted, the need to convert to Base64 string can be decrypted//server end should return Base64 after the encrypted data EncryptedData = EncryptedData.ciphertext.toString (); var encryptedhexstr = CryptoJS.enc.Hex.parse (EncryptedData);

Results Demo

Tip:aes.js N Oneness

Http://react.file.alimmdn.com/aes.js

Demo Console View

http://react.file.alimmdn.com/aes.html?t=1490179790965

"Source code is as follows"

<! 

DOCTYPE html>  



Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.