如何将encrypt方法转换为解密

时间:2018-03-21 08:30:45

标签: swift swift3 swift4

我有以下要解密的方法。以下方法加密。我该怎么做才能解密?

代码

PlainString = "5lg3yg7Q+Y8="

func tripleDesEncrypt(PlainString: String) -> String {

    let keyData         = "FIFEDCBA98765432104AABFD"
    let PlainStringvar  = PlainString

    guard let data = PlainStringvar.data(using: .utf8) else {
        return ""           
    }

    let cryptData   = NSMutableData(length: Int(data.count) + kCCBlockSize3DES)!

    let keyLength              = size_t(kCCKeySize3DES)
    let operation: CCOperation = UInt32(kCCEncrypt)
    let algoritm:  CCAlgorithm = UInt32(kCCAlgorithm3DES)
    let options:   CCOptions   = UInt32(kCCOptionECBMode + kCCOptionPKCS7Padding)

    var numBytesEncrypted :size_t = 0

    let cryptStatus = CCCrypt(operation,
                              algoritm,
                              options,
                              keyData.bytes, keyLength,
                              nil,
                              data.bytes, data.count,
                              cryptData.mutableBytes, cryptData.length,
                              &numBytesEncrypted)

    if UInt32(cryptStatus) == UInt32(kCCSuccess) {
        cryptData.length = Int(numBytesEncrypted)

       var base64cryptString = cryptData.base64EncodedString(options: .lineLength64Characters)


        return base64cryptString //// encodeString(str: base64cryptString)

    } else {
        print("Error: \(cryptStatus)")
    }

    return ""
}

2 个答案:

答案 0 :(得分:1)

我从未对Swift进行过加密/解密,但基于该代码,您只需更改此行

let operation: CCOperation = UInt32(kCCEncrypt)

let operation: CCOperation = UInt32(kCCDecrypt)

在对这个答案进行小规模搜索时,我遇到了这个问题 https://github.com/DigitalLeaves/CommonCrypto-in-Swift

它可能很有用,不仅可以作为一个你可以使用的库,而且可以用来研究代码,看起来你是Swift语言的新手,所以我建议你去看看

答案 1 :(得分:1)

您的SELECT COUNT(a.tripId) as total, a.tripId as tripId FROM trip_details a INNER JOIN escort_allocation b ON a.allocationId = b.allocationId GROUP BY a.allocationId LIMIT 0 , 30 无法在Swift 4中编译,因此我需要修复它:

tripleDesEncrypt

代码由3部分组成:

  • 将plainString转换为UTF-8编码数据
  • 使用keyData
  • 在3DES,ECB,PKCS7中加密UTF-8编码数据
  • 将加密数据转换为base-64字符串

因此,要解密,您需要以相反的顺序应用逆转换:

  • 将base-64字符串转换为加密数据
  • 使用keyData解密3DES,ECB,PKCS7中的UTF-8编码数据
  • 将UTF-8编码数据转换为plainString

方法如下:

func tripleDesEncrypt(plainString: String) -> String {

    let keyData         = "FIFEDCBA98765432104AABFD".data(using: .utf8)!

    //Converting plainString to UTF-8 encoded data
    guard let data = plainString.data(using: .utf8) else {
        return ""
    }

    var cryptData   = Data(count: data.count + kCCBlockSize3DES)

    let keyLength              = size_t(kCCKeySize3DES)
    let operation: CCOperation = CCOperation(kCCEncrypt)
    let algoritm:  CCAlgorithm = CCAlgorithm(kCCAlgorithm3DES)
    let options:   CCOptions   = CCOptions(kCCOptionECBMode + kCCOptionPKCS7Padding)

    var numBytesEncrypted :size_t = 0

    //Encrypting UTF-8 encoded data in 3DES, ECB, PKCS7 with keyData
    let cryptStatus = keyData.withUnsafeBytes {keyBytes in
        data.withUnsafeBytes {dataBytes in
            cryptData.withUnsafeMutableBytes {mutableBytes in
                CCCrypt(operation,
                        algoritm,
                        options,
                        keyBytes, keyLength,
                        nil,
                        dataBytes, data.count,
                        mutableBytes, cryptData.count,
                        &numBytesEncrypted)
            }
        }
    }

    if cryptStatus == CCCryptorStatus(kCCSuccess) {
        //Converting encrypted data into base-64 String
        cryptData.count = Int(numBytesEncrypted)
        let base64cryptString = cryptData.base64EncodedString(options: .lineLength64Characters)
        return base64cryptString
    } else {
        print("Error: \(cryptStatus)")
        return ""
    }
}

用法:

func tripleDesDecrypt(base64String: String) -> String {

    let keyData         = "FIFEDCBA98765432104AABFD".data(using: .utf8)!

    //Converting base-64 String into encrypted data
    guard let encryptedData = Data(base64Encoded: base64String, options: .ignoreUnknownCharacters) else {
        return ""
    }

    var decryptedData   = Data(count: encryptedData.count)

    let keyLength              = size_t(kCCKeySize3DES)
    let operation: CCOperation = CCOperation(kCCDecrypt)
    let algoritm:  CCAlgorithm = CCAlgorithm(kCCAlgorithm3DES)
    let options:   CCOptions   = CCOptions(kCCOptionECBMode + kCCOptionPKCS7Padding)

    var numBytesDecrypted :size_t = 0

    //Decrypting to UTF-8 encoded data in 3DES, ECB, PKCS7 with keyData
    let cryptStatus = keyData.withUnsafeBytes {keyBytes in
        encryptedData.withUnsafeBytes {dataBytes in
            decryptedData.withUnsafeMutableBytes {mutableBytes in
                CCCrypt(operation,
                        algoritm,
                        options,
                        keyBytes, keyLength,
                        nil,
                        dataBytes, encryptedData.count,
                        mutableBytes, decryptedData.count,
                        &numBytesDecrypted)
            }
        }
    }

    if cryptStatus == CCCryptorStatus(kCCSuccess) {
        //Converting UTF-8 encoded data into plainString
        let plainString = String(data: decryptedData, encoding: .utf8) ?? ""
        return plainString
    } else {
        print("Error: \(cryptStatus)")
        return ""
    }
}