Created
October 24, 2017 00:01
-
-
Save almsx/ee21e63427772fea420e42e1f918372f to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// | |
// encriptarInformacion.swift | |
// WalMart | |
// | |
// Created by Alberto Luebbert M. on 26/09/17. | |
// Copyright © 2017 All rights reserved. | |
// | |
import Foundation | |
import CryptoSwift | |
class encriptarInformacion { | |
//IV de Encripción | |
let iv = NSLocalizedString("profile.cadena.segura",comment:"") | |
//Llave de Encripción | |
let datoA16 = NSLocalizedString("profile.cadena.segura",comment:"") | |
func txtNormToEncript(_ txtToEncript: String) -> String { | |
let e_Password: String = txtToEncript | |
let encP = try! e_Password.aesEncrypt(key: datoA16, iv: iv) | |
return encP | |
} | |
func txtEncriptToNorm(_ txtToDecript: String) -> String { | |
let d_Password: String = txtToDecript | |
let desP = try! d_Password.aesDecrypt(key: datoA16, iv: iv) | |
return desP | |
} | |
} | |
extension String{ | |
func aesEncrypt(key: String, iv: String) throws -> String { | |
let data = self.data(using: .utf8)! | |
let encrypted = try! AES(key: key, iv: iv, blockMode: .CBC, padding: PKCS7()).encrypt([UInt8](data)) | |
let encryptedData = Data(encrypted) | |
return encryptedData.base64EncodedString() | |
} | |
func aesDecrypt(key: String, iv: String) throws -> String { | |
let data = Data(base64Encoded: self)! | |
let decrypted = try! AES(key: key, iv: iv, blockMode: .CBC, padding: PKCS7()).decrypt([UInt8](data)) | |
let decryptedData = Data(decrypted) | |
return String(bytes: decryptedData.bytes, encoding: .utf8) ?? "No puedo desencriptar. Seguramente cambio token encripcion p_walmart" | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment