Post

Swift里Data加解密

其他测试代码

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
import Foundation
import GoogleUtilities
import CryptoSwift
import SwiftyRSA

class DataCryptTool {
    static func test() {
        let data = "123456".data(using: .utf8)
        let data1 = data?.gzip
        let data2 = data1?.gUnZip
        let data3 = data?.AESCrypt(key: "1234567890123456", padding: .pkcs5Padding, isEncode: true)
        let data4 = data3?.AESCrypt(key: "1234567890123456", padding: .pkcs5Padding, isEncode: false)
        print(data?.bString ?? "")
        print(data1?.bString ?? "")
        print(data2?.bString ?? "")
        print(data3?.bString ?? "")
        print(data4?.bString ?? "")
    }
}
enum AESPaddingType {
    case pkcs7Padding
    case pkcs5Padding
    
    var aesVal: CryptoSwift.Padding {
        switch self {
        case .pkcs5Padding:
            return .pkcs5
        case .pkcs7Padding:
            return .pkcs7
        }
    }
}
extension Data {
    var gzip: Data? {
        do {
            let result =  try NSData.gul_data(byGzippingData: self)
            return result
        } catch let error {
            print(error)
        }
        return nil
    }
    var gUnZip: Data? {
        do {
            let result =  try NSData.gul_data(byInflatingGzippedData: self)
            return result
        } catch let error {
            print(error)
        }
        return nil
    }
    // AES/ECB/Padding7
    // AES/ECB/Padding5
    func AESCrypt(key: String, padding: AESPaddingType, isEncode: Bool) -> Data? {
        do {
            let setting = try AES(key: key.bytes, blockMode: ECB(), padding: padding.aesVal)
            let result = try isEncode ? setting.encrypt(self.bytes) : setting.decrypt(self.bytes)
            return result.data
        } catch let error {
            print(error)
        }
        return nil
    }
    public typealias RSAPadding = SecPadding
    static func RSACrypt(val: String, key: String, padding: RSAPadding, isEncode: Bool) -> String? {
        guard let keyData = key.data(using: .utf8) else {
            return nil
        }
        do {
            if isEncode {
                // 公钥加密
                let publicKey = try PublicKey(base64Encoded: key)
                let message = try ClearMessage(string: val, using: .utf8)
                let data = try message.encrypted(with: publicKey, padding: padding)
                return data.base64String
            } else {
                // 私钥解密
                let privateKey = try PrivateKey(base64Encoded: key)
                let message = try EncryptedMessage(base64Encoded: val)
                let data = try message.decrypted(with: privateKey, padding: padding)
                return data.base64String
            }
        } catch let error {
            print(error)
        }
        return nil
    }
}


This post is licensed under CC BY 4.0 by the author.