-
Notifications
You must be signed in to change notification settings - Fork 0
/
CryptoManager.swift
110 lines (89 loc) · 3.23 KB
/
CryptoManager.swift
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
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
}
}