app加密算法
php端
<?php
namespace app\controllers;
use yii\web\Controller;
class TestController extends Controller
{
public function actionIndex(){
// return $this->get_real_ip();
$time=time().'000';
$ip=$this->get_real_ip();
return $this->aes128Encrypt('e234242423',"timestamp=".$time."&ip=".$ip);
$sign=bin2hex($this->aes128Encrypt('跟软件后台的密钥字符相同',"timestamp=".$time."&ip=".$ip));
return $sign;
}
public function aes128Encrypt($key, $data) {
$padding = 16 - (strlen($data) % 16);
$data .= str_repeat(chr($padding), $padding);
$keySize = 16;
$ivSize = 16;
$rawKey = $key;
$genKeyData = '';
return md5('sdfsfs',true);
return md5( $genKeyData.$rawKey, true );
do
{
$genKeyData = $genKeyData.md5( $genKeyData.$rawKey, true );
} while( strlen( $genKeyData ) < ($keySize + $ivSize) );
return $genKeyData;
$generatedKey = substr( $genKeyData, 0, $keySize );
$generatedIV = substr( $genKeyData, $keySize, $ivSize );
return mcrypt_encrypt(MCRYPT_RIJNDAEL_128, $generatedKey, $data, MCRYPT_MODE_CBC, $generatedIV);
}
public function get_real_ip(){
$ip=false;
if(!empty($_SERVER["HTTP_CLIENT_IP"])){
$ip = $_SERVER["HTTP_CLIENT_IP"];
}
if (!empty($_SERVER['HTTP_X_FORWARDED_FOR'])) {
$ips = explode (", ", $_SERVER['HTTP_X_FORWARDED_FOR']);
if ($ip) { array_unshift($ips, $ip); $ip = FALSE; }
for ($i = 0; $i < count($ips); $i++) {
if (!eregi ("^(10|172\.16|192\.168)\.", $ips[$i])) {
$ip = $ips[$i];
break;
}
}
}
return ($ip ? $ip : $_SERVER['REMOTE_ADDR']);
}
}
ios端
int padding = 16 - value.length%16;
// 从指定的ASCII返回值
NSString * asciiValue = [NSData strChr:padding];
NSMutableString * paddingString = [NSMutableString string];
for (int i=0; i<padding; i++) {
[paddingString appendString:asciiValue];
}
// 加密的数据加padding
NSString * dataString = [NSString stringWithFormat:@"%@%@",value,paddingString];
NSLog(@"%@",dataString);
int keySize = 16;
int ivSize = 16;
NSString * rawKey = key;
NSMutableString * genKeyData = [NSMutableString string];
do {
NSString *md5Str = [NSString stringWithFormat:@"%@%@",genKeyData,rawKey];
// md5 16位 加密
NSString *newMD5Str = [NSData createMD5:md5Str];
[genKeyData appendString:newMD5Str];
} while (genKeyData.length < keySize + ivSize);
// 最总加密的key和向量
NSString *generatedKey = [genKeyData substringWithRange:NSMakeRange(0, keySize)];
NSString *generatedIV = [genKeyData substringWithRange:NSMakeRange(keySize, ivSize)];
// 加密
size_t bufferSize = [dataValue length] + kCCBlockSizeAES128;
void *buffer = malloc(bufferSize);
size_t numBytesEncrypted = 0;
CCCryptorStatus cryptorStatus = CCCrypt(kCCEncrypt, kCCAlgorithmAES128, kCCOptionPKCS7Padding,
(__bridge const void *)(generatedKey), kCCKeySizeAES128,
(__bridge const void *)(generatedIV),
[dataValue bytes], [dataValue length],
buffer, bufferSize,
&numBytesEncrypted);
if(cryptorStatus == kCCSuccess){
NSLog(@"Success");
return [NSData dataWithBytesNoCopy:buffer length:numBytesEncrypted];
}else{
NSLog(@"Error");
}
free(buffer);
return nil;