本文介绍了CryptoSwift,将Objective-C代码转换成swift的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我真的需要一些帮助,使用 CryptoSwift 将目标c代码转换为swift。
我不知道如何使用如下功能: bzero getCString static NSString * key = @BitCave012345678;
char keyPtr [kCCKeySizeAES128 + 1];
bzero(keyPtr,sizeof(keyPtr));

[key getCString:keyPtr maxLength:sizeof(keyPtr)encoding:NSUTF8StringEncoding];
NSUInteger dataLength = [data length];
size_t bufferSize = dataLength + kCCBlockSizeAES128;
void * buffer = malloc(bufferSize);

size_t numBytesEncrypted = 0;

CCCryptorStatus cryptStatus = CCCrypt(kCCEncrypt,kCCAlgorithmAES128,kCCOptionECBMode,
keyPtr,kCCKeySizeAES128,NULL,[data bytes],dataLength,
buffer,bufferSize,& numBytesEncrypted)

if(cryptStatus == kCCSuccess)
{
return [NSData dataWithBytesNoCopy:buffer length:numBytesEncrypted];
}

free(buffer);

return nil;
}

有没有人有一个想法如何满足需要指针的参数?
我正在尝试使用 UnsafeMutablePointers ,我也尝试了下面的代码,但我知道这是完全错误的:

  var key:NSString =BitCave012345678
var keyPtr:Array< Character> = Array< Character>(count:17,repeatedValue:0)
bzero(& keyPtr,17 * sizeof(Character))
key.getCString(& keyPtr,maxLength:17 * sizeof (字符),编码:NSUTF8StringEncoding)


解决方案

Swift 2.0



没有必要使用bzero,getCString,malloc,这里不是一个例子:



添加项目的安全性

将#import添加到桥接头。

  //操作是'kCCEncrypt'或'kCCDecrypt'
func testCrypt(data data:NSData,keyData:NSData,operation:Int) - > NSData的? {
let keyBytes = UnsafePointer< UInt8>(keyData.bytes)

let dataLength = Int(data.length)
let dataBytes = UnsafePointer< UInt8>(data.bytes)

让cryptData:NSMutableData! = NSMutableData(length:Int(dataLength)+ kCCBlockSizeAES128)
let cryptPointer = UnsafeMutablePointer< UInt8>(cryptData.mutableBytes)
let cryptLength = size_t(cryptData.length)

let keyLength = size_t(kCCKeySizeAES128)
let algorithmit:CCAlgorithm = UInt32(kCCAlgorithmAES128)
let options:CCOptions = UInt32(kCCOptionECBMode + kCCOptionPKCS7Padding)

var numBytesEncrypted:size_t = 0

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

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

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

return cryptData;
}

使用示例:

  let keyData =12345678901234567890123456789012.dataUsingEncoding(NSUTF8StringEncoding)
let messageData =不要尝试阅读这个文本顶级秘密资料.dataUsingEncoding(NSUTF8StringEncoding)
let encrypted = testCrypt(data:messageData !, keyData:keyData !, operation:kCCEncrypt)

这里是Swift的版本数组的UInt8没有 NSData 对象:


