当前位置: 首页 > 后端技术 > PHP

【转载】PHPandroidios相互兼容的AES加密算法

时间:2023-03-30 00:44:40 PHP

APP项目用户密码传输没有使用HTTPS,考虑到用户的隐私,先用AES先加密密码,也可以用于用户之间的加密交互未来手机和服务器。PHP的免费版phpAES项目,在手机上解码出现各种错误。终于找到了PHPANDROIDIOS,相互加解密正常的AES加密算法代码。PHP的AES加密算法:key=hash('sha256',$this->key,true);//echo$this->key.'
';}functionencrypt($str){$td=mcrypt_module_open(MCRYPT_RIJNDAEL_128,'',MCRYPT_MODE_CBC,'');mcrypt_generic_init($td,$this->key,$this->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);返回base64_encode($encrypted);}函数解密($code){$td=mcrypt_module_open(MCRYPT_RIJNDAEL_128,'',MCRYPT_MODE_CBC,'');mcrypt_generic_init($td,$this->key,$this->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);返回$this->strippadding($str);}/*对于PKCS7填充*/privatefunctionaddpadding($string,$blocksize=16){$len=strlen($string);$pad=$blocksize-($len%$blocksize);$string.=str_repeat(chr($pad),$pad);返回$字符串;}privatefunctionstrippadding($string){$slast=ord(substr($string,-1));$slastc=chr($slast);$pcheck=substr($string,-$slast);如果(preg_match("/$slastc{".$slast."}/",$string)){$string=substr($string,0,strlen($string)-$slast);返回$字符串;}别的{返回假;}}函数hexToStr($hex){$string='';对于($i=0;$iencrypt('123456')。"
";echo$encryption->decrypt('tpyxISJ83dqEs3uw8bN/+w==');?>java的AES加密算法:importjavax.crypto.Cipher;importjavax.crypto.spec.IvParameterSpec;importjavax.crypto.spec.SecretKeySpec;importandroid.util.Base64;/***@authorvipin.cb,vipin.cb@experionglobal.com
*2013年9月27日,下午5:18:34
*包:-com.veebow.util
*项目:-Veebow*

*/publicclassAESCrypt{privatefinalCipher密码;私有最终SecretKeySpec密钥;私有AlgorithmParameterSpec规范;publicstaticfinalStringSEED_16_CHARACTER="U1MjU1M0FDOUZ.Qz";公共AESCrypt()throwsException{//使用SHA-256散列密码并将输出裁剪为128位密钥MessageDigestdigest=MessageDigest.getInstance("SHA-256");digest.update(SEED_16_CHARACTER.getBytes("UTF-8"));byte[]keyBytes=newbyte[32];System.arraycopy(digest.digest(),0,keyBytes,0,keyBytes.length);cipher=Cipher.getInstance("AES/CBC/PKCS7Padding");key=newSecretKeySpec(keyBytes,"AES");spec=getIV();}publicAlgorithmParameterSpecgetIV(){byte[]iv={0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,};IvParameterSpecivParameterSpec;ivParameterSpec=newIvParameterSpec(iv);返回ivParameterSpec;}publicStringencrypt(StringplainText)throwsException{cipher.init(Cipher.ENCRYPT_MODE,key,spec);byte[]encrypted=cipher.doFinal(plainText.getBytes("UTF-8"));StringencryptedText=newString(Base64.encode(e加密,Base64.DEFAULT),"UTF-8");返回加密文本;}publicStringdecrypt(StringcryptedText)throwsException{cipher.init(Cipher.DECRYPT_MODE,key,spec);byte[]bytes=Base64.decode(cryptedText,Base64.DEFAULT);byte[]decrypted=cipher.doFinal(bytes);StringdecryptedText=newString(解密,"UTF-8");返回解密文本;}}IOS的AES加密算法:https://github.com/Gurpartap/...stackoverflow参考http://stackoverflow.com/ques...http://stackoverflow.com/ques...