MD5 3DES加密Swift [英] MD5 3DES encryption Swift

查看:120
本文介绍了MD5 3DES加密Swift的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I have an app that must send login credentials that have been encrypted first by MD5 and then by 3DES.

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

I have managed to use CryptoSwift to encrypt the string by MD5. However I cannot find anything to encrypt by 3DES on Swift.

我尝试过CommonCrypto.据我所知,这是用C语言编写的,但可以通过桥接标头导入到Objective C中.

I have tried CommonCrypto. As far as I can tell this is in C but could be imported into Objective C with a bridging header.

我发现了一些文章和教程,它们告诉我如何通过桥接标头(警告它不适用于框架)或Model.map将CommonCrypto导入Swift.但是,两者都不起作用.我不确定这是否是最新版本的iOS或Xcode中的限制.

I have found a few articles and tutorials that tell me how to import CommonCrypto into Swift, either by a bridging header(with the warning it will not work with frameworks) or by Model.map. However neither are working. Im not sure if this is a limitation in the latest versions of iOS or Xcode.

有人可以建议替代方法吗?

Could someone please advise an alternative?

谢谢

已编辑

您好,请参阅我已执行的以下步骤

Hi, please see the below steps I have taken

  1. 好的,所以我创建了一个名为newEncrypt的新项目.
  2. 我选择不使用标头选项,因为说明说这仅限于非Framework应用程序/
  3. 我在newEncrypt内创建了一个名为CommonCrypto的文件夹,其中包含module.map文件.其内容为:module CommonCrypto [system] { 标头"/usr/include/CommonCrypto/CommonCrypto.h" 出口 * }
  4. 添加了$ {SRCROOT}/CommonCrypto,以加快编译器搜索路径导入路径.调试并发布.
  5. 这是指示停止的地方.我假设我需要将CommonCrypto导入到我的班级中.出现无法建立目标C模块'CommonCrypto'的错误. 我还假设我应该在"/usr/include/CommonCrypto/CommonCrypto.h"或"/newEncrypt/CommonCrypto/CommonCrypto.h"中拥有CommonCrypto库文件(来自CommonCryto的"include"文件夹)? 我已经尝试过了,但是却遇到了同样的错误.
  6. 然后我尝试使用#import广告头文件,并将-lfoo添加到其他链接器标志中进行调试和释放(尽管这可能不是正确的),以防万一仍然需要这样做.但是我仍然得到同样的无法建立客观c错误的信息. 我确定我做错了那很明显
  1. Ok so I created a new project called newEncrypt.
  2. I chose not to use the header option as the instructions say this is limited to non Framework apps/
  3. I created a folder inside newEncrypt called CommonCrypto, with a module.map file inside. the contents of which are: module CommonCrypto [system] { header "/usr/include/CommonCrypto/CommonCrypto.h" export * }
  4. added ${SRCROOT}/CommonCrypto to swift compiler-search paths-import paths. Debug and release.
  5. This is where the instructions sort of stop. I assume I need to import CommonCrypto into my class. This error with "could not build objective C module ‘CommonCrypto’. Im also assuming I should have the CommonCrypto library files (from the CommonCryto ‘include’ folder) in "/usr/include/CommonCrypto/CommonCrypto.h" or "/newEncrypt/CommonCrypto/CommonCrypto.h"? I have tried this, but I just get the same errors.
  6. I have then tried to ad a header file with #import and added -lfoo to other linker flags debug and release (although this may not be the correct one) ust in case this could still be required. But I still get the same could not build objective c error. Im sure I am doing something wrong thats obvious

推荐答案

因此,事实证明,我完全使这一过程复杂化了.

So it turns out I was completely overcomplicating this.

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

There is a really useful article already http://www.stackoverflow.dluat.com/questions/31004609/how-to-convert-common-crypto-code-from-objective-c-to-swift

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

I didn't need to import any external libraries or SDKs, all I needed was a bridging header and to #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

    }
}

我希望这对某人有帮助.

I hope this helps someone.

这篇关于MD5 3DES加密Swift的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