Skip to content

Instantly share code, notes, and snippets.

@almsx
Created October 24, 2017 00:01
Show Gist options
  • Save almsx/ee21e63427772fea420e42e1f918372f to your computer and use it in GitHub Desktop.
Save almsx/ee21e63427772fea420e42e1f918372f to your computer and use it in GitHub Desktop.
//
// 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