1,java End (dependent common-codec jar)
[Java]View PlainCopy
- Package Com.jiami;
- Import Javax.crypto.Cipher;
- Import Javax.crypto.spec.IvParameterSpec;
- Import Javax.crypto.spec.SecretKeySpec;
- Import org.apache.commons.codec.binary.Base64;
- Public class Aesutils {
- Private final static String key="1234123412341324";
- Private final static String iv="1234123412341234";
- /**
- * AES Encryption
- * @param data
- * @return
- */
- public static string EncryptData (String data) {
- try {
- Cipher Cipher = cipher.getinstance ("aes/cbc/nopadding");
- int blockSize = Cipher.getblocksize ();
- byte[] databytes = Data.getbytes ();
- int plaintextlength = databytes.length;
- if (plaintextlength% blockSize! = 0) {
- Plaintextlength = Plaintextlength + (blockSize-(plaintextlength% blockSize));
- }
- byte[] plaintext = new byte[plaintextlength];
- System.arraycopy (Databytes, 0, plaintext, 0, databytes.length);
- Secretkeyspec Keyspec = new Secretkeyspec (Key.getbytes (), "AES");
- Ivparameterspec Ivspec = new Ivparameterspec (Iv.getbytes ());
- Cipher.init (Cipher.encrypt_mode, Keyspec, Ivspec);
- byte[] encrypted = cipher.dofinal (plaintext);
- return New String (Base64.encodebase64 (encrypted));
- } catch (Exception e) {
- E.printstacktrace ();
- }
- return null;
- }
- /**
- * AES Decryption
- * @param data redaction
- * @return
- */
- public static string Decryptdata (String data) {
- try {
- byte[] encrypted1 =base64.decodebase64 (Data.getbytes ());
- Cipher Cipher = cipher.getinstance ("aes/cbc/nopadding");
- Secretkeyspec Keyspec = new Secretkeyspec (Key.getbytes (), "AES");
- Ivparameterspec Ivspec = new Ivparameterspec (Iv.getbytes ());
- Cipher.init (Cipher.decrypt_mode, Keyspec, Ivspec);
- byte[] Original = cipher.dofinal (encrypted1);
- String originalstring = new String (original);
- return originalstring;
- } catch (Exception e) {
- E.printstacktrace ();
- }
- return null;
- }
- public static void Main (string[] args) {
- String data="PHP and Java interoperability!";
- String enstr=aesutils.encryptdata (data);
- SYSTEM.OUT.PRINTLN ("encryption:" +ENSTR);
- String Destr=aesutils.decryptdata (ENSTR);
- System.out.println ("decryption:" +DESTR);
- }
- }
2,php End
[PHP]View PlainCopy
- <?php
- $privateKey = "1234123412341324";
- $iv = "1234123412341324";
- $data = "data for testing";
- //Encryption
- $encrypted = Mcrypt_encrypt (mcrypt_rijndael_128, $privateKey, $data, MCRYPT_MODE_CBC, $iv);
- Echo (base64_encode ($encrypted));
- echo ' <br/> ';
- //Decryption
- $encryptedData = Base64_decode (base64_encode ($encrypted));
- $decrypted = Mcrypt_decrypt (mcrypt_rijndael_128, $privateKey, $encryptedData, MCRYPT_MODE_CBC, $iv);
- Echo ($decrypted);
- ?>
3,js End
[JavaScript]View PlainCopy
- <script src="./crypto-js.js" ></script>
- <script src="./aes.js" ></script>
- <script src="./pad-zeropadding.js" ></script>
- <script>
- var data = "Test";
- var key = CryptoJS.enc.Latin1.parse (' 1234123412341324 ');
- var IV = CryptoJS.enc.Latin1.parse (' 1234123412341324 ');
- Encryption
- var encrypted = CryptoJS.AES.encrypt (data,key,{iv:iv,mode:cryptojs.mode.cbc,padding:cryptojs.pad.zeropadding});
- alert (encrypted);
- Console.log (Encrypted.tostring ());
- Decrypt
- var decrypted = CryptoJS.AES.decrypt (encrypted,key,{iv:iv,padding:cryptojs.pad.zeropadding});
- Console.log (decrypted.tostring (CryptoJS.enc.Utf8));
- Alert (decrypted.tostring (CryptoJS.enc.Utf8));
- Lt;/script>
Three JS files: http://download.csdn.net/detail/wd4871/9526147
Java,php,js; AES Interoperability Plus decryption