Post

Replies

Boosts

Views

Activity

Swift RSA Public Key Encryption Howto
Hello All,After successfully generating an RSA Public/Private key pairs, I'm now trying to encrypt regular text with either my private or public keys. The app is designed with Swift for OSX 10.11 (not iOS). The signature for my encryption function is as follows:func encryptMessageWithKey(message: String, keyType: KeyType, withCompletionBlock: (success: Bool, data: NSData?, error: Exceptions?) -> Void)I can successfully retreive the SecKeyRef for either my private or public keys. However, I haven't been able to find a Swift reference for how the actual encryption should be done. I have been able to find reference for SecTransform and CMS:https://developer.apple.com/library/mac/documentation/Security/Conceptual/SecTransformPG/EncryptionandDecryption/EncryptionandDecryption.html#//apple_ref/doc/uid/TP40010801-CH3-SW3https://developer.apple.com/library/mac/documentation/Security/Reference/CryptoMessageRef/index.html#//apple_ref/c/func/CMSEncodeContentBut I wasn't able to finalize the implementation with the above. Any pointers to how I can encode a String and get an NSData / String encoded value?EditI got to this point:var sourceData = message.dataUsingEncoding(NSUTF8StringEncoding)! var privKey = self.getPrivateKeyReference() var errorRef: Unmanaged<CFError>? var encoder = SecEncryptTransformCreate(privKey!, &errorRef) SecTransformSetAttribute(encoder, kSecPaddingKey, kSecPaddingPKCS1Key, &errorRef) SecTransformSetAttribute(encoder, kSecTransformInputAttributeName, sourceData, &errorRef) if (errorRef != nil) { let error = errorRef!.takeRetainedValue() print(error) } var encryptedData = SecTransformExecute(encoder, nil)However, there seems to be a problem with sourceData that is causing the application to crash (with: EXEC_BAD_ACCESS) once SecTransformExecute() is executed.It's important to note that I'm using: Xcode Version 7.1 (7B91b)
6
0
9.5k
Dec ’15