AES encryption and decryption implementations of PHP, Java, net, and JavaScript

Source: Internet
Author: User
# PHP Articles

 
  ';    Echo (Base64_encode ($encrypted));    Echo '
'; Decryption $encryptedData = Base64_decode ("2fbww9+8vpid2/foafzq6q=="); $decrypted = Mcrypt_decrypt (mcrypt_rijndael_128, $privateKey, $encryptedData, MCRYPT_MODE_CBC, $iv); Echo ($decrypted);? >

#Javascript篇

            

# Java Chapter

Import Javax.crypto.cipher;import Javax.crypto.spec.ivparameterspec;import Javax.crypto.spec.secretkeyspec;import    Org.junit.Test;        @Test public void Testcrosslanguageencrypt () throws exception{System.out.println (Encrypt ());    System.out.println (Desencrypt ());            public static string Encrypt () throws Exception {try {string data = "Test String";            String key = "1234567812345678";            String IV = "1234567812345678";            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 Sun.misc.BASE64Encoder (). Encode (encrypted);            } catch (Exception e) {e.printstacktrace ();        return null; }} public static string Desencrypt () throws Exception {try {String data = "2fbww9+8vpid2            /foafzq6q== ";            String key = "1234567812345678";            String IV = "1234567812345678";            byte[] encrypted1 = new Sun.misc.BASE64Decoder (). Decodebuffer (data);            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; }    }

#. NET article

Using system;using system.collections.generic;using system.linq;using system.text;using            System.security.cryptography;namespace test{class Class1 {static void Main (string[] args) {            Console.WriteLine ("I am comming");            String Source = "Test String";            String EncryptData = class1.encrypt (source, "1234567812345678", "1234567812345678");            Console.WriteLine ("=1==");            Console.WriteLine (EncryptData);            Console.WriteLine ("=2==");            String decryptdata = Class1.decrypt ("2fbww9+8vpid2/foafzq6q==", "1234567812345678", "1234567812345678");            Console.WriteLine (Decryptdata);            Console.WriteLine ("=3==");        Console.WriteLine ("I'll go Out"); public static string Encrypt (string toencrypt, String key, String iv) {byte[] Keyarray = utf8e Ncoding. UTF8.            GetBytes (key);            byte[] Ivarray = UTF8Encoding.UTF8.GetBytes (iv); byte[] Toencryptarray = UTF8Encoding.UTF8.GetBytes (Toencrypt);            RijndaelManaged Rdel = new RijndaelManaged ();            Rdel.key = Keyarray;            RDEL.IV = Ivarray;            Rdel.mode = CIPHERMODE.CBC;            rdel.padding = Paddingmode.zeros;            ICryptoTransform ctransform = Rdel.createencryptor ();            byte[] Resultarray = Ctransform.transformfinalblock (toencryptarray, 0, toencryptarray.length);        Return convert.tobase64string (resultarray, 0, resultarray.length); public static string Decrypt (string todecrypt, String key, String iv) {byte[] Keyarray = utf8e Ncoding. UTF8.            GetBytes (key);            byte[] Ivarray = UTF8Encoding.UTF8.GetBytes (iv);            byte[] Toencryptarray = convert.frombase64string (Todecrypt);            RijndaelManaged Rdel = new RijndaelManaged ();            Rdel.key = Keyarray;            RDEL.IV = Ivarray;            Rdel.mode = CIPHERMODE.CBC;            rdel.padding = Paddingmode.zeros; IcrypTotransform ctransform = Rdel.createdecryptor ();            byte[] Resultarray = Ctransform.transformfinalblock (toencryptarray, 0, toencryptarray.length);        Return UTF8Encoding.UTF8.GetString (resultarray); }    }}

The requirements for cross-language plus decryption are: aes/cbc/zeropadding 128-bit mode, key and IV, encoding unified with Utf-8. If you do not support zeropadding, use nopadding.

The above describes the PHP, Java, NET and JavaScript AES encryption and decryption implementation, including the aspects of the content, I hope the PHP tutorial interested in a friend helpful.

  • 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.