我真的需要一些帮助来将 Objective-c 代码转换为 swift using CryptoSwift
. 我不确定如何在 Swift中使用类似: bzero
, getCString
,的函数。malloc
+(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;
}
有谁知道如何满足需要指针的参数?我正在尝试使用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(Character), encoding: NSUTF8StringEncoding)