MG--Swift3.0 加密相关方法

  • 一、Swift3.0系统提供的base64加密方法(可逆)

extension String {
    /**
     *   Base64 加密
     *   return 加密字符串
     */
    func encodeToBase64() -> String {
        guard let data = self.data(using: String.Encoding.utf8) else { print("加密失败"); return "" }
        return data.base64EncodedString(options: Data.Base64EncodingOptions(rawValue: 0)) //系统提供的方法,iOS7之后可用
    }
    /**
     *   Base64 解密
     *   return 解密字符串
     */
    func decodeBase64() -> String {
        guard let data = Data(base64Encoded: self, options: Data.Base64DecodingOptions(rawValue: 0)) else { print("解密失败"); return ""                  }
        return String(data: data, encoding: String.Encoding.utf8)!
    }
}
let base64Str = "明哥"                       // 打印 "明哥"
let encodeStr = base64Str.encodeToBase64()  // 打印 "5piO5ZOl"
let decodeStr = encodeStr.decodeBase64()    // 打印 "明哥"
打印截图



  • 二、HMAC_SHA1/MD5/SHA1/SHA224...... (不可逆)

// MARK: - 加密  HMAC_SHA1/MD5/SHA1/SHA224...... 
/**  需在桥接文件导入头文件 ,因为C语言的库
 *   #import <CommonCrypto/CommonDigest.h>
 *   #import <CommonCrypto/CommonHMAC.h>
 */
enum CryptoAlgorithm {  // 2,SHA(安全散列算法:Secure Hash Algorithm) // 不可逆
    /// 加密的枚举选项 HMAC
    case MD5, SHA1, SHA224, SHA256, SHA384, SHA512
    var HMACAlgorithm: CCHmacAlgorithm {
        var result: Int = 0
        switch self {
            case .MD5:      result = kCCHmacAlgMD5
            case .SHA1:     result = kCCHmacAlgSHA1
            case .SHA224:   result = kCCHmacAlgSHA224
            case .SHA256:   result = kCCHmacAlgSHA256
            case .SHA384:   result = kCCHmacAlgSHA384
            case .SHA512:   result = kCCHmacAlgSHA512
        }
        return CCHmacAlgorithm(result)
    }
    var digestLength: Int {
        var result: Int32 = 0
        switch self {
            case .MD5:      result = CC_MD5_DIGEST_LENGTH
            case .SHA1:     result = CC_SHA1_DIGEST_LENGTH
            case .SHA224:   result = CC_SHA224_DIGEST_LENGTH
            case .SHA256:   result = CC_SHA256_DIGEST_LENGTH
            case .SHA384:   result = CC_SHA384_DIGEST_LENGTH
            case .SHA512:   result = CC_SHA512_DIGEST_LENGTH
        }
        return Int(result)
    }
}
extension String {
    /**
        func: 加密方法
         - parameter algorithm: 加密方式;
         - parameter key: 加密的key
     */
    func hmac(algorithm: CryptoAlgorithm, key: String) -> String {
        let str = self.cString(using: String.Encoding.utf8)
        let strLen = Int(self.lengthOfBytes(using: String.Encoding.utf8))
        let digestLen = algorithm.digestLength
        let result = UnsafeMutablePointer<CUnsignedChar>.allocate(capacity: digestLen)
        let keyStr = key.cString(using: String.Encoding.utf8)
        let keyLen = Int(key.lengthOfBytes(using: String.Encoding.utf8))
        //  CCHmac是系统内部方法
        CCHmac(algorithm.HMACAlgorithm, keyStr!, keyLen, str!, strLen, result)
        let digest = stringFromResult(result:  result, length: digestLen)
        result.deallocate(capacity: digestLen)
        return digest
    }
    // 内部结果
    private func stringFromResult(result: UnsafeMutablePointer<CUnsignedChar>, length: Int) -> String {
        let hash = NSMutableString()
        for i in 0..<length {
            hash.appendFormat("%02x", result[i])
        }
        return String(hash)
    }
}
  • 举个使用:
override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
        super.touchesBegan(touches, with: event)
        let str = "welcome to hangge.com"
        let key = "67FG"
        //  HMAC
        let hmacStr = str.hmac(algorithm: .SHA1, key: key)
        print("原始字符串:\(str)")
        print("key:\(key)")
        print("HMAC运算结果:\(hmacStr)")
        //  SHA224
        let SHA224 = str.hmac(algorithm: .SHA224, key: key)
        print("原始字符串:\(str)")
        print("key:\(key)")
        print("SHA224运算结果:\(SHA224)")
        //  md5
        let str1 = "欢迎来到zhuhai"
        let key2 = "你好"
        let md5 = str1.hmac(algorithm: .MD5, key: key2)
        print("原始字符串:\(str1)")
        print("key:\(key2)")
        print("md5运算结果:\(md5)")
        //  普通md5
        let md55 = str1.md5
        print("md55 = \(md55)")
        //  普通md5测试 
        print("md测试 = \("lacar我是牛人才怪lacar".md5)")
        print("md用户测试 = \("用户名".md5)")
        print("md我们测试 = \("我们".md5)")
        print("md123测试 = \("123".md5)")
        print("mdming测试 = \("ming".md5)")
        print("md你好测试 = \("你好".md5)")
        print("mdlacar测试 = \("lacar".md5)")
    }
