<!--
{
  "availability" : [
    "iOS: 10.0.0 -",
    "iPadOS: 10.0.0 -",
    "macCatalyst: 13.1.0 -",
    "macOS: 10.12.0 -",
    "tvOS: 10.0.0 -",
    "visionOS: 1.0.0 -",
    "watchOS: 3.0.0 -"
  ],
  "documentType" : "symbol",
  "framework" : "Security",
  "identifier" : "/documentation/Security/SecKeyCreateEncryptedData(_:_:_:_:)",
  "metadataVersion" : "0.1.0",
  "role" : "Function",
  "symbol" : {
    "kind" : "Function",
    "modules" : [
      "Security"
    ],
    "preciseIdentifier" : "c:@F@SecKeyCreateEncryptedData"
  },
  "title" : "SecKeyCreateEncryptedData(_:_:_:_:)"
}
-->

# SecKeyCreateEncryptedData(_:_:_:_:)

Encrypts a block of data using a public key and specified algorithm.

```
func SecKeyCreateEncryptedData(_ key: SecKey, _ algorithm: SecKeyAlgorithm, _ plaintext: CFData, _ error: UnsafeMutablePointer<Unmanaged<CFError>?>?) -> CFData?
```

## Parameters

`key`

The public key to use to perform the encryption.

`algorithm`

The encryption algorithm to use. Use one of the encryption algorithms listed in [`SecKeyAlgorithm`](/documentation/Security/SecKeyAlgorithm). You can use the [`SecKeyIsAlgorithmSupported(_:_:_:)`](/documentation/Security/SecKeyIsAlgorithmSupported(_:_:_:)) function to test that the key is suitable for the algorithm.

`plaintext`

The data to be encrypted.

`error`

The address of a <doc://com.apple.documentation/documentation/CoreFoundation/CFError> object. If an error occurs, this is set to point at an error instance that describes the failure.

## Return Value

The encrypted data or `NULL` on failure. In Objective-C, call the <doc://com.apple.documentation/documentation/CoreFoundation/CFRelease> function to free the data’s memory when you are done with it.

## Discussion

You can decrypt this data with the corresponding private key and a call to [`SecKeyCreateDecryptedData(_:_:_:_:)`](/documentation/Security/SecKeyCreateDecryptedData(_:_:_:_:)).

---

Copyright &copy; 2026 Apple Inc. All rights reserved. | [Terms of Use](https://www.apple.com/legal/internet-services/terms/site.html) | [Privacy Policy](https://www.apple.com/privacy/privacy-policy)