美文网首页
iOS DES 加密,ECB模式,对称加密

iOS DES 加密,ECB模式,对称加密

作者: 移动的键盘 | 来源:发表于2020-11-24 13:26 被阅读0次
    import UIKit
    import CommonCrypto
    
    class MBADESSwift: NSObject {
    
    /// DES 加密
    /// - Parameters:
    ///   - data: 需要加密的数据
    ///   - key: 加密 密钥 8 位长度
    /// - Returns: 加密后的数据
    @objc class func encrypt(data:Data,key:String) -> Data? {
        
        let keyLength = kCCKeySizeDES + 1
        
        var keyPtr:Array<CChar> = []
    
        for _:Int in 0 ..< keyLength {
            keyPtr.append(CChar(0))
        }
        
        if key.getCString(&keyPtr, maxLength: keyLength, encoding: .utf8) {
            
            let dataLength:Int = data.count
            
            let bufferSize:Int = dataLength + kCCBlockSizeDES
            
            let buffer = malloc(bufferSize)
            
            var numBytesEncrypted:Int = 0
            
            let bytes = [UInt8](data)
            
            let cryptStatus:CCCryptorStatus = CCCrypt(CCOperation(kCCEncrypt), CCAlgorithm(kCCAlgorithmDES), CCOptions(ccPKCS7Padding | kCCModeECB) , keyPtr, kCCKeySizeDES, nil, bytes, dataLength, buffer, bufferSize, &numBytesEncrypted)
            
            var outData:Data?
            
            if cryptStatus == kCCSuccess {
                
                outData = Data.init(bytes: buffer!, count: numBytesEncrypted)
            }
            
            free(buffer)
            
            return outData
        }
        
        return nil;
    }
    
    /// DES 解密
    /// - Parameters:
    ///   - data: 需要解密的数据
    ///   - key: 解密的密钥 8位
    /// - Returns: 解密后的数据
    @objc class func decrypt(data:Data,key:String) -> Data? {
    
        let keyLength = kCCKeySizeDES + 1
        
        var keyPtr:Array<CChar> = []
        
        for _:Int in 0 ..< keyLength {
            keyPtr.append(CChar(0))
        }
    
        if key.getCString(&keyPtr, maxLength: keyLength, encoding: .utf8) {
            
            let dataLength:Int = data.count
            
            let bufferSize:Int = dataLength + kCCBlockSizeDES
            
            let buffer = malloc(bufferSize)
            
            var numBytesDecrypted:Int = 0
            
            let bytes = [UInt8](data)
            
            let cryptStatus:CCCryptorStatus = CCCrypt(CCOperation(kCCDecrypt), CCAlgorithm(kCCAlgorithmDES), CCOptions(ccPKCS7Padding | kCCModeECB) , keyPtr, kCCKeySizeDES, nil, bytes, dataLength, buffer, bufferSize, &numBytesDecrypted)
            
            var outData:Data?
            
            if cryptStatus == kCCSuccess {
                
                outData = Data.init(bytes: buffer!, count: numBytesDecrypted)
            }
            
            free(buffer)
    
            return outData
        }
        
        return nil
    }
    }
    

    加解密 key ,iv 参数,加解密对齐模式ECB/CBC 和服务端协商

    相关文章

      网友评论

          本文标题:iOS DES 加密,ECB模式,对称加密

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