App encryption algorithm

Source: Internet
Author: User

PHP side

<?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 (' same as key characters in software background ', "timestamp=". $time. " &ip= ". $ip));
return $sign;
}

Public Function Aes128encrypt ($key, $data) {
$padding =-(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 side

NSData *datavalue = [NSData datawithbytes:[value cstringusingencoding:nsutf8stringencoding] length:value.length];

int padding = 16-value.length%16;
Returns the value from the specified ASCII
NSString * Asciivalue = [NSData strchr:padding];

nsmutablestring * paddingstring = [nsmutablestring string];

for (int i=0; i<padding; i++) {
[Paddingstring Appendstring:asciivalue];
}

Encrypted data plus 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-bit encryption
NSString *newmd5str = [NSData createmd5:md5str];

[Genkeydata APPENDSTRING:NEWMD5STR];

} while (Genkeydata.length < keySize + ivsize);

The most total encrypted keys and vectors
NSString *generatedkey = [Genkeydata substringwithrange:nsmakerange (0, keySize)];
NSString *generatediv = [Genkeydata substringwithrange:nsmakerange (KeySize, ivsize)];

Encryption
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;

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