$ b $ func testCrypt(data data:[UInt8],keyData:[UInt8],operation:Int) - > [UINT8]? {
let keyBytes = UnsafeMutablePointer< UInt8>(keyData)

让dataLength = data.count
让dataBytes = UnsafeMutablePointer< UInt8&()数据

var cryptData = [UInt8](count:data.count + kCCBlockSizeAES128,repeatedValue:0)
let cryptPointer = UnsafeMutablePointer< UInt8>(cryptData)
let cryptLength = size_t(cryptData.count)

let keyLength = size_t(kCCKeySizeAES128)
let algorithmit:CCAlgorithm = UInt32(kCCAlgorithmAES128)
let options:CCOptions = UInt32(kCCOptionECBMode + kCCOptionPKCS7Padding)

var numBytesEncrypted :size_t = 0

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

如果UInt32(cryptStatus)== UInt32(kCCSuccess) {
cryptData.removeRange(numBytesEncrypted ..< cryptData.count)

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

return cryptData;
}

使用示例:

  let keyData = Array(12345678901234567890123456789012.utf8)
let messageData = Array(不要尝试阅读这个文本顶级秘密的东西.utf8)
let encrypted = testCrypt(data:messageData,keyData:keyData,operation:kCCEncrypt)


I really need some help to convert the objective-c code to swift using CryptoSwift.I'm not sure how to use functions like: bzero, getCString, malloc in Swift.

+(NSData*)encryptData:(NSData*)data
{
    static NSString *key = @"BitCave012345678";
    char keyPtr[kCCKeySizeAES128+1];
    bzero(keyPtr, sizeof(keyPtr));

    [key getCString:keyPtr maxLength:sizeof(keyPtr) encoding:NSUTF8StringEncoding];
    NSUInteger dataLength = [data length];
    size_t bufferSize = dataLength + kCCBlockSizeAES128;
    void *buffer = malloc(bufferSize);

    size_t numBytesEncrypted = 0;

    CCCryptorStatus cryptStatus = CCCrypt(kCCEncrypt, kCCAlgorithmAES128, kCCOptionECBMode,
                                          keyPtr,kCCKeySizeAES128,NULL,[data bytes],dataLength,
                                          buffer, bufferSize, &numBytesEncrypted);

    if(cryptStatus == kCCSuccess)
    {
        return [NSData dataWithBytesNoCopy:buffer length:numBytesEncrypted];
    }

    free(buffer);

    return nil;
}

Does anyone has an idea how to satisfy arguments requiring pointers?I was trying using UnsafeMutablePointers and I also tried the code below, but I know it is completely wrong:

var key: NSString = "BitCave012345678"
        var keyPtr: Array<Character> = Array<Character>(count: 17, repeatedValue: "0")
        bzero(&keyPtr, 17*sizeof(Character))
        key.getCString(&keyPtr, maxLength: 17*sizeof(Character), encoding: NSUTF8StringEncoding)
解决方案

Swift 2.0

It is not necessary to use bzero, getCString, malloc, here is an example that does not:

Add Security.framework to the project
Add #import to the bridging header.

// operation is either 'kCCEncrypt' or 'kCCDecrypt'
func testCrypt(data data:NSData, keyData:NSData, operation:Int) -> NSData? {
    let keyBytes = UnsafePointer<UInt8>(keyData.bytes)

    let dataLength = Int(data.length)
    let dataBytes  = UnsafePointer<UInt8>(data.bytes)

    let cryptData: NSMutableData! = NSMutableData(length: Int(dataLength) + kCCBlockSizeAES128)
    let cryptPointer = UnsafeMutablePointer<UInt8>(cryptData.mutableBytes)
    let cryptLength  = size_t(cryptData.length)

    let keyLength              = size_t(kCCKeySizeAES128)
    let algoritm:  CCAlgorithm = UInt32(kCCAlgorithmAES128)
    let options:   CCOptions   = UInt32(kCCOptionECBMode + kCCOptionPKCS7Padding)

    var numBytesEncrypted :size_t = 0

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

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

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

    return cryptData;
}

Example usage:

let keyData     = "12345678901234567890123456789012".dataUsingEncoding(NSUTF8StringEncoding)
let messageData = "Don´t try to read this text. Top Secret Stuff".dataUsingEncoding(NSUTF8StringEncoding)
let encrypted = testCrypt(data:messageData!, keyData:keyData!, operation:kCCEncrypt)

Here is version in Swift Arrays of UInt8 with no NSData objects:

func testCrypt(data data:[UInt8], keyData:[UInt8], operation:Int) -> [UInt8]? {
    let keyBytes = UnsafeMutablePointer<UInt8>(keyData)

    let dataLength = data.count
    let dataBytes  = UnsafeMutablePointer<UInt8>(data)

    var cryptData = [UInt8](count:data.count+kCCBlockSizeAES128, repeatedValue:0)
    let cryptPointer = UnsafeMutablePointer<UInt8>(cryptData)
    let cryptLength  = size_t(cryptData.count)

    let keyLength              = size_t(kCCKeySizeAES128)
    let algoritm:  CCAlgorithm = UInt32(kCCAlgorithmAES128)
    let options:   CCOptions   = UInt32(kCCOptionECBMode + kCCOptionPKCS7Padding)

    var numBytesEncrypted :size_t = 0

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

    if UInt32(cryptStatus) == UInt32(kCCSuccess) {
        cryptData.removeRange(numBytesEncrypted..<cryptData.count)

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

    return cryptData;
}

Example usage:

let keyData     = Array("12345678901234567890123456789012".utf8)
let messageData = Array("Don´t try to read this text. Top Secret Stuff".utf8)
let encrypted   = testCrypt(data:messageData, keyData:keyData, operation:kCCEncrypt)

这篇关于CryptoSwift,将Objective-C代码转换成swift的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

05-30 12:03