Bytes for Beginners: XOR encryption and decryption (interactive example)



Two-way encryption

Following a request on Twitter, I was persuaded to extend the recent XOR and Unicode posts to provide an example of XOR encryption. This is a two-way process, where given a cipher (or encryption key) a piece of text can be encrypted and decrypted quite simply.


To encrypt a string it is necessary to simply XOR (^) the array of bytes that represent it. And the only step required to decrypt an encoded string of bytes is to repeat the exact same XOR (with the cipher) as was carried out with the original string.

Interactive example

Enter text here (limit 8 char.)

Bytes (decimal)

00000000

Enter cipher text here (limit 8 char.)


Bytes (decimal)

00000000






What you see following encryption is an array of bytes in the text box. This is the XOR result of the byte representation of your two strings (text and cipher).

Tip: Once the string is encrypted try changing the encrypted bytes or the cipher and then pressing decrypt. This will break the decryption, while maintaining all as it is following encryption will provide a perfect decryption.

In Swift

In Swift we could do the following to achieve the same encryption and decryption:
let text = [UInt8]("hello!!!".utf8)
let cipher = [UInt8]("goodbye!".utf8)

var encrypted = [UInt8]()

// encrypt bytes
for t in text.enumerate() {
    encrypted.append(t.element ^ cipher[t.index])
}

var decrypted = [UInt8]()

// decrypt bytes
for t in encrypted.enumerate() {
    decrypted.append(t.element ^ cipher[t.index])
}

String(bytes: decrypted, encoding: NSUTF8StringEncoding) // hello!!!
Any questions, please ask below or on twitter.

Fun time!

Now the basics have been explained, I thought it was time to have some fun and lift the character limits on the encryption. Clicking this supercharge button will increase the limit to 80 characters in the text and cipher boxes above.


Anything you encrypt can be decrypted by anyone using the cipher text and the byte array, so happy sharing!

Endorse on Coderwall

Comments

  1. Replies
    1. enumerate(text) has changed to text.enumerate() in the latest version of Swift/Xcode. For each item in an array it returns a sequence of pairs from which we can extract not only the element but also its index: (index, element). Another way to write the statements would be: for (i, e) in encrypted.enumerate() {
      decrypted.append(e ^ cipher[i])
      }

      Delete
  2. Hey i want encryption and decryption of a string of password and upper code is giving me array of numbers at index path could you provide code for encryption and decryption of string in swift

    ReplyDelete
    Replies
    1. Try this code:

      func xor(text:String, cipher:String) -> String {
      let textBytes = [UInt8](text.utf8)
      var cipherBytes = [UInt8](cipher.utf8)
      var encrypted = [UInt8]()

      if cipherBytes.count < textBytes.count {
      let cipherExtension = [UInt8](count:textBytes.count - cipherBytes.count, repeatedValue:1)
      cipherBytes.appendContentsOf(cipherExtension)
      }
      // encrypt bytes
      for t in textBytes.enumerate() {
      encrypted.append(t.element ^ cipherBytes[t.index])
      }
      return String(bytes: encrypted, encoding: NSUTF8StringEncoding)! // hello!!!

      }

      let encrypted = xor("Hello Swift", cipher: "goodbye")
      xor(encrypted, cipher: "goodbye")

      Delete
    2. The problem arises with this method where bytes are outside the UTF8 range of characters. You might therefore wish to look at my latest post about CommonCrypto.

      Delete

Post a Comment