0
var cryptor: CValuesRef<CCCryptorRefVar>

使用 kotlin KMM,在项目 iosMain

我无法为密码器初始化数据。

我需要 var 的初始化数据,因为我需要使用 func CCCryptorCreateWithMode

我试试

var cryptor: CValuesRef<CCCryptorRefVar> = memScoped { cValue() }

但是,当我在运行 func CCCryptorCreateWithMode 后获得价值时

cryptor.getPointer(MemScope()).pointed.value

它总是NULL,我需要它有价值

请帮我。

完整代码

fun encryptOrDecryptData(data: NSData, key: Any, iv: Any): NSData? {
        var status: CCCryptorStatus

        if (key is NSData || key is NSString) {
            //validate ok
        } else {
            println("key class type invalid (valid: NSData || NSString)")
            return null
        }

        if (iv is NSData || iv is NSString) {
            //validate ok
        } else {
            println("iv class type invalid (valid: NSData || NSString)")
            return null
        }

        var keyData: NSMutableData?
        var ivData: NSMutableData?

        if (key is NSData) {
            keyData = key.mutableCopy() as NSMutableData?
        } else {
            keyData = (key as NSString).dataUsingEncoding(NSUTF8StringEncoding)?.mutableCopy() as NSMutableData?
        }

        if (iv is NSData) {
            ivData = iv.mutableCopy() as NSMutableData?
        } else {
            ivData = (iv as NSString).dataUsingEncoding(NSUTF8StringEncoding)?.mutableCopy() as NSMutableData?
        }

        var cryptor: CValuesRef<CCCryptorRefVar> = memScoped { cValue() }

        status = CCCryptorCreateWithMode(
            op = kCCEncrypt,
            mode = kCCModeCTR,
            alg = kCCAlgorithmAES128,
            padding = ccNoPadding,
            iv = ivData?.bytes(),
            key = keyData?.bytes(),
            keyLength = 16,
            tweak = null,
            tweakLength = 0,
            numRounds = 0,
            options = kCCModeOptionCTR_BE,
            cryptorRef = cryptor)

        if (status != kCCSuccess) {
            println("ERROR: CCCryptorCreateWithMode error: status = " + status)
            return null
        }

        var cryptorRef = cryptor.getPointer(MemScope()).pointed.value

        if (cryptorRef != null) {
            var result: NSData? = _runCryptor(cryptorRef, data)
            return result
        } else {
            println("ERROR: cryptorRef == null")
            return null
        }
    }
4

0 回答 0