iOS AES/ECB/PKCS5Padding & HMACSHA1 & URLEncode & Base64

以 @“hello world”为例,不要在意502 那是因为我发的是Helloworld 服务端生气了

加密算法

  1. AES --> Base64 -->URLEncode

  2. HMACSHA1-->Base64--> URLEncode

  3. 将字符串转化为NSData

  4. data 进行HMACSHA1加密 感谢 AESCrypt-Objc --- github

- (NSData *) HMACWithAlgorithm: (CCHmacAlgorithm) algorithm key: (id) key
{
 NSParameterAssert(key == nil || [key isKindOfClass: [NSData class]] || [key isKindOfClass: [NSString class]]);
 
 NSData * keyData = nil;
 if ( [key isKindOfClass: [NSString class]] )
  keyData = [key dataUsingEncoding: NSUTF8StringEncoding];
 else
  keyData = (NSData *) key;
 
 // this could be either CC_SHA1_DIGEST_LENGTH or CC_MD5_DIGEST_LENGTH. SHA1 is larger.
 unsigned char buf[CC_SHA1_DIGEST_LENGTH];
 CCHmac( algorithm, [keyData bytes], [keyData length], [self bytes], [self length], buf );
 
 return ( [NSData dataWithBytes: buf length: (algorithm == kCCHmacAlgMD5 ? CC_MD5_DIGEST_LENGTH : CC_SHA1_DIGEST_LENGTH)] );
}


3. data 进行base64加密                 感谢base64
- (NSString *)base64EncodedString
{
    return [self base64EncodedStringWithWrapWidth:0];
}

4.得到的字符串尽心URLEncode
- (NSString *)URLEncode {
    return [self URLEncodeUsingEncoding:NSUTF8StringEncoding];
}

- (NSString *)URLEncodeUsingEncoding:(NSStringEncoding)encoding {
    return (__bridge_transfer NSString *)CFURLCreateStringByAddingPercentEscapes(NULL,
                                                                                 (__bridge CFStringRef)self,
                                                                                 NULL,
                                                                                 (CFStringRef)@"!*'\"();:@&=+$,/?%#[]% ",
                                                                                 CFStringConvertNSStringEncodingToEncoding(encoding));
}

1。感谢    NSString+AES.h

对@“helloworld“ 进行 AES/ECB/PKCS5Padding 加密
- (NSString *)AES128EncryptWithKey:(NSString *)key
{
    NSData *plainData = [self dataUsingEncoding:NSUTF8StringEncoding];
    NSData *encryptedData = [plainData AES128EncryptWithKey:key];
    

2. 这里已经进行base64就不需要再进行base64 加密否则值会发生变化

    NSString *encryptedString = [encryptedData base64Encoding];
    
    return encryptedString;
}

- (NSString *)AES128DecryptWithKey:(NSString *)key
{
    NSData *encryptedData = [NSData dataWithBase64EncodedString:self];
    NSData *plainData = [encryptedData AES128DecryptWithKey:key];
    
    NSString *plainString = [[NSString alloc] initWithData:plainData encoding:NSUTF8StringEncoding];
    
    return plainString;
}

3. 最后进行URLEncode
- (NSString *)URLEncode {
    return [self URLEncodeUsingEncoding:NSUTF8StringEncoding];
}

- (NSString *)URLEncodeUsingEncoding:(NSStringEncoding)encoding {
    return (__bridge_transfer NSString *)CFURLCreateStringByAddingPercentEscapes(NULL,
                                                                                 (__bridge CFStringRef)self,
                                                                                 NULL,
                                                                                 (CFStringRef)@"!*'\"();:@&=+$,/?%#[]% ",
                                                                                 CFStringConvertNSStringEncodingToEncoding(encoding));
}

得到的结果如下 不要在意502 我们是在测试加密算法有没有问题

iOS AES/ECB/PKCS5Padding & HMACSHA1 & URLEncode & Base64_第1张图片
加密.png

你可能感兴趣的:(iOS AES/ECB/PKCS5Padding & HMACSHA1 & URLEncode & Base64)