Friday, June 28, 2024
HomeiOS DevelopmentAES/GCM/NoPadding encryption in Swift iOS (min deployment 12.1) would not produce anticipated...

AES/GCM/NoPadding encryption in Swift iOS (min deployment 12.1) would not produce anticipated ciphertext size


To make sure that your Swift implementation of AES/GCM/NoPadding encryption matches the habits of your Java implementation, it is important to confirm a number of features, together with the right setup of the important thing, IV, and encryption parameters. Each languages ought to produce the identical ciphertext size when given the identical enter, offered that the AES/GCM setup is an identical.

Here is an in depth step-by-step comparability and setup that will help you obtain constant encryption outcomes between Java and Swift:

Java Code

Let’s first evaluate your Java encryption setup to make sure all parameters are clearly outlined:

import javax.crypto.Cipher;
import javax.crypto.KeyGenerator;
import javax.crypto.SecretKey;
import javax.crypto.spec.GCMParameterSpec;
import javax.crypto.spec.SecretKeySpec;
import java.nio.ByteBuffer;
import java.safety.SecureRandom;

public class AesGcmEncryption {
    non-public static remaining int GCM_IV_LENGTH = 12;
    non-public static remaining int GCM_TAG_LENGTH = 16;
    non-public static remaining int AES_KEY_SIZE = 256;

    public static void most important(String[] args) throws Exception {
        // Generate AES key
        KeyGenerator keyGen = KeyGenerator.getInstance("AES");
        keyGen.init(AES_KEY_SIZE);
        SecretKey key = keyGen.generateKey();

        // Generate IV
        byte[] iv = new byte[GCM_IV_LENGTH];
        SecureRandom random = new SecureRandom();
        random.nextBytes(iv);

        // Message to encrypt
        byte[] messageBytes = "Hiya, World!".getBytes();

        // Encrypt
        Cipher cipher = Cipher.getInstance("AES/GCM/NoPadding");
        GCMParameterSpec spec = new GCMParameterSpec(GCM_TAG_LENGTH * Byte.SIZE, iv);
        cipher.init(Cipher.ENCRYPT_MODE, key, spec);
        byte[] encryptedMessageByte = cipher.doFinal(messageBytes);

        // Mix IV, salt (if any), and ciphertext
        byte[] salt = new byte[16]; // Assuming a 16-byte salt for instance
        random.nextBytes(salt);

        byte[] cipherByte = ByteBuffer.allocate(iv.size + salt.size + encryptedMessageByte.size)
                .put(iv)
                .put(salt)
                .put(encryptedMessageByte)
                .array();

        // Print lengths
        System.out.println("IV Size: " + iv.size);
        System.out.println("Salt Size: " + salt.size);
        System.out.println("Encrypted Message Byte Size: " + encryptedMessageByte.size);
        System.out.println("Cipher Byte Size: " + cipherByte.size);
    }
}

Swift Code

Now, let’s guarantee your Swift code matches the Java setup:

  1. Guarantee AES/GCM Configuration: The important thing, IV, and GCM tag lengths must be constant.
  2. Mix IV, salt, and encrypted message: Ensure that the ultimate concatenated information construction is similar.

Here is the revised Swift code:

import Basis
import CryptoSwift

// Constants
let gcmTagLength = 16
let ivLength = 12
let saltLength = 16

// Generate key (256 bits)
let key = Knowledge((0..<32).map { _ in UInt8.random(in: 0...255) })

// Generate IV
let iv = Knowledge((0..<ivLength).map { _ in UInt8.random(in: 0...255) })

// Message to encrypt
let message = "Hiya, World!".information(utilizing: .utf8)!

// Generate salt
let salt = Knowledge((0..<saltLength).map { _ in UInt8.random(in: 0...255) })

do {
    // Setup GCM
    let gcm = GCM(iv: iv.bytes, additionalAuthenticatedData: nil, tagLength: gcmTagLength)
    let aes = strive AES(key: key.bytes, blockMode: gcm, padding: .noPadding)
    
    // Encrypt
    let encryptedMessage = strive aes.encrypt(message.bytes)
    
    // Mix IV, salt, and encrypted message
    let cipherData = iv + salt + Knowledge(encryptedMessage)
    
    // Print lengths
    print("IV Size: (iv.depend)")
    print("Salt Size: (salt.depend)")
    print("Encrypted Message Size: (encryptedMessage.depend)")
    print("Cipher Knowledge Size: (cipherData.depend)")
    
    // Output cipher information for debugging
    print("Cipher Knowledge: (cipherData.base64EncodedString())")
    
} catch {
    print("Encryption failed: (error)")
}

Key Factors to Confirm

  1. Key Size: Guarantee the important thing size is 256 bits (32 bytes) in each implementations.
  2. IV Size: Make sure the IV size is 12 bytes.
  3. Tag Size: The GCM tag size must be 16 bytes.
  4. Message Encoding: Confirm that the message encoding is similar (e.g., UTF-8).

Troubleshooting

  • Guarantee Constant Libraries: Confirm that the Swift CryptoSwift library is getting used accurately and helps the required AES/GCM performance.
  • Evaluate Outputs: Print the base64-encoded output of the encrypted message in each Java and Swift to confirm they produce the identical outcomes for a similar inputs.
  • Debug Steps: If the lengths differ, break down the method to match intermediate steps such because the encrypted message earlier than concatenating IV and salt.

By intently aligning the parameters and verifying every step, you need to be capable of obtain constant encryption outcomes between your Java and Swift implementations.



Supply hyperlink

RELATED ARTICLES

LEAVE A REPLY

Please enter your comment!
Please enter your name here

- Advertisment -
Google search engine

Most Popular

Recent Comments