AES+128+CBC 加密
注釋:128加密算法長度 CBC加密方式
一.iOS寫法如下
1.新建類目文件AES,.h文件如下
//
// NSString+AES.h
// wallet
//
// Created by liang xu on 2019/3/7.
// Copyright ? 2019年 xuliang. All rights reserved.
//
NS_ASSUME_NONNULL_BEGIN
@interface NSString (AES)
/**< 加密方法 */
- (NSString*)aci_encryptWithAES;
/**< 解密方法 */
- (NSString*)aci_decryptWithAES;
@end
NS_ASSUME_NONNULL_END
2..m文件如下
//
// NSString+AES.m
// wallet
//
// Created by liang xu on 2019/3/7.
// Copyright ? 2019年 xuliang. All rights reserved.
//
#import "NSString+AES.h"
#import <CommonCrypto/CommonDigest.h>
#import <CommonCrypto/CommonCryptor.h>
#import "GTMBase64.h"
@implementation NSString (AES)
// key跟后臺(tái)協(xié)商一個(gè)即可晨汹,保持一致
static NSString *const PSW_AES_KEY = @"abcdefghijklmnop";
// 這里的偏移量也需要跟后臺(tái)一致因宇,一般跟key一樣就行
static NSString *const AES_IV_PARAMETER = @"1234567890123456";
-(NSString*)aci_encryptWithAES{
NSData *data = [self dataUsingEncoding:NSUTF8StringEncoding];
NSData *AESData = [self AES128operation:kCCEncrypt
data:data
key:PSW_AES_KEY
iv:AES_IV_PARAMETER];
NSString *baseStr_GTM = [self encodeBase64Data:AESData];
return baseStr_GTM;
}
-(NSString*)aci_decryptWithAES{
NSData *data = [self dataUsingEncoding:NSUTF8StringEncoding];
NSData *baseData_GTM = [self decodeBase64Data:data];
NSData *baseData = [[NSData alloc]initWithBase64EncodedString:self options:0];
NSData *AESData_GTM = [self AES128operation:kCCDecrypt
data:baseData_GTM
key:PSW_AES_KEY
iv:AES_IV_PARAMETER];
NSData *AESData = [self AES128operation:kCCDecrypt
data:baseData
key:PSW_AES_KEY
iv:AES_IV_PARAMETER];
NSString *decStr_GTM = [[NSString alloc] initWithData:AESData_GTM encoding:NSUTF8StringEncoding];
NSLog(@"decStr_GTM : %@",decStr_GTM);
NSString *decStr = [[NSString alloc] initWithData:AESData encoding:NSUTF8StringEncoding];
return decStr;
}
/**
* AES加解密算法
*
* @param operation kCCEncrypt(加密)kCCDecrypt(解密)
* @param data 待操作Data數(shù)據(jù)
* @param key key
* @param iv 向量
*
*
*/
-(NSData *)AES128operation:(CCOperation)operation data:(NSData *)data key:(NSString *)key iv:(NSString *)iv {
char keyPtr[kCCKeySizeAES128 + 1]; //kCCKeySizeAES128是加密位數(shù) 可以替換成256位的
bzero(keyPtr, sizeof(keyPtr));
[key getCString:keyPtr maxLength:sizeof(keyPtr) encoding:NSUTF8StringEncoding];
// IV
char ivPtr[kCCBlockSizeAES128 + 1];
bzero(ivPtr, sizeof(ivPtr));
[iv getCString:ivPtr maxLength:sizeof(ivPtr) encoding:NSUTF8StringEncoding];
size_t bufferSize = [data length] + kCCBlockSizeAES128;
void *buffer = malloc(bufferSize);
size_t numBytesEncrypted = 0;
// 設(shè)置加密參數(shù)
/**
這里設(shè)置的參數(shù)ios默認(rèn)為CBC加密方式,如果需要其他加密方式如ECB济似,在kCCOptionPKCS7Padding這個(gè)參數(shù)后邊加上kCCOptionECBMode猖凛,即kCCOptionPKCS7Padding | kCCOptionECBMode赂蠢,但是記得修改上邊的偏移量,因?yàn)橹挥蠧BC模式有偏移量之說
*/
CCCryptorStatus cryptorStatus = CCCrypt(operation, kCCAlgorithmAES128, kCCOptionPKCS7Padding,
keyPtr, kCCKeySizeAES128,
ivPtr,
[data bytes], [data length],
buffer, bufferSize,
&numBytesEncrypted);
if(cryptorStatus == kCCSuccess) {
NSLog(@"Success");
return [NSData dataWithBytesNoCopy:buffer length:numBytesEncrypted];
} else {
NSLog(@"Error");
}
free(buffer);
return nil;
}
// 這里附上GTMBase64編碼的代碼辨泳,可以手動(dòng)寫一個(gè)分類虱岂,也可以直接cocopods下載,pod 'GTMBase64'菠红。
/**< GTMBase64編碼 */
- (NSString*)encodeBase64Data:(NSData *)data {
data = [GTMBase64 encodeData:data];
NSString *base64String = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];
return base64String;
}
/**< GTMBase64解碼 */
- (NSData*)decodeBase64Data:(NSData *)data {
data = [GTMBase64 decodeData:data];
return data;
}
@end
使用:
NSString *aesdata = [@"中文@yi自.4s5=+" aci_encryptWithAES];
NSLog(@"%@",aesdata);
NSString *enstr = [aesdata aci_decryptWithAES];
NSLog(@"%@",enstr);
二.PHP 7.1以后第岖,廢棄了mcrypt,改用openssl试溯,相對iOS來說蔑滓,簡單很多
新建類
public $key = 'abcdefghijklmnop';//加密key (也可以不是16位,但當(dāng)它大于16位時(shí)遇绞,7.1的openssl函數(shù)會(huì)截取前16位键袱,有點(diǎn)坑)
public $iv = '1234567890123456'; //保證偏移量為16位
public $method = 'aes-128-cbc';//加密方式 #AES-256-CBC等
/**
* 加密數(shù)據(jù)
*
* @param null $data
* @return string
*/
public function aesEn($data)
{
$enData = openssl_encrypt($data, $this->method,$this->key, 0, $this->iv);
return $enData;
}
/**
* 解密數(shù)據(jù)
*
* @param null $data
* @return string
*/
public function aesDe($data)
{
$decrypted = openssl_decrypt($data, $this->method, $this->key, 0, $this->iv);
return $decrypted;
}
希望可以幫到開發(fā)的小伙伴??,加油咯