Java,php,js; AES Interoperability Plus Decryption

Source: Internet
Author: User
Tags base64 decrypt vars

1,java End (dependent common-codec jar)

[Java]View PlainCopy
  1. Package Com.jiami;
  2. Import Javax.crypto.Cipher;
  3. Import Javax.crypto.spec.IvParameterSpec;
  4. Import Javax.crypto.spec.SecretKeySpec;
  5. Import org.apache.commons.codec.binary.Base64;
  6. Public class Aesutils {
  7. Private final static String key="1234123412341324";
  8. Private final static String iv="1234123412341234";
  9. /** 
  10. * AES Encryption
  11. * @param data
  12. * @return
  13. */
  14. public static string EncryptData (String data) {
  15. try {
  16. Cipher Cipher = cipher.getinstance ("aes/cbc/nopadding");
  17. int blockSize = Cipher.getblocksize ();
  18. byte[] databytes = Data.getbytes ();
  19. int plaintextlength = databytes.length;
  20. if (plaintextlength% blockSize! = 0) {
  21. Plaintextlength = Plaintextlength + (blockSize-(plaintextlength% blockSize));
  22. }
  23. byte[] plaintext = new byte[plaintextlength];
  24. System.arraycopy (Databytes, 0, plaintext, 0, databytes.length);
  25. Secretkeyspec Keyspec = new Secretkeyspec (Key.getbytes (), "AES");
  26. Ivparameterspec Ivspec = new Ivparameterspec (Iv.getbytes ());
  27. Cipher.init (Cipher.encrypt_mode, Keyspec, Ivspec);
  28. byte[] encrypted = cipher.dofinal (plaintext);
  29. return New String (Base64.encodebase64 (encrypted));
  30. } catch (Exception e) {
  31. E.printstacktrace ();
  32. }
  33. return null;
  34. }
  35. /** 
  36. * AES Decryption
  37. * @param data redaction
  38. * @return
  39. */
  40. public static string Decryptdata (String data) {
  41. try {
  42. byte[] encrypted1 =base64.decodebase64 (Data.getbytes ());
  43. Cipher Cipher = cipher.getinstance ("aes/cbc/nopadding");
  44. Secretkeyspec Keyspec = new Secretkeyspec (Key.getbytes (), "AES");
  45. Ivparameterspec Ivspec = new Ivparameterspec (Iv.getbytes ());
  46. Cipher.init (Cipher.decrypt_mode, Keyspec, Ivspec);
  47. byte[] Original = cipher.dofinal (encrypted1);
  48. String originalstring = new String (original);
  49. return originalstring;
  50. } catch (Exception e) {
  51. E.printstacktrace ();
  52. }
  53. return null;
  54. }
  55. public static void Main (string[] args) {
  56. String data="PHP and Java interoperability!";
  57. String enstr=aesutils.encryptdata (data);
  58. SYSTEM.OUT.PRINTLN ("encryption:" +ENSTR);
  59. String Destr=aesutils.decryptdata (ENSTR);
  60. System.out.println ("decryption:" +DESTR);
  61. }
  62. }


2,php End

[PHP]View PlainCopy
  1. <?php
  2. $privateKey = "1234123412341324";
  3. $iv = "1234123412341324";
  4. $data = "data for testing";
  5. //Encryption
  6. $encrypted = Mcrypt_encrypt (mcrypt_rijndael_128, $privateKey, $data, MCRYPT_MODE_CBC, $iv);
  7. Echo (base64_encode ($encrypted));
  8. echo ' <br/> ';
  9. //Decryption
  10. $encryptedData = Base64_decode (base64_encode ($encrypted));
  11. $decrypted = Mcrypt_decrypt (mcrypt_rijndael_128, $privateKey, $encryptedData, MCRYPT_MODE_CBC, $iv);
  12. Echo ($decrypted);
  13. ?>


3,js End

[JavaScript]View PlainCopy
  1. <script src="./crypto-js.js" ></script>
  2. <script src="./aes.js" ></script>
  3. <script src="./pad-zeropadding.js" ></script>
  4. <script>
  5. var data = "Test";
  6. var key = CryptoJS.enc.Latin1.parse (' 1234123412341324 ');
  7. var IV = CryptoJS.enc.Latin1.parse (' 1234123412341324 ');
  8. Encryption
  9. var encrypted = CryptoJS.AES.encrypt (data,key,{iv:iv,mode:cryptojs.mode.cbc,padding:cryptojs.pad.zeropadding});
  10. alert (encrypted);
  11. Console.log (Encrypted.tostring ());
  12. Decrypt
  13. var decrypted = CryptoJS.AES.decrypt (encrypted,key,{iv:iv,padding:cryptojs.pad.zeropadding});
  14. Console.log (decrypted.tostring (CryptoJS.enc.Utf8));
  15. Alert (decrypted.tostring (CryptoJS.enc.Utf8));
  16. Lt;/script>



Three JS files: http://download.csdn.net/detail/wd4871/9526147

Java,php,js; AES Interoperability Plus decryption

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.