如何将通用加密代码从 Objective-C 转换为 Swift?

How to convert Common crypto code from Objective-C to Swift?

我正在努力将 Objective-C 代码转换为 Swift。这里我们发现 CommonCrypto 在 Swift.

中不可用

因此,我们在我激动人心的 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

    }
}

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

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()!]

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>
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 指出的那样,这是不正确的。您正试图 return 在循环的中间。这是一个明显的错字。你的意思是 result[kval++] 而不是 return [kval++].

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

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

Swift基本相同:

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

使用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
  }
}

我建议您使用 CryptoSwift。它是用纯 swift.

写的