美文网首页
AES_Base64解密,横向量 UTF8String

AES_Base64解密,横向量 UTF8String

作者: smkoc | 来源:发表于2017-11-09 13:31 被阅读0次
+(NSString *)AES128Decrypt:(NSString *)encryptText
{
    char keyPtr[kCCKeySizeAES128 + 1];
    memset(keyPtr, 0, sizeof(keyPtr));
    [[[NYBUserCenter defaultCenter]getAccessKey] getCString:keyPtr maxLength:sizeof(keyPtr) encoding:NSUTF8StringEncoding];
    
    NSData *data = [GTMBase64 decodeData:[encryptText dataUsingEncoding:NSUTF8StringEncoding]];

    NSUInteger dataLength = [data length];
    size_t bufferSize     = dataLength + kCCBlockSizeAES128;
    void *buffer          = malloc(bufferSize);
    
    size_t numBytesCrypted      = 0;
    CCCryptorStatus cryptStatus = CCCrypt(kCCDecrypt,
                                          kCCAlgorithmAES128,
                                          kCCOptionPKCS7Padding,
                                          keyPtr,
                                          kCCBlockSizeAES128,
                                          [[[NYBUserCenter defaultCenter]getAccessIV] UTF8String],    // iv   UTF8String
                                          [data bytes],
                                          dataLength,
                                          buffer,
                                          bufferSize,
                                          &numBytesCrypted);
    if (cryptStatus == kCCSuccess) {
        NSData *resultData = [NSData dataWithBytesNoCopy:buffer length:numBytesCrypted];
        return [[NSString alloc] initWithData:resultData encoding:NSUTF8StringEncoding];
    }
    free(buffer);
    return nil;
}

相关文章

网友评论

      本文标题: AES_Base64解密,横向量 UTF8String

      本文链接:https://www.haomeiwen.com/subject/igppmxtx.html