---
title: "SecKeyEncrypt(_:_:_:_:_:_:)"
framework: security
role: symbol
role_heading: Function
path: "security/seckeyencrypt(_:_:_:_:_:_:)"
---

# SecKeyEncrypt(_:_:_:_:_:_:)

Encrypts a block of plaintext.

## Declaration

```swift
func SecKeyEncrypt(_ key: SecKey, _ padding: SecPadding, _ plainText: UnsafePointer<UInt8>, _ plainTextLen: Int, _ cipherText: UnsafeMutablePointer<UInt8>, _ cipherTextLen: UnsafeMutablePointer<Int>) -> OSStatus
```

## Parameters

- `key`: Public key with which to encrypt the data.
- `padding`: The type of padding to use. Possible values are listed in doc://com.apple.security/documentation/Security/SecPadding. Typically, doc://com.apple.security/documentation/Security/SecPadding/PKCS1 is used, which adds PKCS1 padding before encryption. If you specify doc://com.apple.security/documentation/Security/SecPadding/kSecPaddingNone, the data is encrypted as-is.
- `plainText`: The data to encrypt.
- `plainTextLen`: Length in bytes of the data in the plainText buffer. This must be less than or equal to the value returned by the doc://com.apple.security/documentation/Security/SecKeyGetBlockSize(_:) function. When PKCS1 padding is performed, the maximum length of data that can be encrypted is 11 bytes less than the value returned by the doc://com.apple.security/documentation/Security/SecKeyGetBlockSize(_:) function (secKeyGetBlockSize() - 11).
- `cipherText`: On return, the encrypted text.
- `cipherTextLen`: On entry, the size of the buffer provided in the cipherText parameter. On return, the amount of data actually placed in the buffer.

## Return Value

Return Value A result code. See Security Framework Result Codes.

## Discussion

Discussion The input buffer (plainText) can be the same as the output buffer (cipherText) to reduce the amount of memory used by the function.
