时间戳--加密,简单示例:在时间time后面拼接@"WbdxyuUM8aiSDnU7",然后AES加密
#import "TSYFriendVc.h"
// 这是获取时间戳的请求
[[NetworkServicesharedInstance]getTimeSuccess:^(NSDictionary*responseObject) {
NSString * time =responseObject[@"time"];
NSString* signString = [NSString stringWithFormat:@"%@%@",@"WbdxyuUM8aiSDnU7", time];
NSData *keydata = [[NSData alloc]init];
keydata= [signString dataUsingEncoding:NSUTF8StringEncoding];
//加密
keydata = [keydata AESSecurity128EncryptWithKey:@"aaaaaaaaaaaaaaaa"];
//编码
NSString * keyString =
[keydata base64EncodedStringFrom:keydata];
//MD5加密(Utilities类中写了MD5加密方法)
NSString *md5Str = [Utilities md5:keyString];
NSString *sign = [md5StruppercaseString];
//发送红包的网络请求
[[NetworkService sharedInstance] redShare:@{@"redCode":self.code,@"idList":_selectFriendArray,@"time":time,@"sign":sign} Success:^(NSDictionary *responseObject) {
[SVProgressHUDshowSuccessWithStatus:@"红包发送成功"];
[self.navigationController popViewControllerAnimated:YES];
} fail:^(NSError
*error) {
[SVProgressHUD showErrorWithStatus:error.userInfo[@"errmsg"]];
}];
}fail:^(NSError
*error) {
}];
}
#import"NSData+Encryption.m"
//给NSData扩充的分类(加密算法,base64编码)
//宏定义
static char base64[] = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
#define gIv @"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"//可以自行修改
#define gIv1 @"aaaaaaaaaaaaaaaa"//可以自行修改
static const char encodingTable[] = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
- (NSData *)AESSecurity128EncryptWithKey:(NSString *)key {//加密
key=@"WbdxyuUM8aiSDnU7";
charkeyPtr[kCCKeySizeAES128+1];
bzero(keyPtr, sizeof(keyPtr));
[key getCString:keyPtr maxLength:sizeof(keyPtr) encoding:NSUTF8StringEncoding];
charivPtr[kCCKeySizeAES128+1];
memset(ivPtr, 0,
sizeof(ivPtr));
[gIv1getCString:ivPtr maxLength:sizeof(ivPtr) encoding:NSUTF8StringEncoding];
NSUInteger dataLength = [self length];
intdiff = kCCKeySizeAES128- (dataLength % kCCKeySizeAES128);
unsigned long
newSize = 0;
if(diff > 0)
{
newSize = dataLength + diff;
}
char dataPtr[newSize];
memcpy(dataPtr, [self bytes], [self length]);
for(int i =0; i < diff; i++) {
// 这里是关键,这里是使用NoPadding的
dataPtr[i + dataLength] = 0x0000;
}
size_t bufferSize = newSize +kCCBlockSizeAES128;
void *buffer = malloc(bufferSize);
memset(buffer, 0, bufferSize);
size_t numBytesEncrypted = 0;
CCCryptorStatus cryptStatus =CCCrypt(kCCEncrypt,
kCCAlgorithmAES128,
kCCOptionECBMode,
keyPtr,
kCCBlockSizeAES128,
ivPtr,
dataPtr,
sizeof(dataPtr),
buffer,
bufferSize,
&numBytesEncrypted);
if (cryptStatus == kCCSuccess)
{
return [NSData dataWithBytesNoCopy:bufferlength:numBytesEncrypted];
}
if (cryptStatus == kCCParamError)
{
}
free(buffer);
returnnil;
}
// Base64编码
- (NSString *)base64EncodedStringFrom:(NSData *)data
{
if ([data length] == 0)
return @"";
char *characters = malloc((([data length] + 2) / 3) * 4);
if (characters == NULL)
return nil;
NSUInteger length = 0;
NSUInteger i = 0;
while (i < [data length])
{
char buffer[3] = {0,0,0};
short bufferLength =0;
while (bufferLength <3 && i < [data length])
buffer[bufferLength++] = ((char*)[data bytes])[i++];
characters[length++] = encodingTable[(buffer[0] & 0xFC) >> 2];
characters[length++] = encodingTable[((buffer[0] & 0x03) << 4) | ((buffer[1] & 0xF0) >> 4)];
if (bufferLength >1)
characters[length++] = encodingTable[((buffer[1] & 0x0F) << 2) | ((buffer[2] & 0xC0) >> 6)];
else characters[length++] ='=';
if (bufferLength >2)
characters[length++] = encodingTable[buffer[2] & 0x3F];
else characters[length++] ='=';
}
return[[NSStringalloc] initWithBytesNoCopy:characters length:length encoding:NSASCIIStringEncodingfreeWhenDone:YES];
}
#import"Utilities.m"
//MD5加密
#define CC_MD5_DIGEST_LENGTH 16
+(NSString *) md5: (NSString*) inPutText{
const char*cStr = [inPutText UTF8String];
unsignedcharresult[CC_MD5_DIGEST_LENGTH];
CC_MD5(cStr, strlen(cStr), result);
return[[NSStringstringWithFormat:@"%02X%02X%02X%02X%02X%02X%02X%02X%02X%02X%02X%02X%02X%02X%02X%02X",
result[0],result[1], result[2],result[3],result[4],result[5], result[6],result[7],result[8],result[9], result[10],result[11],result[12],result[13], result[14],result[15]
] lowercaseString];
}