AES128加密解密(key和向量)

参考文章:https://www.jianshu.com/p/eaa3d9df3ef5?from=singlemessage&isappinstalled=0
详细有OC的小坑点

主要是向量和key和后端对应好规则!!!
后台java和安卓那边用的相同的代码库,所以iOS这边没有文档完全尴尬!

  NSString *Iv = [MD5key substringWithRange:NSMakeRange(0, 16)];
  NSString *key = [MD5key substringFromIndex:(16)];
//TODO: AES128加密
+(NSString *)AES128EncryptWithText:(NSString *)text Key:(NSString *)MD5key{
    
    NSString *Iv = [MD5key substringWithRange:NSMakeRange(0, 16)];
    NSString *key = [MD5key substringFromIndex:(16)];
    
    NSData *data = [text dataUsingEncoding:NSUTF8StringEncoding];
    // 'key' should be 32 bytes for AES128, will be null-padded otherwise
    char keyPtr[kCCKeySizeAES128+1]; // room for terminator (unused)
    bzero(keyPtr, sizeof(keyPtr)); // fill with zeroes (for padding)
    
    // fetch key data
    [key getCString:keyPtr maxLength:sizeof(keyPtr) encoding:NSUTF8StringEncoding];
    
    
    char ivPtr[kCCKeySizeAES128+1];
    memset(ivPtr, 0, sizeof(ivPtr));
    [Iv getCString:ivPtr maxLength:sizeof(ivPtr) encoding:NSUTF8StringEncoding];
    
    NSUInteger dataLength = [data length];
    
    //See the doc: For block ciphers, the output size will always be less than or
    //equal to the input size plus the size of one block.
    //That's why we need to add the size of one block here
    size_t bufferSize = dataLength + kCCBlockSizeAES128;
    void *buffer = malloc(bufferSize);
    
    size_t numBytesEncrypted = 0;
    CCCryptorStatus cryptStatus = CCCrypt(kCCEncrypt, kCCAlgorithmAES128, kCCOptionPKCS7Padding,
                                          keyPtr, kCCKeySizeAES128,
                                          ivPtr /* initialization vector (optional) */,
                                          [data bytes], dataLength, /* input */
                                          buffer, bufferSize, /* output */
                                          &numBytesEncrypted);
    if (cryptStatus == kCCSuccess) {
        //the returned NSData takes ownership of the buffer and will free it on deallocation
        NSData *resultData = [NSData dataWithBytesNoCopy:buffer length:numBytesEncrypted];
        NSString *base64Encode = [resultData base64EncodedStringWithOptions:0];
        return base64Encode;
    }
    
    free(buffer); //free the buffer;
    return nil;
}
/**AES128解密*/
+(NSString *)AES128Decrypt:(NSString *)encryptText Key:(NSString *)MD5key{
    
    NSString *Iv = [MD5key substringWithRange:NSMakeRange(0, 16)];
    NSString *key = [MD5key substringFromIndex:(16)];
    
    char keyPtr[kCCKeySizeAES128+1]; // room for terminator (unused)
    bzero(keyPtr, sizeof(keyPtr)); // fill with zeroes (for padding)
    
    // fetch key data
    [key getCString:keyPtr maxLength:sizeof(keyPtr) encoding:NSUTF8StringEncoding];
    
    
    char ivPtr[kCCKeySizeAES128+1];
    memset(ivPtr, 0, sizeof(ivPtr));
    [Iv getCString:ivPtr maxLength:sizeof(ivPtr) encoding:NSUTF8StringEncoding];
    
    //将nsstring转化为nsdata
    NSData *base64Data = [encryptText dataUsingEncoding:NSUTF8StringEncoding];
    NSData *data = [[NSData alloc] initWithBase64EncodedData:base64Data options:0];
    NSUInteger dataLength = [data length];
    size_t bufferSize = dataLength + kCCBlockSizeAES128;
    void *buffer = malloc(bufferSize);
    
    size_t numBytesDecrypted = 0;
    CCCryptorStatus cryptStatus = CCCrypt(kCCDecrypt, kCCAlgorithmAES128, kCCOptionPKCS7Padding,
                                          keyPtr, kCCKeySizeAES128,
                                          ivPtr /* initialization vector (optional) */,
                                          [data bytes], dataLength, /* input */
                                          buffer, bufferSize, /* output */
                                          &numBytesDecrypted);
    
    if (cryptStatus == kCCSuccess) {
        //the returned NSData takes ownership of the buffer and will free it on deallocation
        NSData *resultData = [NSData dataWithBytesNoCopy:buffer length:numBytesDecrypted];
        NSString *str = [[NSString alloc] initWithData:resultData encoding:NSUTF8StringEncoding];
        return str;
    }
    
    free(buffer); //free the buffer;
    return nil;
}

你可能感兴趣的:(AES128加密解密(key和向量))