0

I am hoping StackOverFlow comes to the rescue on this one. I am trying to convert/rewrite some iOS code to Kotlin that encrypts a key.

Both seem to be using AES128, ECB and both take NSData/ByteArray.

iOS Code (outputs {length = 16, bytes = 0x62afc569 1dea2367 f0a881d5 f03ea8fd ... d76e9a2b cad89a01})

func encrypt(data:Data, keyData:Data) -> Data {
    let cryptLength  = size_t(data.count + kCCBlockSizeAES128)
    var cryptData = Data(count:cryptLength)
    
    let keyLength = size_t(kCCKeySizeAES128)
    let options   = CCOptions(kCCOptionECBMode | kCCOptionPKCS7Padding)
    
    //default the IV
    let ivBytes:[UInt8] = [00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00 ]
    let ivData = Data(ivBytes)
    
    var numBytesEncrypted :size_t = 0
    
    let cryptStatus = cryptData.withUnsafeMutableBytes {cryptBytes in
        data.withUnsafeBytes {dataBytes in
            ivData.withUnsafeBytes {ivBytes in
                keyData.withUnsafeBytes {keyBytes in
                    CCCrypt(CCOperation(kCCEncrypt),
                            CCAlgorithm(kCCAlgorithmAES128),
                            options,
                            keyBytes, keyLength,
                            ivBytes,
                            dataBytes, data.count,
                            cryptBytes, cryptLength,
                            &numBytesEncrypted)
                }
            }
        }
    }
    
    if UInt32(cryptStatus) == UInt32(kCCSuccess) {
        cryptData.removeSubrange(numBytesEncrypted..<cryptData.count)
        
    } else {
        print("Error: \(cryptStatus)")
    }
    
    return cryptData;
}

In Kotlin (Outputs 62-51-3b691d-162367-10-58-7f-2b-103e-58-36c-400a66-2e-53-7d-1-296e-662b-36-28-6601)

fun encrypt(strToEncrypt: ByteArray, keyBytes: ByteArray): String? {
        Security.addProvider(BouncyCastleProvider())

        try {
            val skey = SecretKeySpec(keyBytes, "AES")

            synchronized(Cipher::class.java) {
                val cipher = Cipher.getInstance("AES/ECB/PKCS7Padding")
                cipher.init(Cipher.ENCRYPT_MODE, skey)

                val cipherText = ByteArray(cipher.getOutputSize(strToEncrypt.size))
                var ctLength = cipher.update(
                    strToEncrypt, 0, strToEncrypt.size,
                    cipherText, 0
                )
                ctLength += cipher.doFinal(cipherText, ctLength)
                Log.i("PIN", "${cipherText.toHexString()}")

                return String(
                    Base64.encode(cipherText, Base64.DEFAULT)
                )
            }
        } catch (uee: UnsupportedEncodingException) {
            uee.printStackTrace()
        } catch (ibse: IllegalBlockSizeException) {
            ibse.printStackTrace()
        } catch (bpe: BadPaddingException) {
            bpe.printStackTrace()
        } catch (ike: InvalidKeyException) {
            ike.printStackTrace()
        } catch (nspe: NoSuchPaddingException) {
            nspe.printStackTrace()
        } catch (nsae: NoSuchAlgorithmException) {
            nsae.printStackTrace()
        } catch (e: ShortBufferException) {
            e.printStackTrace()
        }

        return null
    }
Burf2000
  • 5,001
  • 14
  • 58
  • 117

0 Answers0