You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Returns the public portion of a certificate in binary format. This function takes a certificate ID and returns the encoded certificate. The binary result can be passed to **CREATE CERTIFICATE … WITH BINARY** to create a new certificate.
32
+
This function returns the public portion of a certificate in binary format. This function takes a certificate ID as an argument, and returns the encoded certificate. To create a new certificate, pass the binary result to **CREATE CERTIFICATE … WITH BINARY**.
33
33
34
34
## Syntax
35
35
@@ -39,39 +39,39 @@ CERTENCODED ( cert_id )
39
39
40
40
## Arguments
41
41
*cert_id*
42
-
Is the **certificate_id** of the certificate. This is available from sys.certificates or by using the [CERT_ID (Transact-SQL)](../../t-sql/functions/cert-id-transact-sql.md) function. *cert_id*is type **int**
42
+
The **certificate_id** of the certificate. Find this value in sys.certificates; the [CERT_ID (Transact-SQL)](../../t-sql/functions/cert-id-transact-sql.md) function will return it as well. *cert_id*has data type **int**.
43
43
44
44
## Return types
45
45
**varbinary**
46
46
47
47
## Remarks
48
-
**CERTENCODED** and **CERTPRIVATEKEY**are used together to returndifferent portions of a certificate in binary form.
48
+
Use **CERTENCODED** and **CERTPRIVATEKEY** together to return, in binary form, different portions of a certificate.
49
49
50
50
## Permissions
51
-
**CERTENCODED** is available to public.
51
+
**CERTENCODED** is publicly available.
52
52
53
53
## Examples
54
54
55
55
### Simple Example
56
-
The following example creates a certificate named `Shipping04` and then uses the **CERTENCODED** function to return the binary encoding of the certificate.
56
+
This example creates a certificate named `Shipping04`, and then uses the **CERTENCODED** function to return the binary encoding of the certificate. This example sets the certificate expiry date to October 31, 2040.
57
57
58
58
```sql
59
-
CREATEDATABASETEST1;
60
-
GO
61
-
USE TEST1
62
-
CREATE CERTIFICATE Shipping04
63
-
ENCRYPTION BY PASSWORD ='pGFD4bb925DGvbd2439587y'
64
-
WITH SUBJECT ='Sammamish Shipping Records',
65
-
EXPIRY_DATE ='20161031';
66
-
GO
67
-
SELECT CERTENCODED(CERT_ID('Shipping04'));
59
+
CREATEDATABASETEST1;
60
+
GO
61
+
USE TEST1
62
+
CREATE CERTIFICATE Shipping04
63
+
ENCRYPTION BY PASSWORD ='pGFD4bb925DGvbd2439587y'
64
+
WITH SUBJECT ='Sammamish Shipping Records',
65
+
EXPIRY_DATE ='20401031';
66
+
GO
67
+
SELECT CERTENCODED(CERT_ID('Shipping04'));
68
68
69
69
```
70
70
71
71
### B. Copying a Certificate to Another Database
72
-
The following more complicated example, creates two databases, `SOURCE_DB` and `TARGET_DB`. The goal is to create a certificate in the `SOURCE_DB`, and then copy the certificate to the `TARGET_DB`, and then demonstrate that data encrypted in `SOURCE_DB` can be decrypted in `TARGET_DB` using the copy of the certificate.
72
+
The more complex example creates two databases, `SOURCE_DB` and `TARGET_DB`. Then, create a certificate in `SOURCE_DB`, and then copy the certificate to the `TARGET_DB`. Finally, demonstrate that data encrypted in `SOURCE_DB` can be decrypted in `TARGET_DB` using the copy of the certificate.
73
73
74
-
To create the example environment, create the `SOURCE_DB` and `TARGET_DB` databases, and a master key in each. Then create a certificate in `SOURCE_DB`.
74
+
To create the example environment, create the `SOURCE_DB` and `TARGET_DB` databases, and a master key in each database. Then, create a certificate in `SOURCE_DB`.
Now extract the binary description of the certificate.
99
+
Next, extract the binary description of the certificate.
100
100
101
101
```sql
102
102
DECLARE @CERTENC VARBINARY(MAX);
@@ -109,7 +109,7 @@ SELECT @CERTPVK AS EncryptedBinaryCertificate;
109
109
GO
110
110
```
111
111
112
-
Create the duplicate certificate in the `TARGET_DB` database. You must modify the following code, inserting the two binary values returned in the previous step.
112
+
Then, create the duplicate certificate in the `TARGET_DB` database. Modify the following code for this to work, inserting the two binary values - @CERTENC and @CERTPVK - returned in the previous step. Don't surround these values with quotes.
113
113
114
114
```sql
115
115
-- Create the duplicate certificate in the TARGET_DB database
@@ -128,7 +128,7 @@ UNION
128
128
SELECT*FROMTARGET_DB.sys.certificates;
129
129
```
130
130
131
-
The following code executed as a single batch demonstrates that data encrypted in `SOURCE_DB` can be decrypted in `TARGET_DB`.
131
+
This code, executed as a single batch, demonstrates that `TARGET_DB` can decrypt data originally encrypted in `SOURCE_DB`.
Returns the private key of a certificate in binary format. This function takes three arguments.
32
+
This function returns the private key of a certificate in binary format. This function takes three arguments.
33
33
- A certificate ID.
34
-
- An encryption password which is used to encrypt the private key bits when they are returned by the function, so that the keys are not exposed clear text to users.
35
-
-A decryption password which is optional. If a decryption password is specified, then it is used to decrypt the private key of the certificate otherwise database master key is used.
34
+
- An encryption password, used to encrypt the private key bits returned by the function. This approach does not expose the keys as clear text text to users.
35
+
-An optional decryption password. A specified decryption password is used to decrypt the private key of the certificate. Otherwise, the database master key is used.
36
36
37
-
Only users that have access to certificate’s private key will be able to use this function. This function returns the private key in PVK format.
37
+
Only users with access to the certificate private key can use this function. This function returns the private key in PVK format.
38
38
39
39
## Syntax
40
40
@@ -49,7 +49,7 @@ CERTPRIVATEKEY
49
49
50
50
## Arguments
51
51
*certificate_ID*
52
-
Is the **certificate_id** of the certificate. This is available from sys.certificates or by using the [CERT_ID (Transact-SQL)](../../t-sql/functions/cert-id-transact-sql.md) function. *cert_id*is type **int**
52
+
The **certificate_id** of the certificate. Obtain this value from sys.certificates or from the [CERT_ID (Transact-SQL)](../../t-sql/functions/cert-id-transact-sql.md) function. *cert_id*has data type **int**.
53
53
54
54
*encryption_password*
55
55
The password used to encrypt the returned binary value.
@@ -61,10 +61,10 @@ The password used to decrypt the returned binary value.
61
61
**varbinary**
62
62
63
63
## Remarks
64
-
**CERTENCODED** and **CERTPRIVATEKEY**are used together to return different portions of a certificate in binary form.
64
+
Use **CERTENCODED** and **CERTPRIVATEKEY** together to return different portions of a certificate, in binary form.
For a more complex example that uses **CERTPRIVATEKEY** and **CERTENCODED** to copy a certificate to another database, see example B in the topic [CERTENCODED (Transact-SQL)](../../t-sql/functions/certencoded-transact-sql.md).
84
+
See [CERTENCODED (Transact-SQL)](../../t-sql/functions/certencoded-transact-sql.md), Example B, for a more complex example that uses **CERTPRIVATEKEY** and **CERTENCODED** to copy a certificate to another database.
Is a variable that contains data from which to generate an authenticator. Must match the value that was supplied to EncryptByKey.
62
62
63
63
## Return Types
64
-
**varbinary** with a maximum size of 8,000 bytes.
64
+
**varbinary** with a maximum size of 8,000 bytes.
65
+
66
+
Returns NULL if the symmetric key used for encrypting the data is not open or the *ciphertext* is NULL.
65
67
66
68
## Remarks
67
69
DecryptByKey uses a symmetric key. This symmetric key must already be open in the database. There can be multiple keys open at the same time. You do not have to open the key immediately before decrypting the cipher text.
0 commit comments