Skip to content

Instantly share code, notes, and snippets.

What would you like to do?
Obfuscator for Swift
// Obfuscator.swift
// SwiftObfuscatorExample
// Created by Mathieu White on 2016-07-03.
// Copyright © 2016 Mathieu White. All rights reserved.
import Foundation
class Obfuscator: AnyObject {
// MARK: - Variables
/// The salt used to obfuscate and reveal the string.
private var salt: String = ""
// MARK: - Initialization
init(withSalt salt: [AnyObject]) {
self.salt = salt.description
// MARK: - Instance Methods
This method obfuscates the string passed in using the salt
that was used when the Obfuscator was initialized.
- parameter string: the string to obfuscate
- returns: the obfuscated string in a byte array
func bytesByObfuscatingString(string: String) -> [UInt8] {
let text = [UInt8](string.utf8)
let cipher = [UInt8](self.salt.utf8)
let length = cipher.count
var encrypted = [UInt8]()
for t in text.enumerate() {
encrypted.append(t.element ^ cipher[t.index % length])
print("Salt used: \(self.salt)\n")
print("Swift Code:\n************")
print("// Original \"\(string)\"")
print("let key: [UInt8] = \(encrypted)\n")
return encrypted
This method reveals the original string from the obfuscated
byte array passed in. The salt must be the same as the one
used to encrypt it in the first place.
- parameter key: the byte array to reveal
- returns: the original string
func reveal(key: [UInt8]) -> String {
let cipher = [UInt8](self.salt.utf8)
let length = cipher.count
var decrypted = [UInt8]()
for k in key.enumerate() {
decrypted.append(k.element ^ cipher[k.index % length])
return String(bytes: decrypted, encoding: NSUTF8StringEncoding)!
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment