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

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

Encrypts a block of plaintext.

```
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 [`SecPadding`](/documentation/Security/SecPadding). Typically, [`PKCS1`](/documentation/Security/SecPadding/PKCS1) is used, which adds PKCS1 padding before encryption. If you specify [`kSecPaddingNone`](/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 [`SecKeyGetBlockSize(_:)`](/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 [`SecKeyGetBlockSize(_:)`](/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

A result code. See [Security Framework Result Codes](/documentation/Security/security-framework-result-codes).

## Discussion

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

---

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)