<!--
{
  "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/SecKeyCreateRandomKey(_:_:)",
  "metadataVersion" : "0.1.0",
  "role" : "Function",
  "symbol" : {
    "kind" : "Function",
    "modules" : [
      "Security"
    ],
    "preciseIdentifier" : "c:@F@SecKeyCreateRandomKey"
  },
  "title" : "SecKeyCreateRandomKey(_:_:)"
}
-->

# SecKeyCreateRandomKey(_:_:)

Generates a new public-private key pair.

```
func SecKeyCreateRandomKey(_ parameters: CFDictionary, _ error: UnsafeMutablePointer<Unmanaged<CFError>?>?) -> SecKey?
```

## Parameters

`parameters`

A dictionary you use to specify the attributes of the generated keys. See [Key Generation Attributes](/documentation/Security/key-generation-attributes) for details.

`error`

An error reference pointer that [`SecKeyCreateRandomKey(_:_:)`](/documentation/Security/SecKeyCreateRandomKey(_:_:)) populates with a suitable error instance on failure.

## Return Value

The newly generated private key, or `NULL` on failure. In Objective-C, call the <doc://com.apple.documentation/documentation/CoreFoundation/CFRelease> function to free the key when you are done with it.

## Discussion

To get the associated public key, use [`SecKeyCopyPublicKey(_:)`](/documentation/Security/SecKeyCopyPublicKey(_:)). [`SecKeyCreateRandomKey(_:_:)`](/documentation/Security/SecKeyCreateRandomKey(_:_:)) fails and returns [`errSecInteractionNotAllowed`](/documentation/Security/errSecInteractionNotAllowed) if you call it in the background on iPhone or iPad while the device is locked.

---

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)