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跟后台协商一个即可,保持一致
static NSString *const PSW_AES_KEY = @"abcdefghijklmnop";
// 这里的偏移量也需要跟后台一致,一般跟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数据
* @param key key
* @param iv 向量
*
*
*/
-(NSData *)AES128operation:(CCOperation)operation data:(NSData *)data key:(NSString *)key iv:(NSString *)iv {
char keyPtr[kCCKeySizeAES128 + 1]; //kCCKeySizeAES128是加密位数 可以替换成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;
// 设置加密参数
/**
这里设置的参数ios默认为CBC加密方式,如果需要其他加密方式如ECB,在kCCOptionPKCS7Padding这个参数后边加上kCCOptionECBMode,即kCCOptionPKCS7Padding | kCCOptionECBMode,但是记得修改上边的偏移量,因为只有CBC模式有偏移量之说
*/
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编码的代码,可以手动写一个分类,也可以直接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位,但当它大于16位时,7.1的openssl函数会截取前16位,有点坑)
public $iv = '1234567890123456'; //保证偏移量为16位
public $method = 'aes-128-cbc';//加密方式 #AES-256-CBC等
/**
* 加密数据
*
* @param null $data
* @return string
*/
public function aesEn($data)
{
$enData = openssl_encrypt($data, $this->method,$this->key, 0, $this->iv);
return $enData;
}
/**
* 解密数据
*
* @param null $data
* @return string
*/
public function aesDe($data)
{
$decrypted = openssl_decrypt($data, $this->method, $this->key, 0, $this->iv);
return $decrypted;
}
希望可以帮到开发的小伙伴😊,加油咯
网友评论