测试结果



  • 三、AES、DES等(可逆)

    • 封装方法

//  NSData+Extension.swift
//  Created by ming.com on 17/3/21.
//  Copyright © 2017年 MG明明. All rights reserved.
import UIKit
// MARK: - 加密  AES/AES128/DES/DES3/CAST/RC2/RC4/Blowfish......
/**  需在桥接文件导入头文件 ,因为C语言的库
    #import <CommonCrypto/CommonDigest.h>
    #import <CommonCrypto/CommonCrypto.h>
 */
enum CryptoAlgorithm {
    /// 加密的枚举选项 AES/AES128/DES/DES3/CAST/RC2/RC4/Blowfish......
    case AES, AES128, DES, DES3, CAST, RC2,RC4, Blowfish
    var algorithm: CCAlgorithm {
        var result: UInt32 = 0
            switch self {
            case .AES:          result = UInt32(kCCAlgorithmAES)
            case .AES128:       result = UInt32(kCCAlgorithmAES128)
            case .DES:          result = UInt32(kCCAlgorithmDES)
            case .DES3:         result = UInt32(kCCAlgorithm3DES)
            case .CAST:         result = UInt32(kCCAlgorithmCAST)
            case .RC2:          result = UInt32(kCCAlgorithmRC2)
            case .RC4:          result = UInt32(kCCAlgorithmRC4)
            case .Blowfish:     result = UInt32(kCCAlgorithmBlowfish)
        }
        return CCAlgorithm(result)
    }
    var keyLength: Int {
        var result: Int = 0
        switch self {
            case .AES:          result = kCCKeySizeAES128
            case .AES128:       result = kCCKeySizeAES256
            case .DES:          result = kCCKeySizeDES
            case .DES3:         result = kCCKeySize3DES
            case .CAST:         result = kCCKeySizeMaxCAST
            case .RC2:          result = kCCKeySizeMaxRC2
            case .RC4:          result = kCCKeySizeMaxRC4
            case .Blowfish:     result = kCCKeySizeMaxBlowfish
        }
        return Int(result)
    }
    var cryptLength: Int {
        var result: Int = 0
        switch self {
            case .AES:          result = kCCKeySizeAES128
            case .AES128:       result = kCCBlockSizeAES128
            case .DES:          result = kCCBlockSizeDES
            case .DES3:         result = kCCBlockSize3DES
            case .CAST:         result = kCCBlockSizeCAST
            case .RC2:          result = kCCBlockSizeRC2
            case .RC4:          result = kCCBlockSizeRC2
            case .Blowfish:     result = kCCBlockSizeBlowfish
        }
        return Int(result)
    }
}
// MARK: - 加密扩展NSData
extension NSData {
    /*
     加密
     - parameter algorithm: 加密方式
     - parameter keyData:   加密key
     - return NSData: 加密后的数据 可选值
     */
    func enCrypt(algorithm: CryptoAlgorithm, keyData:NSData) -> NSData? {
        return crypt(algorithm: algorithm, operation: CCOperation(kCCEncrypt), keyData: keyData)
    }
    /*
     解密
     - parameter algorithm: 解密方式
     - parameter keyData:   解密key  
     - return NSData: 解密后的数据  可选值
     */
    func deCrypt(algorithm: CryptoAlgorithm, keyData:NSData) -> NSData? {
        return crypt(algorithm: algorithm, operation: CCOperation(kCCDecrypt), keyData: keyData)
    } 
    /*
     解密和解密方法的抽取的封装方法
     - parameter algorithm: 何种加密方式
     - parameter operation: 加密和解密
     - parameter keyData:   加密key
     - return NSData: 解密后的数据  可选值
     */
    func crypt(algorithm: CryptoAlgorithm, operation:CCOperation, keyData:NSData) -> NSData? {
        let keyBytes        = keyData.bytes
        let keyLength       = Int(algorithm.keyLength)
        let dataLength      = self.length
        let dataBytes       = self.bytes
        let cryptLength     = Int(dataLength+algorithm.cryptLength)
        let cryptPointer    = UnsafeMutablePointer<UInt8>.allocate(capacity: cryptLength)
        let algoritm:  CCAlgorithm = CCAlgorithm(algorithm.algorithm)
        let option:   CCOptions    = CCOptions(kCCOptionECBMode + kCCOptionPKCS7Padding)
        let numBytesEncrypted = UnsafeMutablePointer<Int>.allocate(capacity: 1)
        numBytesEncrypted.initialize(to: 0)
        let cryptStatus = CCCrypt(operation, algoritm, option, keyBytes, keyLength, nil, dataBytes, dataLength, cryptPointer, cryptLength, numBytesEncrypted)
        // 判断是否加密成功
        if CCStatus(cryptStatus) == CCStatus(kCCSuccess) {
            let len = Int(numBytesEncrypted.pointee)
            let data:NSData = NSData(bytesNoCopy: cryptPointer, length: len)
            numBytesEncrypted.deallocate(capacity: 1)
            return data
        } else {
            numBytesEncrypted.deallocate(capacity: 1)
            cryptPointer.deallocate(capacity: cryptLength)
            return nil
        }
      }
}
  • 如何使用:

