"Go" PHP Android iOS compatible AES encryption algorithm

Source: Internet
Author: User
Tags chr decrypt mcrypt

App Project user password transmission has been no use of HTTPS, considering that the user's privacy is temporarily encrypted with AES, and later can be used for mobile phone and server-side encryption interaction.

PHP free version of the Phpaes project, the phone-side decoding all kinds of wrong.

Finally find the PHP ANDROID IOS, Mutual decryption normal AES encryption algorithm code.

AES encryption algorithm for PHP:

<?PHPclassMCrypt {Private $hex _iv= ' 00000000000000000000000000000000 ';#converted JAVA byte code in to HEX and placed it here    Private $key= ' p4ssw0rd ';#same as in JAVA    function__construct () {$this-Key= Hash (' sha256 ',$this-Key,true); //echo $this->key. ' <br/> ';    }    functionEncrypt$str) {           $TD= Mcrypt_module_open (mcrypt_rijndael_128, ", MCRYPT_MODE_CBC,"); Mcrypt_generic_init ($TD,$this-Key,$this-&GT;HEXTOSTR ($this-Hex_iv)); $block= Mcrypt_get_block_size (mcrypt_rijndael_128,MCRYPT_MODE_CBC); $pad=$block- (strlen($str) %$block); $str.=str_repeat(CHR($pad),$pad); $encrypted= Mcrypt_generic ($TD,$str); Mcrypt_generic_deinit ($TD); Mcrypt_module_close ($TD); return Base64_encode($encrypted); }    functionDecrypt$code) {            $TD= Mcrypt_module_open (mcrypt_rijndael_128, ", MCRYPT_MODE_CBC,"); Mcrypt_generic_init ($TD,$this-Key,$this-&GT;HEXTOSTR ($this-Hex_iv)); $str= Mdecrypt_generic ($TD,Base64_decode($code)); $block= Mcrypt_get_block_size (mcrypt_rijndael_128,MCRYPT_MODE_CBC); Mcrypt_generic_deinit ($TD); Mcrypt_module_close ($TD); return $this->strippadding ($str); }    /*For PKCS7 padding*/    Private functionAddpadding ($string,$blocksize= 16) {        $len=strlen($string); $pad=$blocksize- ($len%$blocksize); $string.=str_repeat(CHR($pad),$pad); return $string; }    Private functionStrippadding ($string) {        $slast=Ord(substr($string,-1)); $SLASTC=CHR($slast); $pcheck=substr($string, -$slast); if(Preg_match("/$SLASTC{" .$slast. "}/",$string)) {            $string=substr($string, 0,strlen($string) -$slast); return $string; } Else {            return false; }    }functionHEXTOSTR ($hex){    $string= ' ';  for($i= 0;$i<strlen($hex)-1;$i+=2)    {        $string.=CHR(Hexdec($hex[$i].$hex[$i+1])); }    return $string;}}$encryption=NewMCrypt ();Echo $encryption->encrypt (' Top Secret message '). "<br/>";Echo $encryption->decrypt (' eqyz+uku+ss+sinf15bapdyj5anrrwy632v8ehrzlpk= ');?>

AES encryption algorithm for Java:

ImportJavax.crypto.Cipher;ImportJavax.crypto.spec.IvParameterSpec;ImportJavax.crypto.spec.SecretKeySpec;Importandroid.util.Base64;/** * @authorVIPIN.CB, [email protected] <br> * Sep, 5:18:34 PM <br> * package:-<b>co M.veebow.util</b> <br> * project:-<b>Veebow</b> * <p>*/ Public classAescrypt {Private FinalCipher Cipher; Private FinalSecretkeyspec Key; PrivateAlgorithmparameterspec spec;  Public Static FinalString seed_16_character = "U1MJU1M0FDOUZ.QZ";  PublicAescrypt ()throwsException {//hash password with SHA-256 and crop the output to 128-bit for keyMessageDigest digest = messagedigest.getinstance ("SHA-256"); Digest.update (Seed_16_character.getbytes ("UTF-8")); byte[] Keybytes =New byte[32]; System.arraycopy (Digest.digest (),0, Keybytes, 0, keybytes.length); Cipher= Cipher.getinstance ("aes/cbc/pkcs7padding"); Key=NewSecretkeyspec (Keybytes, "AES"); Spec=Getiv (); }     PublicAlgorithmparameterspec Getiv () {byte[] IV = {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, };        Ivparameterspec Ivparameterspec; Ivparameterspec=NewIvparameterspec (iv); returnIvparameterspec; }     PublicString Encrypt (string plaintext)throwsException {cipher.init (Cipher.encrypt_mode, key, spec); byte[] encrypted = Cipher.dofinal (Plaintext.getbytes ("UTF-8")); String Encryptedtext=NewString (Base64.encode (encrypted, base64.default),"UTF-8"); returnEncryptedtext; }     PublicString Decrypt (String cryptedtext)throwsException {cipher.init (Cipher.decrypt_mode, key, spec); byte[] bytes =Base64.decode (Cryptedtext, Base64.default); byte[] decrypted =cipher.dofinal (bytes); String Decryptedtext=NewString (decrypted, "UTF-8"); returnDecryptedtext; }}

AES encryption algorithm for iOS:

Https://github.com/Gurpartap/AESCrypt-ObjC

StackOverflow Reference
Http://stackoverflow.com/questions/5928915/wanted-compatible-aes-code-encrypt-decrypt-for-iphone-android-windows-xp

http://stackoverflow.com/questions/19196728/aes-128-encryption-in-java-decryption-in-php

Transferred from: Http://www.funboxpower.com/php_android_ios_aes

"Go" PHP Android iOS compatible AES encryption algorithm

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.