MD5 3DES加密Swift

Thu*_*ris 2 encryption 3des md5 commoncrypto swift

我有一个应用程序,必须发送已由MD5首先加密,然后由3DES加密的登录凭据.

我已设法使用CryptoSwift通过MD5加密字符串.但是我在Swift上找不到任何加密3DES的东西.

我尝试过CommonCrypto.据我所知,这是在C中,但可以使用桥接头导入到Objective C中.

我找到了一些文章和教程,告诉我如何将CommonCrypto导入Swift,或者通过桥接头(带有警告它不能用于框架)或者通过Model.map.然而,两者都不起作用 我不确定这是否是最新版iOS或Xcode的限制.

有人可以请一个替代方案吗?

谢谢

EDITED

嗨,请看我采取的以下步骤

  1. 好的,我创建了一个名为newEncrypt的新项目.
  2. 我选择不使用标头选项,因为说明这仅限于非框架应用程序/
  3. 我在newEncrypt中创建了一个名为CommonCrypto的文件夹,里面有一个module.map文件.其内容为:模块CommonCrypto [system] {header"/usr/include/CommonCrypto/CommonCrypto.h"export*}
  4. 将$ {SRCROOT}/CommonCrypto添加到swift编译器搜索路径 - 导入路径.调试和发布.
  5. 这是指令停止的地方.我假设我需要将CommonCrypto导入我的班级.此错误"无法构建目标C模块'CommonCrypto'.我还假设我应该在"/usr/include/CommonCrypto/CommonCrypto.h"或"/newEncrypt/CommonCrypto/CommonCrypto.h"中有CommonCrypto库文件(来自CommonCryto'include'文件夹)?我试过这个,但是我只是得到了同样的错误.
  6. 然后,我尝试使用#import添加头文件,并将-lfoo添加到其他链接器标志调试和释放(尽管这可能不是正确的),以防万一仍然需要这样做.但我仍然得到同样无法建立客观c错误.我确定我做错了很明显

Thu*_*ris 9

事实证明,我完全过于复杂了.

有一篇非常有用的文章 http://www.stackoverflow.dluat.com/questions/31004609/how-to-convert-common-crypto-code-from-objective-c-to-swift

我不需要导入任何外部库或SDK,我需要的只是一个桥接头和 #import <CommonCrypto/CommonCrypto.h>

override func viewDidLoad() {
    super.viewDidLoad()
    myEncrypt("my string to encrypt") 
}

func myEncrypt(encryptData:String) -> NSData?{

        var myKeyData : NSData = ("myEncryptionKey" as NSString).dataUsingEncoding(NSUTF8StringEncoding)!
        var myRawData : NSData = encryptData.dataUsingEncoding(NSUTF8StringEncoding)!
        var iv : [UInt8] = [56, 101, 63, 23, 96, 182, 209, 205]  // I didn't use
        var buffer_size : size_t = myRawData.length + kCCBlockSize3DES
        var buffer = UnsafeMutablePointer<NSData>.alloc(buffer_size)
        var num_bytes_encrypted : size_t = 0

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

        var Crypto_status: CCCryptorStatus = CCCrypt(operation, algoritm, options, myKeyData.bytes, keyLength, nil, myRawData.bytes, myRawData.length, buffer, buffer_size, &num_bytes_encrypted)

        if UInt32(Crypto_status) == UInt32(kCCSuccess){

            var myResult: NSData = NSData(bytes: buffer, length: num_bytes_encrypted)

            free(buffer)
            println("my result \(myResult)") //This just prints the data

            let keyData: NSData = myResult
            let hexString = keyData.toHexString()
            println("hex result \(hexString)") // I needed a hex string output


            myDecrypt(myResult) // sent straight to the decryption function to test the data output is the same
            return myResult
        }else{
            free(buffer)
            return nil
        }   
    }


func myDecrypt(decryptData : NSData) -> NSData?{

    var mydata_len : Int = decryptData.length
    var keyData : NSData = ("myEncryptionKey" as NSString).dataUsingEncoding(NSUTF8StringEncoding)!

    var buffer_size : size_t = mydata_len+kCCBlockSizeAES128
    var buffer = UnsafeMutablePointer<NSData>.alloc(buffer_size)
    var num_bytes_encrypted : size_t = 0

    var iv : [UInt8] = [56, 101, 63, 23, 96, 182, 209, 205]  // I didn't use

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

    var decrypt_status : CCCryptorStatus = CCCrypt(operation, algoritm, options, keyData.bytes, keyLength, nil, decryptData.bytes, mydata_len, buffer, buffer_size, &num_bytes_encrypted)

    if UInt32(decrypt_status) == UInt32(kCCSuccess){

        var myResult : NSData = NSData(bytes: buffer, length: num_bytes_encrypted)
        free(buffer)
        println("decrypt \(myResult)")

        var stringResult = NSString(data: myResult, encoding:NSUTF8StringEncoding)
        println("my decrypt string \(stringResult!)")
        return myResult
    }else{
        free(buffer)
        return nil

    }
}
Run Code Online (Sandbox Code Playgroud)

我希望这可以帮助别人.