https://kotlinlang.org logo
#multiplatform
Title
# multiplatform
t

Thanh nguyen

03/12/2024, 3:54 AM
Copy code
@Suppress("CAST_NEVER_SUCCEEDS")
fun String.nsdata(): NSData? {
    return (this as NSString).dataUsingEncoding(NSUTF8StringEncoding)
}

fun String.toUTF8String(): String {
    val bytes = this.toByteArray(Charsets.UTF_8)
    return bytes.decodeToString()
}
@OptIn(ExperimentalEncodingApi::class)
actual fun tripleDesEncryptString(input: String, keyRd: String): String? {
    val inputData = input.nsdata()
    val keyData = keyRd.nsdata()
    val outLength = cValue<ULongVarOf<size_t>>()
    val outputData = NSMutableData.dataWithLength(inputData?.length?.plus(kCCBlockSize3DES) ?: 0uL)

    memScoped {
        val result = CCCrypt(
            op = kCCEncrypt,
            alg = kCCAlgorithm3DES,
            options = (kCCOptionPKCS7Padding or kCCOptionECBMode),
            key = keyData?.bytes,
            keyLength = keyData?.length ?: 0uL,
            iv = null,
            dataIn = inputData?.bytes,
            dataInLength = inputData?.length ?: 0uL,
            dataOut = outputData?.mutableBytes,
            dataOutAvailable = outputData?.length ?: 0uL,
            dataOutMoved = outLength
        )

        if (result != kCCSuccess) {
            return null
        }
        outputData?.setLength(outLength.size.toULong())
    }
    return outputData?.base64EncodedStringWithOptions(options = 0u)
}
I used algorim Triple Des and I try convert swift code to kotlin multilplaform. but outputData wrong. Has anyone ever come across this problem? Hoping for your help
2 Views