Skip to content

Commit

Permalink
Initial Commit
Browse files Browse the repository at this point in the history
  • Loading branch information
pavankotesh committed Apr 20, 2020
0 parents commit 19bbfc0
Show file tree
Hide file tree
Showing 2 changed files with 148 additions and 0 deletions.
110 changes: 110 additions & 0 deletions CryptoManager.swift
Original file line number Diff line number Diff line change
@@ -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
}
}
38 changes: 38 additions & 0 deletions ReadMe.md
Original file line number Diff line number Diff line change
@@ -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, [email protected]

## 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.

0 comments on commit 19bbfc0

Please sign in to comment.