如何将Objective-C中的Common加密代码转换为Swift?

时间:2015-06-23 13:48:19

标签: ios objective-c swift swift2

我正致力于将Objective-C Code转换为Swift。在这里,我们发现Swift中没有 CommonCrypto

因此,我们在令人兴奋的Swift项目中使用映射来执行module.map文件并访问Common Crypto文件。我在下面写了Objective-C到Swift的代码。

但是我在代码转换方面遇到了问题。下面我写了两种语言代码。 请注意,此SWIFT代码包含错误 不完美。请帮我解决Swift语言代码。

Objective-C代码:

- (NSData *) hashKey:(NSString *)hash{
    unsigned char result[1000];
    const char *cStr = [hash UTF8String];
    CC_MD5(cStr, (CC_LONG)strlen(cStr), result);

    for (int jval = 0, kval = 16; jval < 8;) {
        result[kval++] = result[jval++];
    }
    return [NSMutableData dataWithBytes:result length:24];;
}

- (NSData *) myEncrypt:(NSString *)encryptData{
    NSData *myKeyData = [self hashKey:MySecretKey];
    NSData *myRawData = [encryptData dataUsingEncoding:NSUTF8StringEncoding];

    size_t buffer_size           = [myRawData length] + kCCBlockSize3DES;
    void* buffer                 = malloc(buffer_size);
    size_t num_bytes_encrypted   = 0;

    uint8_t iv[8] = { 56, 101, 63, 23, 96, 182, 209, 205};

    CCCryptorStatus Crypto_status = CCCrypt(kCCEncrypt, kCCAlgorithm3DES, kCCOptionPKCS7Padding,
                                            [myKeyData bytes],
                                            kCCKeySize3DES,
                                            iv,
                                            [myRawData bytes],
                                            [myRawData length],
                                            buffer,
                                            buffer_size,
                                            &num_bytes_encrypted);

    if (Crypto_status == kCCSuccess){
        NSData *myResult = [NSData dataWithBytes:buffer length:num_bytes_encrypted];
        free(buffer);
        return myResult;
    }
    else {
        free(buffer);
        return nil;
    }
}


- (NSData *) myDecrypt:(NSData *)decryptData{

    NSUInteger mydata_len = [decryptData length];
    NSData *keyData = [self hashKey:MySecretKey];

    size_t buffer_size           = mydata_len + kCCBlockSizeAES128;
    void* buffer                 = malloc(buffer_size);
    size_t num_bytes_encrypted   = 0;

    uint8_t iv[8] = { 56, 101, 63, 23, 96, 182, 209, 205};

    CCCryptorStatus decrypt_status = CCCrypt(kCCDecrypt, kCCAlgorithm3DES, kCCOptionPKCS7Padding,
                                             [keyData bytes],
                                             kCCKeySize3DES,
                                             iv,
                                             [decryptData bytes],
                                             mydata_len,
                                             buffer,
                                             buffer_size,
                                             &num_bytes_encrypted);

    if (decrypt_status == kCCSuccess){
        NSData *myResult = [NSData dataWithBytes:buffer length:num_bytes_encrypted];
        free(buffer);
        return myResult;
    }
    else {
        free(buffer);
        return nil;
    }

}

Swift代码:

func hashKey(hash:String) -> NSData{

    let digestLength = Int(CC_MD5_DIGEST_LENGTH)
    let result = UnsafeMutablePointer<CUnsignedChar>.alloc(digestLength)
    var cStr = hash.cStringUsingEncoding(NSUTF8StringEncoding)
    var strLen = CC_LONG(strlen(cStr!))

    CC_MD5(cStr!, strLen, result)

    var kval = 16

    for (var jval=0; kval==16; jval<8){
    result [kval++] = result[jval++] ***//Here We Got An Error: Cannot assign a value of type 'CUnsignedChar' to a value of type '[Int]'***

    }

    return NSMutableData(bytes: result, length: 24)
}


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

    var myKeyData : NSData = self.hashKey(MySecretKey)
    var myRawData : NSData = encryptData.dataUsingEncoding(NSUTF8StringEncoding)!

    var buffer_size : size_t = myRawData.length + kCCBlockSize3DES
    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]

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

    var Crypto_status: CCCryptorStatus = CCCrypt(operation, algoritm, options, myKeyData.bytes, keyLength, iv, 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)
    return myResult
}else{
    free(buffer)
    return nil
    }

}


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

    var mydata_len : Int = decryptData.length
    var keyData : NSData = self.hashKey(MySecretKey)

    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]

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

    var decrypt_status : CCCryptorStatus = CCCrypt(operation, algoritm, options, keyData.bytes, keyLength, iv, 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)
    return myResult
}else{
    free(buffer)
    return nil

    }
}

