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.
@sketchyTech It would be interesting to see your text encoding explanation combined with XOR to demonstrate a simple cypher text in Swift.
— Chromophore (@ChromophoreApp) October 17, 2014
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) | |||||||
0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 |
Enter cipher text here (limit 8 char.)
Bytes (decimal) | |||||||
0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 |
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!
what is enumerate(text)
ReplyDeleteenumerate(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() {
Deletedecrypted.append(e ^ cipher[i])
}
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
ReplyDeleteTry this code:
Deletefunc 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")
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