Home > Net >  ECIES encryption and EC key generation in Swift
ECIES encryption and EC key generation in Swift

Time:04-07

Backend uses this java implementation for encrypting the data using public key(generated from iOS App in swift),

        Cipher iesCipher = Cipher.getInstance("ECIESwithAES-CBC");
        byte[] derivation = Hex.decode(derivationString);
        byte[] encoding = Hex.decode(encodingString);
        byte[] nonce = Hex.decode(nonceString);
        IESParameterSpec params = new IESParameterSpec(derivation, encoding, 128, 128, nonce, true);
        iesCipher.init(Cipher.ENCRYPT_MODE, publicKey, params);
        byte[] ciphertext = iesCipher.doFinal(data.getBytes());

But in swift I could not find any equivalent library to decrypt it. I am using the SwiftECC for generating the EC key-pair and sending the public key to server. Then server encrypts the data using that public key as mentioned in above implementation code. But SwiftECC has no any decrypt function which accepts the arguments like derivation, encoding, nonce. And I could not find anything similar to above java implementation in swift.

CodePudding user response:

ECIES is using ephemeral static-ECDH key derivation encryption, where the public key of the ephemeral key pair is stored with the ciphertext. So if you find ECDH the right key derivation AES-CBC encryption (probably using PKCS#7 padding) then you'd be in business. Apparently BC uses KDF2 with SHA-1 for key derivation. I've asked and answered what KDF2 is here.

CodePudding user response:

I’ve implemented ECIES in EllipticCurveKit, feel free to use it as inspiration. Should be straightening enough to port it to SwiftECC if you don’t want to use EllipticCurveKits EC implementation.

  • Related