import Curve25519Kit import CryptoSwift private extension String { // Convert hex string to Data var hexData: Data { var hex = self var data = Data() while(hex.count > 0) { let subIndex = hex.index(hex.startIndex, offsetBy: 2) let c = String(hex[.. Data? { do { let symmetricKey = self.symmetricKey! return try diffieHellmanEncrypt(plainText: message, symmetricKey: symmetricKey) } catch { Logger.warn("FallBackSessionCipher: Failed to encrypt message") return nil } } /// Decrypt a message /// /// - Parameter message: The message to decrypt /// - Returns: The decrypted message or nil if it failed @objc public func decrypt(message: Data) -> Data? { do { let symmetricKey = self.symmetricKey! return try diffieHellmanDecrypt(cipherText: message, symmetricKey: symmetricKey) } catch { Logger.warn("FallBackSessionCipher: Failed to decrypt message") return nil } } // Encypt the message with the symmetric key and a 16 bit iv private func diffieHellmanEncrypt(plainText: Data, symmetricKey: Data) throws -> Data { let iv = Randomness.generateRandomBytes(ivLength)! let ivBytes = [UInt8](iv) let symmetricKeyBytes = [UInt8](symmetricKey) let messageBytes = [UInt8](plainText) let blockMode = CBC(iv: ivBytes) let aes = try AES(key: symmetricKeyBytes, blockMode: blockMode) let cipherText = try aes.encrypt(messageBytes) let ivAndCipher = ivBytes + cipherText return Data(bytes: ivAndCipher, count: ivAndCipher.count) } // Decrypt the message with the symmetric key private func diffieHellmanDecrypt(cipherText: Data, symmetricKey: Data) throws -> Data { let symmetricKeyBytes = [UInt8](symmetricKey) let ivBytes = [UInt8](cipherText[..