5 个答案:

答案 0 :(得分:3)

for (var jval=0; kval==16; jval<8){
    return [kval++] = result[jval++] ***//Here We Got An Error: Cannot assign a value of type 'CUnsignedChar' to a value of type '[Int]'***
}

正如@Francisco所说,这是不正确的。你试图在循环中间返回。这是一个明显的错字。您的意思是result[kval++]而不是return [kval++]

但还有另一个问题。您修改了for循环。那是:

for (int jval = 0, kval = 16; jval < 8;) {

在Swift中基本相同:

for var jval = 0, kval = 0; jval < 8; {

答案 1 :(得分:1)

Swift 2.0

添加Security.framework

#import <CommonCrypto/CommonCrypto.h> (in bridging header)

示例md5方法:

func mac_md5(string string: String) -> (NSData?) {
    let data = (dataString as NSString).dataUsingEncoding(NSUTF8StringEncoding) as NSData!
    let result = NSMutableData(length: Int(CC_MD5_DIGEST_LENGTH))!
    if (data.length > 0) {
        CC_MD5(data.bytes,
            CC_LONG(data.length),
            UnsafeMutablePointer<UInt8>(result.mutableBytes))
    }
    return result
}

测试:

let dataString = "Here is the data longer"
var mac = mac_md5(string:dataString) as! NSMutableData
print("mac: \(mac)")

输出:

mac: <e3b741ae cadf6725 03e3c95c 9d88a9a6>

问题末尾的代码是复制前16个字节末尾的前8个字节,创建一个24字节的3DES密钥(又名2TDEA)。这主要见于在加密方法中实现DES和3DES兼容性的应用程序。

示例代码:

mac.appendData(mac.subdataWithRange(NSMakeRange(0, 8)))
println("mac: \(mac)")

输出:

mac: <e3b741ae cadf6725 03e3c95c 9d88a9a6 e3b741ae cadf6725>

示例3DES方法:

func testCrypt3DES(data data:NSData, keyData:NSData, ivData:NSData, operation:CCOperation) -> NSData? {
    let keyBytes = UnsafePointer<UInt8>(keyData.bytes)
    let ivBytes = UnsafePointer<UInt8>(ivData.bytes)
    let dataLength = Int(data.length)
    let dataBytes  = UnsafePointer<UInt8>(data.bytes)
    let cryptData: NSMutableData! = NSMutableData(length: Int(dataLength) + kCCBlockSize3DES)
    let cryptPointer = UnsafeMutablePointer<UInt8>(cryptData.mutableBytes)
    let cryptLength  = size_t(cryptData.length)

    let keyLength              = size_t(kCCKeySize3DES)
    let algoritm:  CCAlgorithm = UInt32(kCCAlgorithm3DES)
    let options:   CCOptions   = UInt32(kCCOptionPKCS7Padding)

    var numBytesEncrypted :size_t = 0

    let cryptStatus = CCCrypt(operation,
        algoritm,
        options,
        keyBytes, keyLength,
        ivBytes,
        dataBytes, dataLength,
        cryptPointer, cryptLength,
        &numBytesEncrypted)

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

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

    return cryptData;

//测试代码:

let keyString = "!Use a data key!"
let keyData = (keyString as NSString).dataUsingEncoding(NSUTF8StringEncoding) as NSData!

let ivString = "Use a iv"
let ivData = (keyString as NSString).dataUsingEncoding(NSUTF8StringEncoding) as NSData!

let message = "Don´t try to read this text. Top Secret Stuff"
let data = (message as NSString).dataUsingEncoding(NSUTF8StringEncoding) as NSData!

print("data:          \(data)")
if let encryptedData = testCrypt3DES(data:data, keyData:keyData, ivData:ivData, operation:UInt32(kCCEncrypt)) {
    print("encryptedData: \(encryptedData)")
    if let decryptedData = testCrypt3DES(data:encryptedData, keyData:keyData, ivData:ivData, operation:UInt32(kCCDecrypt)) {
        print("decryptedData: \(decryptedData)")
    }
}

输出:

data:          <446f6ec2 b4742074 72792074 6f207265 61642074 68697320 74657874 2e20546f 70205365 63726574 20537475 6666>
encryptedData: <b146c869 df2eec94 fd4656fd 02757e25 718cc32e 35c51907 31866a1c 99f8e2b9 de3ba203 41a49b0f 8fd5dc4c d7721333>
decryptedData: <446f6ec2 b4742074 72792074 6f207265 61642074 68697320 74657874 2e20546f 70205365 63726574 20537475 6666>

答案 2 :(得分:0)

我不确定你想做什么但是这个:

for (int jval = 0, kval = 16; jval < 8;) {
        result[kval++] = result[jval++];
    }

与此不一样:

for (var jval=0; kval==16; jval<8){
    return [kval++] = result[jval++] 
    }

我想你想要做的是:

for (var jval=0; kval==16; jval<8){
    result[kval++] = result[jval++] 
    }

在任何情况下,您都可以将CUnsignedChar(通过示例命名为var)转换为Int:

[String(var).toInt()!]

答案 3 :(得分:0)

使用Swiftify,我得到了以下代码:

func hashKey(hash: String) -> NSData {
    var result: UInt8
    let cStr: Character = hash.UTF8String()
    CC_MD5(cStr, strlen(cStr), result)
    for var jval = 0, kval = 16; jval < 8;  {
        result[kval++] = result[jval++]
    }
    return NSMutableData.dataWithBytes(result, length: 24)
}

func myEncrypt(encryptData: String) -> NSData {
    var myKeyData: NSData = self.hashKey(MySecretKey)
    var myRawData: NSData = encryptData.dataUsingEncoding(NSUTF8StringEncoding)
    var buffer_size: size_t = myRawData.length() + kCCBlockSize3DES
    var buffer: void = malloc(buffer_size)
    var num_bytes_encrypted: size_t = 0
    var iv: uint8_t = 56101632396182209205
    var Crypto_status: CCCryptorStatus = CCCrypt(kCCEncrypt, kCCAlgorithm3DES, kCCOptionPKCS7Padding, myKeyData.bytes(), kCCKeySize3DES, iv, myRawData.bytes(), myRawData.length(), buffer, buffer_size, &num_bytes_encrypted)

  if Crypto_status == kCCSuccess {
    var myResult: NSData = NSData.dataWithBytes(buffer, length: num_bytes_encrypted)
    free(buffer)
    return myResult
  }
  else {
    free(buffer)
    return nil
  }
}

func myDecrypt(decryptData: NSData) -> NSData {
    var mydata_len: UInt = decryptData.length()
    var keyData: NSData = self.hashKey(MySecretKey)
    var buffer_size: size_t = mydata_len + kCCBlockSizeAES128
    var buffer: void = malloc(buffer_size)
    var num_bytes_encrypted: size_t = 0
    var iv: uint8_t = 56101632396182209205

  var decrypt_status: CCCryptorStatus = CCCrypt(kCCDecrypt, kCCAlgorithm3DES, kCCOptionPKCS7Padding, keyData.bytes(), kCCKeySize3DES, iv, decryptData.bytes(), mydata_len, buffer, buffer_size, &num_bytes_encrypted)
  if decrypt_status == kCCSuccess {
    var myResult: NSData = NSData.dataWithBytes(buffer, length: num_bytes_encrypted)
    free(buffer)
    return myResult
  }
  else {
    free(buffer)
    return nil
  }
}

答案 4 :(得分:0)

我建议您使用CryptoSwift。它是用纯粹的快速写的。