This Github repo - Heimdall should help you generate keys and encrypt your data.
Usage example:
if let heimdall = Heimdall(tagPrefix: "com.example") { let testString = "This is a test string" // Encryption/Decryption if let encryptedString = heimdall.encrypt(testString) { println(encryptedString) // "cQzaQCQLhAWqkDyPoHnPrpsVh..." if let decryptedString = heimdall.decrypt(encryptedString) { println(decryptedString) // "This is a test string" } } // Signatures/Verification if let signature = heimdall.sign(testString) { println(signature) // "fMVOFj6SQ7h+cZTEXZxkpgaDsMrki..." var verified = heimdall.verify(testString, signatureBase64: signature) println(verified) // True // If someone meddles with the message and the signature becomes invalid verified = heimdall.verify(testString + "injected false message", signatureBase64: signature) println(verified) // False }
Data encryption using your own public key:
swift-rsautils btnguyen2k Utilities should help you encrypt your data using your own public key. Its really easy to use.
How to use:
First, just drag and drop the RSAUtils.swift file into your project.
And here it is!
Base line encryption:
let PUBLIC_KEY = "MFwwDQYJKoZIhvcNAQEBBQADSwAwSAJBAJh+/sdLdlVVcM5V5/j/RbwM8SL++Sc3dMqMK1nP73XYKhvO63bxPkWwaY0kwcUU40+QducwjueVOzcPFvHf+fECAwEAAQ==" let sampleText:String = "WHATS UP" let encrypted:NSData? = RSAUtils.encryptWithRSAPublicKey(sampleText.dataUsingEncoding(NSUTF8StringEncoding)!, pubkeyBase64: PUBLIC_KEY, keychainTag: "yourdomain.com") let encryptedDataText = encrypted!.base64EncodedStringWithOptions(NSDataBase64EncodingOptions()) print(encryptedDataText)
Fingerprints:
ML5S87dfDB6l1uHFcACm2IdkGHpDGPUaYoSNTO+83qcWYxTEddFeKhETIcqF5n67nRDL0lKi5XV9uEI7hGTyKA==
source share