gpt4 book ai didi

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

转载 作者:搜寻专家 更新时间:2023-10-31 08:26:37 25 4
gpt4 key购买 nike

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

因此,我们在我激动人心的 Swift 项目中使用映射创建 module.map 文件并访问 Common Crypto 文件。我已经为 Swift 编写了以下 Objective-C 代码。

但是我在代码转换方面遇到了问题。下面我写了两种语言的代码。 请注意此 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 (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; {

关于ios - 如何将通用加密代码从 Objective-C 转换为 Swift?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31004609/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com