-
Notifications
You must be signed in to change notification settings - Fork 0
/
CryptoSwift.swift
119 lines (88 loc) · 4.47 KB
/
CryptoSwift.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
111
112
113
114
115
116
117
118
119
//
// CypherSwift.swift
// Created by Sofia Swidarowicz on 01/11/15.
// Copyright © 2015 Sofia Swidarowicz. All rights reserved.
//
import UIKit
import Security
class CypherSwift: NSObject {
func printData(data:Data) {
let string1 = String(data: data, encoding: String.Encoding.utf8) ?? "Data could not be printed"
print(string1)
}
func base64String(input : String) -> String?
{
let utf8str = input.data(using: String.Encoding.utf8);
return utf8str?.base64EncodedString()
}
func tripleDesEncrypt(pass: String, base64Key: String) -> String{
if base64Key == ""
{
return ""
}
let keyData : NSData! = Data(base64Encoded: base64Key, options: .init(rawValue: UInt(0))) as NSData!;
let message = pass
let data: NSData! = (message as NSString).data(using: String.Encoding.utf8.rawValue) as NSData!
let cryptData = NSMutableData(length: Int(data.length) + kCCBlockSize3DES)!
let keyLength = size_t(kCCKeySize3DES)
let operation: CCOperation = UInt32(kCCEncrypt)
let algoritm: CCAlgorithm = UInt32(kCCAlgorithm3DES)
let options: CCOptions = UInt32(kCCOptionECBMode + kCCOptionPKCS7Padding)
var numBytesEncrypted :size_t = 0
let cryptStatus = CCCrypt(operation,
algoritm,
options,
keyData.bytes, keyLength,
nil,
data.bytes, data.length,
cryptData.mutableBytes, cryptData.length,
&numBytesEncrypted)
if UInt32(cryptStatus) == UInt32(kCCSuccess) {
cryptData.length = Int(numBytesEncrypted)
// Not all data is a UTF-8 string so Base64 is used
let base64cryptString = cryptData.base64EncodedString(options: .lineLength64Characters)
return base64cryptString;
} else {
print("Error: \(cryptStatus)")
}
return ""
}
func tripleDesDecrypt(pass: String, base64Key: String) -> String{
//help from this thread
//http://stackoverflow.com/questions/25754147/issue-using-cccrypt-commoncrypt-in-swift
let keyData : NSData! = Data(base64Encoded: base64Key, options: .init(rawValue: UInt(0))) as NSData!;
let data = Data(base64Encoded: pass, options: .init(rawValue: UInt(0))) as NSData?
let cryptData = NSMutableData(length: Int((data?.length)!) + kCCBlockSize3DES)!
let keyLength = size_t(kCCKeySize3DES)
let operation: CCOperation = UInt32(kCCDecrypt)
let algoritm: CCAlgorithm = UInt32(kCCAlgorithm3DES)
let options: CCOptions = UInt32(kCCOptionECBMode + kCCOptionPKCS7Padding)
var numBytesEncrypted :size_t = 0
let cryptStatus = CCCrypt(operation,
algoritm,
options,
keyData.bytes, keyLength,
nil,
data?.bytes, (data?.length)!,
cryptData.mutableBytes, cryptData.length,
&numBytesEncrypted)
if UInt32(cryptStatus) == UInt32(kCCSuccess) {
cryptData.length = Int(numBytesEncrypted)
return String(data: cryptData as Data, encoding: .utf8)!
// Not all data is a UTF-8 string so Base64 is used
//var base64cryptString = cryptData.base64EncodedString(options: .lineLength64Characters)
//print("base64cryptString = \(base64cryptString)")
//base64cryptString = base64cryptString + "\n"
//return encodeString(str: base64cryptString)
} else {
print("Error: \(cryptStatus)")
}
return ""
}
func encodeString(str: String) -> String{
let customAllowedSet = NSCharacterSet(charactersIn:"==\n").inverted
let escapedString = str.addingPercentEncoding(withAllowedCharacters: customAllowedSet)
print("escapedString: \(String(describing: escapedString))")
return escapedString!
}
}