PHP android ios相互兼容的AES加密算法

APP项目用户密码传输一直没有用HTTPS,考虑到用户的隐私暂时先用AES对密码加密,以后也可以用于手机端与服务端加密交互。

PHP的免费版phpAES项目,手机端解码各种不对。

好不容易找了PHP ANDROID IOS,相互加解密正常的AES加密算法代码。

PHP的AES加密算法:

 1 <?php
 2 class MCrypt {
 3     private $hex_iv = '00000000000000000000000000000000'; # converted JAVA byte code in to HEX and placed it here           
 4     private $key = 'U1MjU1M0FDOUZ.Qz'; #Same as in JAVA
 5     function __construct() {
 6         $this->key = hash('sha256', $this->key, true);
 7         //echo $this->key.'<br/>';
 8     }
 9     function encrypt($str) {   
10         $td = mcrypt_module_open(MCRYPT_RIJNDAEL_128, '', MCRYPT_MODE_CBC, '');
11         mcrypt_generic_init($td, $this->key, $this->hexToStr($this->hex_iv));
12         $block = mcrypt_get_block_size(MCRYPT_RIJNDAEL_128, MCRYPT_MODE_CBC);
13         $pad = $block - (strlen($str) % $block);
14         $str .= str_repeat(chr($pad), $pad);
15         $encrypted = mcrypt_generic($td, $str);
16         mcrypt_generic_deinit($td);
17         mcrypt_module_close($td);    
18         return base64_encode($encrypted);
19     }
20     function decrypt($code) {    
21         $td = mcrypt_module_open(MCRYPT_RIJNDAEL_128, '', MCRYPT_MODE_CBC, '');
22         mcrypt_generic_init($td, $this->key, $this->hexToStr($this->hex_iv));
23         $str = mdecrypt_generic($td, base64_decode($code));
24         $block = mcrypt_get_block_size(MCRYPT_RIJNDAEL_128, MCRYPT_MODE_CBC);
25         mcrypt_generic_deinit($td);
26         mcrypt_module_close($td);    
27         return $this->strippadding($str);           
28     }
29     /*
30       For PKCS7 padding
31      */
32     private function addpadding($string, $blocksize = 16) {
33         $len = strlen($string);
34         $pad = $blocksize - ($len % $blocksize);
35         $string .= str_repeat(chr($pad), $pad);
36         return $string;
37     }
38     private function strippadding($string) {
39         $slast = ord(substr($string, -1));
40         $slastc = chr($slast);
41         $pcheck = substr($string, -$slast);
42         if (preg_match("/$slastc{" . $slast . "}/", $string)) {
43             $string = substr($string, 0, strlen($string) - $slast);
44             return $string;
45         } else {
46             return false;
47         }
48     }
49 function hexToStr($hex)
50 {
51     $string='';
52     for ($i=0; $i < strlen($hex)-1; $i+=2)
53     {
54         $string .= chr(hexdec($hex[$i].$hex[$i+1]));
55     }
56     return $string;
57 }
58 }
59 $encryption = new MCrypt();
60 echo $encryption->encrypt('123456') . "<br/>";
61 echo $encryption->decrypt('tpyxISJ83dqEs3uw8bN/+w==');
62  
63 ?>

java的AES加密算法:

 1 import javax.crypto.Cipher;
 2 import javax.crypto.spec.IvParameterSpec;
 3 import javax.crypto.spec.SecretKeySpec;
 4 import android.util.Base64;
 5 /**
 6  * @author vipin.cb , vipin.cb@experionglobal.com <br>
 7  *         Sep 27, 2013, 5:18:34 PM <br>
 8  *         Package:- <b>com.veebow.util</b> <br>
 9  *         Project:- <b>Veebow</b>
10  *         <p>
11  */
12 public class AESCrypt {
13     private final Cipher cipher;
14     private final SecretKeySpec key;
15     private AlgorithmParameterSpec spec;
16     public static final String SEED_16_CHARACTER = "U1MjU1M0FDOUZ.Qz";
17     public AESCrypt() throws Exception {
18         // hash password with SHA-256 and crop the output to 128-bit for key
19         MessageDigest digest = MessageDigest.getInstance("SHA-256");
20         digest.update(SEED_16_CHARACTER.getBytes("UTF-8"));
21         byte[] keyBytes = new byte[32];
22         System.arraycopy(digest.digest(), 0, keyBytes, 0, keyBytes.length);
23         cipher = Cipher.getInstance("AES/CBC/PKCS7Padding");
24         key = new SecretKeySpec(keyBytes, "AES");
25         spec = getIV();
26     }
27     public AlgorithmParameterSpec getIV() {
28         byte[] iv = { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, };
29         IvParameterSpec ivParameterSpec;
30         ivParameterSpec = new IvParameterSpec(iv);
31         return ivParameterSpec;
32     }
33     public String encrypt(String plainText) throws Exception {
34         cipher.init(Cipher.ENCRYPT_MODE, key, spec);
35         byte[] encrypted = cipher.doFinal(plainText.getBytes("UTF-8"));
36         String encryptedText = new String(Base64.encode(encrypted,
37                 Base64.DEFAULT), "UTF-8");
38         return encryptedText;
39     }
40     public String decrypt(String cryptedText) throws Exception {
41         cipher.init(Cipher.DECRYPT_MODE, key, spec);
42         byte[] bytes = Base64.decode(cryptedText, Base64.DEFAULT);
43         byte[] decrypted = cipher.doFinal(bytes);
44         String decryptedText = new String(decrypted, "UTF-8");
45         return decryptedText;
46     }
47 }

IOS的AES加密算法:

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

stackoverflow参考
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

原文地址:https://www.cnblogs.com/luojianqun/p/4399117.html