|
| 1 | +// |
| 2 | +// AESCipher.swift |
| 3 | +// MobileBanking |
| 4 | +// |
| 5 | +// Created by Pisal on 6/22/22. |
| 6 | +// Copyright © 2022 WB Finance. All rights reserved. |
| 7 | +// |
| 8 | + |
| 9 | +import Foundation |
| 10 | +import CommonCrypto |
| 11 | + |
| 12 | +public class AESCipher { |
| 13 | + |
| 14 | + private var aesKeySizeInBytes: Int! |
| 15 | + private var aesMode: String! |
| 16 | + private var keyIterationCount: UInt32! |
| 17 | + |
| 18 | + public init(aesKeySizeInBytes: Int, |
| 19 | + aesMode: String, |
| 20 | + keyIterationCount: Int = 100 |
| 21 | + ) { |
| 22 | + self.aesKeySizeInBytes = aesKeySizeInBytes |
| 23 | + self.aesMode = aesMode |
| 24 | + self.keyIterationCount = UInt32(keyIterationCount) |
| 25 | + } |
| 26 | + |
| 27 | + public func encrypt(password: String, message: String) -> AESResult { |
| 28 | + let iv = randomizeBytes(count: kCCBlockSizeAES128) |
| 29 | + let salt = randomizeBytes(count: 8) |
| 30 | + let aesKey = aesKey(forPassword: password, salt: salt) |
| 31 | + let data = message.data(using: .utf8)! |
| 32 | + |
| 33 | + let ciphertext = crypt( |
| 34 | + operation: kCCEncrypt, |
| 35 | + algorithm: kCCAlgorithmAES, |
| 36 | + options: kCCOptionPKCS7Padding, |
| 37 | + key: aesKey, |
| 38 | + initializationVector: iv, |
| 39 | + dataIn: data) |
| 40 | + |
| 41 | + return AESResult( |
| 42 | + salt: salt.toBase64String(), |
| 43 | + iv: iv.toBase64String(), |
| 44 | + encodedData: ciphertext!.toBase64String() |
| 45 | + ) |
| 46 | + } |
| 47 | + |
| 48 | + public func decrypt(password: String, salt: Data, iv: Data, base64CipherText: Data) -> NSDictionary { |
| 49 | + let aesKey = aesKey(forPassword: password, salt: salt) |
| 50 | + let decryptedData = crypt(operation: kCCDecrypt, |
| 51 | + algorithm: kCCAlgorithmAES, |
| 52 | + options: kCCOptionPKCS7Padding, |
| 53 | + key: aesKey, |
| 54 | + initializationVector: iv, |
| 55 | + dataIn: base64CipherText |
| 56 | + ) |
| 57 | + |
| 58 | + let str = String(decoding: decryptedData!, as: UTF8.self) |
| 59 | + return str.toDictionary()! as NSDictionary |
| 60 | + } |
| 61 | + |
| 62 | + |
| 63 | + //======================================================================================== |
| 64 | + //MARK:- Internal |
| 65 | + //======================================================================================== |
| 66 | + private func randomizeBytes(count: Int) -> Data { |
| 67 | + let bytes = UnsafeMutableRawPointer.allocate(byteCount: count, alignment: 1) |
| 68 | + defer { bytes.deallocate() } |
| 69 | + let status = SecRandomCopyBytes(nil, count, bytes) |
| 70 | + assert(status == kCCSuccess) |
| 71 | + return Data(bytes: bytes, count: count) |
| 72 | + } |
| 73 | + |
| 74 | + private func aesKey( |
| 75 | + forPassword password: String, |
| 76 | + salt: Data |
| 77 | + ) -> Data { |
| 78 | + let derivedKey = NSMutableData(length: aesKeySizeInBytes * 2)! |
| 79 | + let result = CCKeyDerivationPBKDF( |
| 80 | + CCPBKDFAlgorithm(kCCPBKDF2), |
| 81 | + password, |
| 82 | + password.lengthOfBytes(using: .utf8), |
| 83 | + salt.bytes, |
| 84 | + salt.count, |
| 85 | + CCPseudoRandomAlgorithm(kCCPRFHmacAlgSHA1), |
| 86 | + self.keyIterationCount, |
| 87 | + derivedKey.mutableBytes, |
| 88 | + derivedKey.length |
| 89 | + ) |
| 90 | + assert(result == kCCSuccess) |
| 91 | + return derivedKey.copy() as! Data |
| 92 | + } |
| 93 | + |
| 94 | + private func crypt( |
| 95 | + operation: Int, |
| 96 | + algorithm: Int, |
| 97 | + options: Int, |
| 98 | + key: Data, |
| 99 | + initializationVector iv: Data, |
| 100 | + dataIn: Data |
| 101 | + ) -> Data? { |
| 102 | + return key.withUnsafeBytes { keyUnsafeRawBufferPointer in |
| 103 | + return dataIn.withUnsafeBytes { dataInUnsafeRawBufferPointer in |
| 104 | + return iv.withUnsafeBytes { ivUnsafeRawBufferPointer in |
| 105 | + // Give the data out some breathing room for PKCS7's padding. |
| 106 | + let dataOutSize: Int = dataIn.count + kCCBlockSizeAES128 * 2 |
| 107 | + let dataOut = UnsafeMutableRawPointer.allocate(byteCount: dataOutSize, |
| 108 | + alignment: 1) |
| 109 | + defer { dataOut.deallocate() } |
| 110 | + var dataOutMoved: Int = 0 |
| 111 | + let status = CCCrypt( |
| 112 | + CCOperation(operation), |
| 113 | + CCAlgorithm(algorithm), |
| 114 | + CCOptions(options), |
| 115 | + keyUnsafeRawBufferPointer.baseAddress, |
| 116 | + key.count, |
| 117 | + ivUnsafeRawBufferPointer.baseAddress, |
| 118 | + dataInUnsafeRawBufferPointer.baseAddress, |
| 119 | + dataIn.count, |
| 120 | + dataOut, |
| 121 | + dataOutSize, |
| 122 | + &dataOutMoved |
| 123 | + ) |
| 124 | + guard status == kCCSuccess else { return nil } |
| 125 | + return Data(bytes: dataOut, count: dataOutMoved) |
| 126 | + } |
| 127 | + } |
| 128 | + } |
| 129 | + } |
| 130 | +} |
0 commit comments