Last active
February 6, 2016 17:20
-
-
Save Aaron-A-zz/4e2624e55e3eb377e687 to your computer and use it in GitHub Desktop.
CryptoSwift test code
This file contains 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
let key = "1234567890123456" // key | |
let iv = "1234567890123456" // random | |
let message = textView.text | |
let aes = try! AES(key: key, iv: iv) | |
// encrypt | |
let encrypt = try! aes.encrypt(message.dataUsingEncoding(NSUTF8StringEncoding)!.arrayOfBytes(), padding: PKCS7()) | |
print(encrypt) | |
let encryptedMessage = NSData(bytes: encrypt).base64EncodedStringWithOptions(NSDataBase64EncodingOptions.Encoding64CharacterLineLength) | |
textView.text = encryptedMessage | |
// decrypt | |
let dec = try! aes.decrypt(encrypt, padding: PKCS7()) | |
let str = NSString(data: NSData.withBytes(dec), encoding: NSUTF8StringEncoding) | |
print("revealed message: \(str!)") | |
//PART 2 | |
let concelMessgeTest: String = try! "m;alskjdf;alskjf;aslkjdfa;slfkdjas;ldfkjas;ldfjky".encrypt(AES(key: "secret0key000000", iv: "0123456789012345")) | |
print("base64: \(concelMessgeTest)") | |
//aPf/i9th9iX+vf49eR7PYk2q7S5xmm3jkRLejgzHNJs= looks good | |
var textMessage = "\(concelMessgeTest)" | |
let revealMessgeTest: String = try! textMessage.decrypt(AES(key: "secret0key000000", iv: "0123456789012345")) | |
print("reveal: \(revealMessgeTest)") | |
// fatal error: Array index out of range | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Note: I need to figure out how to take encryptedMessage and turn it back into the same format as encrypt on line 8 which is an arrayOfBytes
If you look at line 16. I'm currently using encrypt and not encryptedMessage