美文网首页
iOS 几种加密方式

iOS 几种加密方式

作者: 木马不在转 | 来源:发表于2017-09-30 10:51 被阅读33次
    一:MD5加密
    + (NSString *)md5:(NSString *)str
    {
        const char *concat_str = [str UTF8String];
        unsigned char result[CC_MD5_DIGEST_LENGTH];
        CC_MD5(concat_str, (unsigned int)strlen(concat_str), result);
        NSMutableString *hash = [NSMutableString string];
        for (int i =0; i<16; i++){
            [hash appendFormat:@"X", result[i]];
        }
        return [hash uppercaseString];
    }
    
    二:base64加密
    @interface NSData (Base64)
    /**
     *  @brief  字符串base64后转data
     */
    + (NSData *)dataWithBase64EncodedString:(NSString *)string
    {
        if (![string length]) return nil;
        NSData *decoded = nil;
    #if __MAC_OS_X_VERSION_MIN_REQUIRED < __MAC_10_9 || __IPHONE_OS_VERSION_MIN_REQUIRED < __IPHONE_7_0
        if (![NSData instancesRespondToSelector:@selector(initWithBase64EncodedString:options:)])
        {
    #pragma clang diagnostic push
    #pragma clang diagnostic ignored "-Wdeprecated-declarations"
            decoded = [[self alloc] initWithBase64Encoding:[string stringByReplacingOccurrencesOfString:@"[^A-Za-z0-9+/=]" withString:@"" options:NSRegularExpressionSearch range:NSMakeRange(0, [string length])]];
    #pragma clang diagnostic pop
        }
        else
    #endif
        {
            decoded = [[self alloc] initWithBase64EncodedString:string options:NSDataBase64DecodingIgnoreUnknownCharacters];
        }
        return [decoded length]? decoded: nil;
    }
    /**
     *  @brief  NSData转string
     *  @param wrapWidth 换行长度  76  64
     */
    - (NSString *)base64EncodedStringWithWrapWidth:(NSUInteger)wrapWidth
    {
        if (![self length]) return nil;
        NSString *encoded = nil;
    #if __MAC_OS_X_VERSION_MIN_REQUIRED < __MAC_10_9 || __IPHONE_OS_VERSION_MIN_REQUIRED < __IPHONE_7_0
        if (![NSData instancesRespondToSelector:@selector(base64EncodedStringWithOptions:)])
        {
    #pragma clang diagnostic push
    #pragma clang diagnostic ignored "-Wdeprecated-declarations"
            encoded = [self base64Encoding];
    #pragma clang diagnostic pop
    
        }
        else
    #endif
        {
            switch (wrapWidth)
            {
                case 64:
                {
                    return [self base64EncodedStringWithOptions:NSDataBase64Encoding64CharacterLineLength];
                }
                case 76:
                {
                    return [self base64EncodedStringWithOptions:NSDataBase64Encoding76CharacterLineLength];
                }
                default:
                {
                    encoded = [self base64EncodedStringWithOptions:(NSDataBase64EncodingOptions)0];
                }
            }
        }
        if (!wrapWidth || wrapWidth >= [encoded length])
        {
            return encoded;
        }
        wrapWidth = (wrapWidth / 4) * 4;
        NSMutableString *result = [NSMutableString string];
        for (NSUInteger i = 0; i < [encoded length]; i+= wrapWidth)
        {
            if (i + wrapWidth >= [encoded length])
            {
                [result appendString:[encoded substringFromIndex:i]];
                break;
            }
            [result appendString:[encoded substringWithRange:NSMakeRange(i, wrapWidth)]];
            [result appendString:@"\r\n"];
        }
        return result;
    }
    /**
     *  @brief  NSData转string 换行长度默认64
     */
    - (NSString *)base64EncodedString
    {
        return [self base64EncodedStringWithWrapWidth:0];
    }
    
    三:AES加密
    #import
    @interface NSData (AES)
    /**
     *  利用AES加密数据
     */
    - (NSData*)encryptedWithAESUsingKey:(NSString*)key andIV:(NSData*)iv {
    
        NSData *keyData = [key dataUsingEncoding:NSUTF8StringEncoding];
    
        size_t dataMoved;
        NSMutableData *encryptedData = [NSMutableData dataWithLength:self.length + kCCBlockSizeAES128];
    
        CCCryptorStatus status = CCCrypt(kCCEncrypt,kCCAlgorithmAES128,kCCOptionPKCS7Padding,keyData.bytes,keyData.length,iv.bytes,self.bytes,self.length,encryptedData.mutableBytes, encryptedData.length,&dataMoved);
    
        if (status == kCCSuccess) {
            encryptedData.length = dataMoved;
            return encryptedData;
        }
    
        return nil;
    }
    
    /**
     *  @brief  利用AES解密据
     */
    - (NSData*)decryptedWithAESUsingKey:(NSString*)key andIV:(NSData*)iv {
    
        NSData *keyData = [key dataUsingEncoding:NSUTF8StringEncoding];
    
        size_t dataMoved;
        NSMutableData *decryptedData = [NSMutableData dataWithLength:self.length + kCCBlockSizeAES128];
    
        CCCryptorStatus result = CCCrypt(kCCDecrypt,kCCAlgorithmAES128,kCCOptionPKCS7Padding,keyData.bytes,keyData.length,iv.bytes,self.bytes,self.length,decryptedData.mutableBytes, decryptedData.length,&dataMoved);
    
        if (result == kCCSuccess) {
            decryptedData.length = dataMoved;
            return decryptedData;
        }
    
        return nil;
    
    }
    
    四:3DES加密
    #import
    @interface NSData (3DES)
    /**
     *  利用3DES加密数据
     */
    - (NSData*)encryptedWith3DESUsingKey:(NSString*)key andIV:(NSData*)iv {
    
        NSData *keyData = [key dataUsingEncoding:NSUTF8StringEncoding];
    
        size_t dataMoved;
        NSMutableData *encryptedData = [NSMutableData dataWithLength:self.length + kCCBlockSize3DES];
    
        CCCryptorStatus result = CCCrypt(kCCEncrypt,kCCAlgorithm3DES,kCCOptionPKCS7Padding,keyData.bytes,keyData.length,iv.bytes,self.bytes,self.length,encryptedData.mutableBytes,encryptedData.length,&dataMoved);
    
        if (result == kCCSuccess) {
            encryptedData.length = dataMoved;
            return encryptedData;
        }
    
        return nil;
    
    }
    /**
     *  @brief   利用3DES解密数据
     */
    - (NSData*)decryptedWith3DESUsingKey:(NSString*)key andIV:(NSData*)iv {
    
        NSData *keyData = [key dataUsingEncoding:NSUTF8StringEncoding];
    
        size_t dataMoved;
        NSMutableData *decryptedData = [NSMutableData dataWithLength:self.length + kCCBlockSize3DES];
    
        CCCryptorStatus result = CCCrypt(kCCDecrypt,kCCAlgorithm3DES,kCCOptionPKCS7Padding,keyData.bytes,keyData.length,iv.bytes,self.bytes,self.length,decryptedData.mutableBytes,decryptedData.length,&dataMoved);
    
        if (result == kCCSuccess) {
            decryptedData.length = dataMoved;
            return decryptedData;
        }
    
        return nil;
    
    }
    

    相关文章

      网友评论

          本文标题:iOS 几种加密方式

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