JS implementation of AES encryption and interoperability with PHP method analysis

Source: Internet
Author: User
Tags decrypt

Tag:asc   creat   str    Analysis    target    ati   decode    security     decoding    

<script type= "Text/javascript" src= "/cryptojs/aes.js" ></SCRIPT>
<script type= "text/ JavaScript "src="/cryptojs/pad-zeropadding.js "></SCRIPT>
<script type=" Text/javascript ";
var data= "test";//cryptographic string
var key = CryptoJS.enc.Latin1.parse (' @12345678912345! '); /Key
var IV = CryptoJS.enc.Latin1.parse (' @12345678912345! '); /keep consistent with key
//encryption
var data = json.stringify (data);//Convert the object to a JSON string
var encrypted = CryptoJS.AES.encrypt (data, Key,{iv:iv,mode:cryptojs.mode.cbc,padding:cryptojs.pad.zeropadding});
Encrypted=encodeuricomponent (encrypted);
document.write (decrypted);//Output encrypted string
//Decrypt
var data= "encrypted string";
Key and IV are consistent with the encryption
var decrypted = CryptoJS.AES.decrypt (data,key,{iv:iv,padding:cryptojs.pad.zeropadding});
Decrypted=decrypted.tostring (CryptoJS.enc.Utf8);
document.write (decrypted);//output decrypted data
</script>

Note: In the actual use of the time and PHP transmission, JS encrypted string inside the browser parsed into a blank space and then PHP decryption error; Here you can do further processing of the encrypted string encrypted=encodeuricomponent ( encrypted); That's not the problem.

PHP encryption and decryption method:

$privateKey = "@12345678912345!";
$iv = "@12345678912345!";
Encryption
$encrypted =mcrypt_encrypt (mcrypt_rijndael_128, $privateKey, $data, MCRYPT_MODE_CBC, $IV);
echo Base64_encode ($encrypted);
Decrypt
$encryptedData =base64_decode ($data);
$decrypted =mcrypt_decrypt (mcrypt_rijndael_128, $privateKey, $encryptedData, MCRYPT_MODE_CBC, $IV);
$decrypted =rtrim ($decrypted, "");//Note! Six red dots will appear behind the decrypted data, which can be processed without affecting further data manipulation
return $decrypted;

PS: About encryption and decryption of interested friends can also refer to the site online tools:

BASE64 Encoding and decoding tool:
Http://tools.jb51.net/transcoding/base64
Password Security online detection:
Http://tools.jb51.net/password/my_password_safe
High Strength Password Generator:
Http://tools.jb51.net/password/CreateStrongPassword
MD5 Online Encryption Tool:
Http://tools.jb51.net/password/CreateMD5Password
Online hashing/hashing algorithm encryption tool:
Http://tools.jb51.net/password/hash_encrypt
Online md5/hash/sha-1/sha-2/sha-256/sha-512/sha-3/ripemd-160 Encryption Tool:
Http://tools.jb51.net/password/hash_md5_sha
Online sha1/sha224/sha256/sha384/sha512 Encryption Tool:
Http://tools.jb51.net/password/sha_encode

JS implementation of AES encryption and interoperability with PHP method analysis

Related Article

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.