---
title: "encrypt(_:algorithm:completion:)"
framework: localauthentication
role: symbol
role_heading: Instance Method
path: "localauthentication/lapublickey/encrypt(_:algorithm:completion:)"
---

# encrypt(_:algorithm:completion:)

Encrypts the data you supply with a given algorithm.

## Declaration

```swift
func encrypt(_ data: Data, algorithm: SecKeyAlgorithm, completion handler: @escaping @Sendable (Data?, (any Error)?) -> Void)
```

```swift
func encrypt(_ data: Data, algorithm: SecKeyAlgorithm) async throws -> Data
```

## Parameters

- `data`: The data to encrypt.
- `algorithm`: The algorithm to use to encrypt the data.
- `handler`: A completion handler to call when the encryption operation completes.

## Discussion

Discussion important: You can call this method from synchronous code using a completion handler, as shown on this page, or you can call it as an asynchronous method that has the following declaration: func encrypt(_ data: Data, algorithm: SecKeyAlgorithm) async throws -> Data For information about concurrency and asynchronous code in Swift, see Calling Objective-C APIs Asynchronously.

## See Also

### Performing cryptographic operations

- [exportBytes(completion:)](localauthentication/lapublickey/exportbytes(completion:).md)
- [verify(_:signature:algorithm:completion:)](localauthentication/lapublickey/verify(_:signature:algorithm:completion:).md)
