Tuesday, January 30, 2024
HomeiOS DevelopmentiOS the right way to decrypt an encrypted massive file?

iOS the right way to decrypt an encrypted massive file?


This is probably not an iOS query – logic from different platforms might in all probability apply right here too.

On iPhone, I’m efficiently capable of encrypt and decrypt knowledge akin to String, UIImage and many others. utilizing CryptoKit as following:

import CryptoKit

do {
    
    let alicePrivateKey = P521.KeyAgreement.PrivateKey()
    let bobPrivateKey = P521.KeyAgreement.PrivateKey()
    let alicePublicKey = alicePrivateKey.publicKey
    let bobPublicKey = bobPrivateKey.publicKey
    
    let shared_secret_alice = attempt alicePrivateKey.sharedSecretFromKeyAgreement(with: bobPublicKey)
    let shared_secret_bob = attempt bobPrivateKey.sharedSecretFromKeyAgreement(with: alicePublicKey)
    
    guard shared_secret_alice == shared_secret_bob else {
        throw "Shared secret is not the identical between Alice and Bob"
    }
    
    guard let dataToSend = UIImage(named: "picture")?.jpegData(compressionQuality: 1) else {
        return
    }
    
    print("unique decrypted: (dataToSend.hex())")
    
    let alice_key = shared_secret_alice.x963DerivedSymmetricKey(utilizing: SHA512.self , sharedInfo: Information(), outputByteCount: 16)
    let encrypted = attempt encryptedData(decryptedData: dataToSend, key: alice_key)
    
    print("encrypted: (encrypted.hex())")
    
    let bobs_key = shared_secret_bob.x963DerivedSymmetricKey(utilizing: SHA512.self , sharedInfo: Information(), outputByteCount: 16)
    
    let decryptedAgain = attempt decryptData(encryptedData: encrypted, key: bobs_key)
            
    print("decrypted once more: (decryptedAgain.hex())")
    
    print("decrypted is similar as unique decrypted?: (dataToSend.hex() == decryptedAgain.hex())")
    
    guard let decryptedImage = UIImage(knowledge: decryptedAgain) else {
        return
    }
    
    print("decryptedImage: (decryptedImage)")
    
} catch {
    print("Error encrypting: (error)")
}

func encryptedData(decryptedData : Information, key : SymmetricKey) throws -> Information {
    let sealedMessage = attempt AES.GCM.seal(decryptedData, utilizing: key)
    
    guard let encryptedData = sealedMessage.mixed else {
        throw "Error in sealedMessage"
    }
    
    return encryptedData
}

func decryptData(encryptedData : Information, key : SymmetricKey) throws -> Information {
    
    let sealedBoxRecreated = attempt AES.GCM.SealedBox(mixed: encryptedData)
    
    return attempt AES.GCM.open(sealedBoxRecreated, utilizing: key);
}

extension String: LocalizedError {
    public var errorDescription: String? { return self }
}

extension Information {
    func hex() -> String {
        return String(map { String(format: "%02hhx", $0) }.joined().prefix(20)) + "..."
    }
}

My downside is that the above works okay for small pictures or strings however for pictures/movies/information of bigger measurement, it requires me to load your complete knowledge in reminiscence to encrypt and decrypt. This could shortly run out of reminiscence for bigger information. So I’m on the lookout for a greater answer which does not require loading your complete factor in reminiscence.

I did discover the next questions:

IOS – How do I AES decrypt a big file if the file is just too massive to load all of it into reminiscence?

AES encrypting massive information in iOS

However these are from 2012 which is lengthy earlier than fashionable CryptoKit grew to become out there.

  1. Is there some newer approach this got here be executed successfully?

  2. I perceive the idea of breaking the info into smaller chunks, studying the smaller chunks, encrypting them, appending them to a brand new location. Nevertheless, I’m not certain the right way to decrypt it afterwards? What I’m confused about is – within the last file which is the appended encrypted chunks, how ought to I decide every particular person encrypted chunk to decrypt?

For instance:

Shall we say unique file is 123456

I break this down into 6 chunks: 1, 2, 3, 4, 5, 6

I encrypt every of those chunks: hf1d, cd2, 12dm, 7d3, 55gr, 61w3d

I append all of them to create the mixed file: hf1dcd212dm7d355gr61w3d

Now I must decrypt this mixed file.

  1. How ought to I break it down into every chunk for decryption? So far as I’m conscious, the encrypted chunks can fluctuate in size.

Or am I very mistaken in my logic?



Supply hyperlink

RELATED ARTICLES

LEAVE A REPLY

Please enter your comment!
Please enter your name here

- Advertisment -
Google search engine

Most Popular

Recent Comments