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
|
@objc public func decrypt(source: NSString) -> NSString? {
guard source.length > 0 && self.privateSecKey != nil else {
return ""
}
let data: Data = (NSData(base64Encoded: (source as String), options: NSData.Base64DecodingOptions.ignoreUnknownCharacters)! as Data)
let blockLen = SecKeyGetBlockSize(self.privateSecKey!)
let outBuf = UnsafeMutablePointer<UInt8>.allocate(capacity: blockLen)
defer {
outBuf.deallocate()
}
var outLen: Int = blockLen
var status: OSStatus = noErr
var resData:NSData? = nil
// if #available(iOS 10.0, *) {
// var error: Unmanaged<CFError>?
// resData = SecKeyCreateDecryptedData(self.privateSecKey!, SecKeyAlgorithm.rsaEncryptionPKCS1, data as CFData, &error)
//
// print("res = \(String(describing: error?.takeUnretainedValue().localizedDescription))")
// } else {
// // Fallback on earlier versions
// //let status = SecKeyEncrypt(seckey, SecPadding.PKCS1, chunkData, chunkData.count, outBuf, &encryptedDataLength)
//
// }
data.withUnsafeBytes { (bytes:UnsafePointer<UInt8>) -> Void in
status = SecKeyDecrypt(self.privateSecKey!, SecPadding.PKCS1, bytes, data.count, outBuf, &outLen)
print("status = \(status)")
}
if status == noErr {
resData = NSData(bytes: outBuf, length: outLen)
}
if resData != nil {
return String(data: (resData! as Data), encoding: String.Encoding.utf8) as NSString?
}
return nil
}
|