Hi everyone, I’m currently stumbling across an iss...
# android
m
Hi everyone, I’m currently stumbling across an issue handling decrypting AES encrypted strings from iOS. Currently the flow would be something like: 1. User on iOS App encrypts data with Common Crypto framework and saves it to back end 2. Android app pulls that encrypted data from endpoint and decrypts it I ensured that on Android side we’re using the same padding and transformations as iOS. I’m currently able to decrypt half of the string but losing data in the first half. I was wondering if it was because of the encrypted string wrapping/newlining and tried a few methods of handling it but to no avail. Can anybody help point me in the right direction?
iOS Encrypted String
Copy code
"yxgNZLv27IP00rtbuy+wAAVT4U6By+u7J1j8Q/QO8Q30+wsjjRruuDorN3Kpbe2E::HSIK62Vp2iLLrAYZvSyvY0e5upEMMHnFMnactC/YMFw="
Android Decryption Result
Copy code
J8CD]KyU1394764",
      "recordingId" : "abcdefg",
      "classId" : "abcdefg"
    }
Expected Result
Copy code
{
      "userId": abcedfg",
      "recordingId" : "abcdefg",
      "classId" : "abcdefg"
    }
Decryption Function
Copy code
fun String.decryptWithAES(key: String, iv: String): String? {
    Security.addProvider(BouncyCastleProvider())

    val skey = SecretKeySpec(key.toByteArray(Charsets.UTF_8), "AES")
    val ivKey = ByteArray(iv.toByteArray(Charsets.UTF_8).size)
    val input = android.util.Base64.decode(this.toByteArray(Charsets.UTF_8), android.util.Base64.CRLF or android.util.Base64.NO_WRAP)

    synchronized(Cipher::class.java) {
        val cipher = Cipher.getInstance("AES/CBC/PKCS5Padding")
        cipher.init(Cipher.DECRYPT_MODE, skey, IvParameterSpec(ivKey))
        val decryptedString = String(cipher.doFinal(input))
        return decryptedString.trim { it <= ' ' }
    }
}
s
@mng Base64 functions may generate different values on Android and iOS. it does not seem to be related encryption, because even 1 bit of change will produce different result completely.
m
Thank you for the response @Sinan Gunes. For my use case, would it be best to have a shared kotlin multiplatform library for encrypting/decrypting?
s
¯\_(ツ)_/¯
m
😂
s
I don’t know details of your use case. but AES should be standard between platforms unlike Base64 implementations.
m
I thought the most peculiar thing was that I was able to decrypt half of the input as shown above
I’m not too sure what happened in the first half
s
Base64 happened. AES is good.
try to compare these two:
Copy code
Data("Hello World{}".utf8).base64EncodedString()
and
Copy code
Base64.encodeToString("Hello World{}".toByteArray(Charsets.UTF_8))
m
What class type is
Data
?
s
it is Swift vs Kotlin. I don’t know details. of swift part
m
ah i seee
let me try really quick
Both of those are equal
both produce
Copy code
SGVsbG8gV29ybGR7fQ==
s
can you try
Copy code
{
      "userId": abcedfg",
      "recordingId" : "abcdefg",
      "classId" : "abcdefg"
    }
as String?
m
both are slightly different, i’m not sure if it’s because i’m doing multiline string in Swift
Copy code
KOTLIN
ewogICAgICBcInVzZXJJZFwiOiBcImFiY2VkZmdcIiwKICAgICAgXCJyZWNvcmRpbmdJZFwiIDogXCJhYmNkZWZnXCIsCiAgICAgIFwiY2xhc3NJZFwiIDogXCJhYmNkZWZnXCIKICAgIH0=

SWIFT
ewogICJ1c2VySWQiOiAiYWJjZWRmZyIsCiAgInJlY29yZGluZ0lkIiA6ICJhYmNkZWZnIiwKICAiY2xhc3NJZCIgOiAiYWJjZGVmZyIKfQ==
s
difference is the escapes on Kotlin code
what about
Copy code
.trim { it <= ' ' }
why do you need it?
m
ah let me try removing
same results but i suppose i don’t really need it
one thing to note in the encrypted String above, i’m supposed to replace instances of
::
with
\r\n
not sure if that’s part of the issue as well
s
I don’t know its relation.