From 19bbfc0733eeda99dc1c5e42e7a3c607ff11922c Mon Sep 17 00:00:00 2001 From: Pavan Kotesh Date: Mon, 20 Apr 2020 11:36:57 +0530 Subject: [PATCH] Initial Commit --- CryptoManager.swift | 110 ++++++++++++++++++++++++++++++++++++++++++++ ReadMe.md | 38 +++++++++++++++ 2 files changed, 148 insertions(+) create mode 100644 CryptoManager.swift create mode 100644 ReadMe.md diff --git a/CryptoManager.swift b/CryptoManager.swift new file mode 100644 index 0000000..7c13ed8 --- /dev/null +++ b/CryptoManager.swift @@ -0,0 +1,110 @@ +// +// CryptoManager.swift +// KWSwiftCryptoWrapper +// +// Created by Pavan Kotesh on 28/03/20. +// Copyright © 2020 KWSwiftCryptoWrapper Inc. All rights reserved. +// + +import CryptoSwift +import SwiftyRSA + +class CryptoManager { + static let shared = CryptoManager() + + // MARK: - Public Methods + func generateKeyPair() -> (publicKey: String, privateKey: String) { + do { + let keyPair = try SwiftyRSA.generateRSAKeyPair(sizeInBits: 2048) + let privateKey = keyPair.privateKey + let publicKey = keyPair.publicKey + + let base64EncodedPublicKey = try publicKey.base64String() + let base64EncodedPrivateKey = try privateKey.base64String() + return (base64EncodedPublicKey, base64EncodedPrivateKey) + } catch { + let exception = NSException( + name: NSExceptionName(rawValue: "GenerateRSAKeyPairFailedException"), + reason: error.localizedDescription, + userInfo: nil + ) + + // throw exception + + print("Failed to initialize encryption. Please restart the application to try again.") + return ("", "") + } + } + + func decrypt(_ content: String, withRSA decryptKey: String) -> String? { + do { + let encrypted = try EncryptedMessage(base64Encoded: content) + let privateKey = try PrivateKey(base64Encoded: decryptKey) + let decrypted = try encrypted.decrypted(with: privateKey, padding: .PKCS1) + return decrypted.data.base64EncodedString() + } catch { + let exception = NSException( + name: NSExceptionName(rawValue: "DecryptWithRSAPrivateKeyFailedException"), + reason: error.localizedDescription, + userInfo: nil + ) + + // throw exception + + print(error.localizedDescription) + } + + return content + } + + func encrypt(_ content: String, withAES key: String, iv: String) -> String? { + do { + let keyData = Data(base64Encoded: key) + let ivData = Data(base64Encoded: iv) + + let aes = try AES(key: keyData!.bytes, blockMode: CBC(iv: ivData!.bytes), padding: .pkcs7) + let crypted = try aes.encrypt(content.bytes) + return crypted.toBase64() + } catch { + let exception = NSException( + name: NSExceptionName(rawValue: "EncryptWithAESKeyFailedException"), + reason: error.localizedDescription, + userInfo: nil + ) + + // throw exception + + print(error.localizedDescription) + } + + return content + } + + func decrypt(_ content: String, withAES key: String, iv: String) -> String? { + let encryptedPrefix = SecurityManager.shared.encryptionPrefix + let encrypted = content.deletePrefix(encryptedPrefix) + + do { + let keyData = Data(base64Encoded: key) + let ivData = Data(base64Encoded: iv) + let encryptedData = Data(base64Encoded: encrypted) + + let aes = try AES(key: keyData!.bytes, blockMode: CBC(iv: ivData!.bytes), padding: .pkcs7) + let decrypted = try aes.decrypt(encryptedData!.bytes) + let decryptedContent = String(bytes: decrypted, encoding: .ascii) + return decryptedContent + } catch { + let exception = NSException( + name: NSExceptionName(rawValue: "DecryptWithAESKeyFailedException"), + reason: error.localizedDescription, + userInfo: nil + ) + + // throw exception + + print(error.localizedDescription) + } + + return encrypted + } +} diff --git a/ReadMe.md b/ReadMe.md new file mode 100644 index 0000000..93af042 --- /dev/null +++ b/ReadMe.md @@ -0,0 +1,38 @@ +# KWSwiftCryptoWrapper + +Easy to use Crypto wrapper using CryptoSwift, SwiftyRSA. + +## What can you do easily? + +- Generate public private key pair (RSA) +- Decrypt content with RSA private key +- Encrypt content with AES symmetric key +- Decrypt content with AES symmetric key + +## Dependencies +Add these pods in your podfile to use this wrapper + +- pod 'CryptoSwift' +- pod 'SwiftyRSA' + +## Usage + +The method names are self explanatory. Check CryptoSwiftManager.swift + +## Author + +KeepWorks, ios@keepworks.com + +## Credits + +KWSwiftCryptoWrapper is owned and maintained by [KeepWorks](http://www.keepworks.com/). + +[![N|Solid](http://www.keepworks.com/assets/logo-800bbf55fabb3427537cf669dc8cd018.png)](http://www.keepworks.com/) + +## Contributing + +Bug reports and pull requests are welcome on GitHub at https://github.com/keepworks/KWSwiftCryptoWrapper. + +## License + +KWSwiftCryptoWrapper is available under the [MIT License](http://opensource.org/licenses/MIT). See the [License](https://github.com/keepworks/KWSwiftCryptoWrapper/blob/master/LICENSE) file for more info.