在SWIFT4中将CryptoSwift框架AES加密与ECB和pkcs7一起使用

时间:2019-01-02 11:28:01

标签: swift encryption cryptojs

我正在尝试与CryptoJS加密相同的文本加密。但是这里我与您共享的URL都不相同,我需要在Swift中进行相同的加密。请帮助我,在这里我也附上我的代码。 Demo CryptoJS encryption 在此URL中,我们使用CryptoJSECB进行pkcs7加密。我在ios中执行的代码相同,但不相同

import UIKit
import Foundation
import CryptoSwift
class CryptoViewController: UIViewController {

 let KEY = "0123456789123456"
    override func viewDidLoad() {
        super.viewDidLoad()
         self.aesCBC_Encrypt(AES_KEY: KEY, payload: "Hello World!")
    }
    func aesCBC_Encrypt(AES_KEY: String,payload: String) -> String {
        var result = ""
        do {
            let key: [UInt8] = Array(AES_KEY.utf8) as [UInt8]
            let iv = AES.randomIV(AES.blockSize)
            let bytes = payload.bytes
            let aes = try! AES(key: key, blockMode: ECB(), padding: .pkcs7)
            //let encrypted = try aes.encrypt(Array(self.utf8))
            let encrypted = try aes.encrypt(bytes)
            print("encrypted: \(encrypted)")
            result = encrypted.toHexString()
            print("AES Encryption Result: \(result)")
        } catch {
            print("Error: \(error)")
        }
        return result
    }
}

从我的代码中,我得到这个结果

AES Encryption Result: 1b8019c2add38f33de9099aefd5369f5

但是当我尝试从CryptoJS加密时,我得到的结果

 Encrypted Text:
Aeoo7GP6b4l/Pdxz2RS+qA==

0 个答案:

没有答案