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:But 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?
Edit
I 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)