ios – File Encryption in Swift with AES

[ad_1]

My goal is to encrypt a specific file and reserve it in Paperwork Listing. I might have used Apple Encrypted Archive to encrypt however the file decryption needs to be finished on my server (Java).

The java code I am utilizing to encrypt on the android app.

public static void performEncrypt(File inputFile, File outputFile) {
    attempt {
        String SECRET_KEY = "MY_SECRETE_KEY";
        String SALT = "MY_SALT_KEY";
        // Producing Secrete Key
        byte[] iv = {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0};
        IvParameterSpec ivspec = new IvParameterSpec(iv);
        SecretKeyFactory manufacturing facility = SecretKeyFactory.getInstance("PBKDF2WithHmacSHA1");
        KeySpec spec = new PBEKeySpec(SECRET_KEY.toCharArray(), SALT.getBytes(), 65536, 256);
        SecretKey tmp = manufacturing facility.generateSecret(spec);
        SecretKeySpec secretKey = new SecretKeySpec(tmp.getEncoded(), "AES");
        
        //Studying Enter File Bytes
        byte[] inputBytes = Information.readAllBytes(inputFile.toPath());
        
        Cipher cipher = Cipher.getInstance("AES/CBC/PKCS5Padding");
        cipher.init(Cipher.ENCRYPT_MODE, secretKey, ivspec);
        // Performing Encryption
        byte[] outputBytes = cipher.doFinal(inputBytes);
        
        //Writing to output file
        FileOutputStream outputStream = new FileOutputStream(outputFile);
        outputStream.write(outputBytes);
        outputStream.shut();

    } catch (Exception e) {
        e.printStackTrace();
    }
}

I have been making an attempt to duplicate the above operate is Swift however I’m having hassle discovering the Swift different of those Java Courses. How do I carry out encryption utilizing my salt and secrete key ? The closest article I might discover associated to my difficulty was this, however I could not perceive it.

func documentPicker(_ controller: UIDocumentPickerViewController, didPickDocumentsAt urls: [URL]) {
    let documentDirectory = FileManager.default.urls(for: .documentDirectory,in: .userDomainMask)[0]
    let tempFileName = documentDirectory.appendingPathComponent("temp.enc")
    do{
        let iv: [UInt8] = [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]
        let fileToEncrypt = attempt Information(contentsOf: urls.first!)
        let operations = CCOperation(kCCEncrypt)
        let algorithm = CCAlgorithm(kCCAlgorithmAES)
        let choices = CCOptions(kCCOptionPKCS7Padding)
        

    }catch{
        print("Error")
    }
}

The final possibility for me could be to transform my Java code into Goal-c utilizing j2objc. However as a newbie in iOS that appears a bit painful .

[ad_2]

Leave a Reply

Your email address will not be published. Required fields are marked *