-1

I am trying to encrypt in android and decrypt in ios.I am using AES.GCM encryption however when i try to decrypt the package in ios i get Aunthetication faliure message.Below is the code for ios decryption

func decryptData(decryptToData:Data,key:SymmetricKey)->String {
    let combinedData = decryptToData // Previous sealed bo

    let sealedBoxToOpen = try! AES.GCM.SealedBox(combined: decryptToData)

    if let decryptedData = try? AES.GCM.open(sealedBoxToOpen, using: key) {
          decryptedString = String(data: decryptedData, encoding: .utf8)!
        print(decryptedString ?? "Failed")
    } else {   
        print(CryptoKitError)                // Ouch, doSomething() threw an error.
    }
}

This is similar to iOS CryptoKit in Java but i am doing the other way around.

This is the android encryption code

public synchronized Map<String, String> encrypt(byte[] rawKey, byte[] rawData, @Nullable byte[] associatedData) throws StashDataEncryptionException {
    byte[] rawEncryptionKey = null;
    if (rawKey == null) {
        SecureRandom secureRandom = new SecureRandom();
        byte[] key = new byte[KEY_LENGTH_BYTE];
        secureRandom.nextBytes(key);
        rawEncryptionKey = key;
    } else {
        rawEncryptionKey = rawKey;
    }

    byte[] iv = null;
    byte[] encrypted = null;
    try {
        iv = new byte[IV_LENGTH_BYTE];
        secureRandom.nextBytes(iv);

        final Cipher cipherEnc = getCipher();
        cipherEnc.init(Cipher.ENCRYPT_MODE, new SecretKeySpec(rawEncryptionKey, "AES"), new GCMParameterSpec(TAG_LENGTH_BIT, iv));

        if (associatedData != null) {
            cipherEnc.updateAAD(associatedData);
        }

        encrypted = cipherEnc.doFinal(rawData);

        String base64Key = Base64.encodeToString(rawEncryptionKey, Base64.DEFAULT);

        //concat all of it to a single message
        ByteBuffer byteBuffer = ByteBuffer.allocate(1 + iv.length + encrypted.length);
        byteBuffer.put((byte) iv.length);
        byteBuffer.put(iv);
        byteBuffer.put(encrypted);
        byte[] cipherMessage = byteBuffer.array();

        Map map = new HashMap<String, String>();
        map.put(MAP_KEY, base64Key);
        map.put(MAP_Byte_CONTENT, cipherMessage);
        return map;
    } catch (Exception e) {
        throw new StashDataEncryptionException("could not encrypt data", e);
    }
}

i checked the key,iv and tag lenght.Its same on the ios side as android

Razi Tiwana
  • 1,425
  • 2
  • 13
  • 16
md12
  • 111
  • 1
  • 8

2 Answers2

3

Hey there I played very long to day with this and I came up with a working Xcode playground demoing CryptoKit AES-GCM 256 encryption & decryption. I also had this error very often today but could solve it. You can clone my playgrounds repo and try it out, play with it:

https://github.com/Blackjacx/Playgrounds/blob/master/playgrounds/CryptoKit.playground/Contents.swift

blackjacx
  • 9,011
  • 7
  • 45
  • 56
-1

Implementations will differ, enough to invalidate authenticated encryption.

Use an enterprise grade, cross platform compatible library like libsodium.

Woodstock
  • 22,184
  • 15
  • 80
  • 118