---
title: "SecKeyCreateEncryptedData(_:_:_:_:)"
framework: security
role: symbol
role_heading: Function
path: "security/seckeycreateencrypteddata(_:_:_:_:)"
---

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

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

## Declaration

```swift
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 doc://com.apple.security/documentation/Security/SecKeyAlgorithm. You can use the doc://com.apple.security/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.

## Mentioned in

Using Keys for Encryption Generating New Cryptographic Keys

## Return Value

Return Value The encrypted data or NULL on failure. In Objective-C, call the CFRelease function to free the data’s memory when you are done with it.

## Discussion

Discussion You can decrypt this data with the corresponding private key and a call to SecKeyCreateDecryptedData(_:_:_:_:).
