如何快速使用带有密码和模式的mcrypt?

时间:2019-02-13 13:13:44

标签: ios swift encryption mcrypt

我正在尝试在我的Swift代码中使用mcrypt,但是所有方法均失败。 这是用于解密的JS代码:

 decrypt(data: string): Object {
if (typeof data !== 'string') {
  return data;
}
try {
  const bin = this.hex2bin(data);
  const message = mcrypt.Decrypt(bin, this.data.iv, this.data.key, this.data.cipher, this.data.mode);
  data = JSON.parse(atob(message.replace(/[^a-z\d+=/]/gi, '')));
} catch (e) {
  console.error('decryption error', e, data);
}
return data;}

这里是使用CryptoSwift pod作为String结构扩展的代码:

  func aesDecrypt() throws -> String {

    var result = NSString()
    do {
        let dec: [UInt8]
        let key = Array("Nt20K855Lb39Bc71Nt20K855Lb39Bc71".utf8)
        let iv = Array("S49t62H102k71R3a".utf8)
        let data = self.data(using: String.Encoding.utf8)

        if(data != nil) {
            dec = try  AES(key: key, blockMode: CBC(iv: iv), padding: .pkcs7).decrypt(data!.bytes)

        } else {
            dec = []
        }
        let decData = NSData(bytes: dec, length: Int(dec.count))
        result = NSString(data: decData as Data, encoding: String.Encoding.utf8.rawValue)!
    } catch let error as NSError {
        print("\n\nDencryption error\(error.localizedDescription)")
    }
    return String(result)
} 

如何在Swift项目中进行解密?

0 个答案:

没有答案