static func test(){
        let keyString     = "12345678901234567890123456789012"
        let keyData: NSData! = (keyString as NSString).data(using: String.Encoding.utf8.rawValue) as NSData!
        let message       = "你是大傻瓜吗?你才是"
        let data: NSData! = (message as NSString).data(using: String.Encoding.utf8.rawValue) as NSData!      
        print("要加密的字符串:" + message)
        // 下边时进行加密解密的代码
        let result:NSData? = data.AES128Crypt(operation: CCOperation(kCCEncrypt), keyData: keyData)
        print("encrypt = \(result!)")       
        let oldData = result?.AES128Crypt(operation: CCOperation(kCCDecrypt), keyData: keyData)
        print("decrypt = \(oldData!)")
        print(String(data: oldData as! Data, encoding: String.Encoding.utf8)!)
         // 下边时进行加密解密的数据
        let enData = data.enCrypt(algorithm: .AES, keyData: keyData)
        let deData = enData?.deCrypt(algorithm: .AES, keyData: keyData)
        print("通过解密后的字符串:" + String(data: deData as! Data, encoding: String.Encoding.utf8)!)
    }
打印结果.png
  • 探究

        let keyString     = "12345678901234567890123456789012"
        let keyData: NSData! = (keyString as NSString).data(using: String.Encoding.utf8.rawValue) as NSData!
        let message       = "你是大傻瓜吗?你才是"
        let data: NSData! = (message as NSString).data(using: String.Encoding.utf8.rawValue) as NSData!
        let enData = data.enCrypt(algorithm: .AES, keyData: keyData)
        let deData = enData?.deCrypt(algorithm: .AES, keyData: keyData)
        print("通过解密后的字符串:" + String(data: deData as! Data, encoding: String.Encoding.utf8)!)
        let enData1 = data.enCrypt(algorithm: .DES, keyData: keyData)
        let deData1 = enData1?.deCrypt(algorithm: .DES, keyData: keyData)
        print("通过解密后的字符串:" + String(data: deData1 as! Data, encoding: String.Encoding.utf8)!)
        let enData2 = data.enCrypt(algorithm: .RC2, keyData: keyData)
        let deData2 = enData2?.deCrypt(algorithm: .RC2, keyData: keyData)
        print("通过解密后的字符串:" + String(data: deData2 as! Data, encoding: String.Encoding.utf8)!)
        print("enData-AES: + \(enData!)")
        print("enData-DES: + \(enData1!)")
        print("enData-RC2: + \(enData2!)")
        print("deData-AES: + \(deData!)")
        print("deData-DES: + \(deData1!)")
        print("deData-RC2: + \(deData2!)")
NData测试打印结果.png

结论: 探究同一个字符串和同一个key不同加密类型发现,加密的方式不同,所生成的加密过得data不同,但是解密出来的data是相同的,当转成字符串之后,就可以看到之前加密过得字符串了


  • 接上第三AES加密,给Data扩展

    • NSData扩展大致一样

      • 与NSData的主要区别: 这个写法不一样
        keyData.withUnsafeBytes {(bytes: UnsafePointer<Int8>)->Void in
        keyBytes = bytes
        }
        let data = Data.init(bytes: cryptPointer, count: len)
// MARK: - Data    Data打印出来是16或者32 bytes,打印需转成NSData类型打印结果比较直观
// MARK: - data
extension Data {
    /*
     加密
     - parameter algorithm: 加密方式
     - parameter keyData:   加密key
     
     - return NSData: 加密后的数据 可选值
     */
    mutating func enCrypt(algorithm: CryptoAlgorithm, keyData:Data) -> Data? {
        return crypt(algorithm: algorithm, operation: CCOperation(kCCEncrypt), keyData: keyData)
    }
    
    /*
     解密
     - parameter algorithm: 解密方式
     - parameter keyData:   解密key
     
     - return NSData: 解密后的数据  可选值
     */
    mutating func deCrypt(algorithm: CryptoAlgorithm, keyData:Data) -> Data? {
        return crypt(algorithm: algorithm, operation: CCOperation(kCCDecrypt), keyData: keyData)
    }
    
    
    /*
     解密和解密方法的抽取的封装方法
     - parameter algorithm: 何种加密方式
     - parameter operation: 加密和解密
     - parameter keyData:   加密key
     
     - return NSData: 解密后的数据  可选值
     */
    mutating func crypt(algorithm: CryptoAlgorithm, operation:CCOperation, keyData: Data) -> Data? {
        let keyLength       = Int(algorithm.keyLength)
        let dataLength      = self.count
        let cryptLength     = Int(dataLength+algorithm.cryptLength)
        let cryptPointer    = UnsafeMutablePointer<UInt8>.allocate(capacity: cryptLength)
        let algoritm:  CCAlgorithm = CCAlgorithm(algorithm.algorithm)
        let option:   CCOptions    = CCOptions(kCCOptionECBMode + kCCOptionPKCS7Padding)
        let numBytesEncrypted = UnsafeMutablePointer<Int>.allocate(capacity: 1)
        numBytesEncrypted.initialize(to: 0)
        
        var keyBytes: UnsafePointer<Int8>?
        keyData.withUnsafeBytes {(bytes: UnsafePointer<Int8>)->Void in
             keyBytes = bytes
        }
        
        var dataBytes: UnsafePointer<Int8>?
        self.withUnsafeBytes {(bytes: UnsafePointer<CChar>)->Void in
//            print(bytes)
            dataBytes = bytes
        }
        
        let cryptStatus = CCCrypt(operation, algoritm, option, keyBytes!, keyLength, nil, dataBytes!, dataLength, cryptPointer, cryptLength, numBytesEncrypted)
        
        if CCStatus(cryptStatus) == CCStatus(kCCSuccess) {
            let len = Int(numBytesEncrypted.pointee)
            let data = Data.init(bytes: cryptPointer, count: len)
            numBytesEncrypted.deallocate(capacity: 1)
            return data
        } else {
            numBytesEncrypted.deallocate(capacity: 1)
            cryptPointer.deallocate(capacity: cryptLength)
            return nil
        }
    }
}
// MARK: - Data测试
extension Data {
    static func test1(){
        let keyString        = "123"
        let message          = "you are so stuip"
        let keyData = keyString.data(using: String.Encoding.utf8)
        var data = message.data(using: String.Encoding.utf8)
        
        var result = data?.enCrypt(algorithm: CryptoAlgorithm.AES, keyData: keyData!)
        print("result = \(result!)")
        let data1 = NSData(data: result!)
        print(data1)
        print(NSString(data: result!, encoding: String.Encoding.utf8.rawValue))
        
        let oldData = result?.deCrypt(algorithm: CryptoAlgorithm.AES, keyData: keyData!)
        print("oldData = \(oldData! as NSData)")
        print(String(data: oldData!, encoding: String.Encoding.utf8)!)
    }
}
Data测试打印.png

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 212,294评论 6 493
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 90,493评论 3 385
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 157,790评论 0 348
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 56,595评论 1 284
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 65,718评论 6 386
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 49,906评论 1 290
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 39,053评论 3 410
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 37,797评论 0 268
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 44,250评论 1 303
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 36,570评论 2 327
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 38,711评论 1 341
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 34,388评论 4 332
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 40,018评论 3 316
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 30,796评论 0 21
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 32,023评论 1 266
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 46,461评论 2 360
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 43,595评论 2 350

推荐阅读更多精彩内容

  • 常见的几种加密方法和实 常见的几种加密方法 : git demo下载 csdn demo下载 MD5 SHA1 R...
    Time_x阅读 14,370评论 0 2
  • 一. 哈希算法 MD5和SHA都是用于创建唯一密钥以保护数据文件的哈希算法。从黑客的角度来看,SHA比MD5更复杂...
    Imkata阅读 181评论 0 0
  • 在密码学中,加密部分主要分为对称加密和非对称加密,非对称加密主要有RSA非对称加密(使用公钥/私钥来加密解密),对...
    iOS弗森科阅读 684评论 0 3
  • 背景 ​ 随着移动互联网的普及,被越来越多的心怀不轨的人觊觎,也越来越多的安全问题暴露了出来。开发者开发出来的应用...
    陵无山阅读 3,021评论 1 13
  • /**ios常见的几种加密方法: 普通的加密方法是讲密码进行加密后保存到用户偏好设置( [NSUserDefaul...
    彬至睢阳阅读 2,919评论 0 7