Encryption Tutorial
Any operations here behave as if you executed them from our own server.
Section 1 - Authorizing a User
An authorized user is identified by an email address that is granted an authorization token (authToken). This token is used to store and access the protected key.
Use the following steps with the request below to create the subscriber:
- Create an API key from the XQ Dashboard.
- Copy and paste the API key in the required api-key field.
- Enter the user's email address as the user the Body field.
- Run the request below (or use Test Request on the matching endpoint in the API reference).
Parameters — /authorize
| Name | Type | Required | Description |
|---|---|---|---|
api-key | string | Yes | General API key |
user | string | Yes | The email address or phone number of the account to validate. The confirmation link or code will be sent to this address or number |
A successful response includes the Pre-Auth Token that will allow the subscriber to exchange it in conjunction with the 2FA pin received in their email/phone with an authToken. Store the Pre-Auth Token for use in following step of the tutorial.
Parameters — /codevalidation
| Name | Type | Required | Description |
|---|---|---|---|
token | string | Yes | The Pre-Auth token that is returned after an initial request for authorization. |
pin | integer | Yes | The pin that was sent to the users email or phone number. |
A successful response will return a 204 with "No Content" but this will validate your pre-auth token with your 2fa credentials.
Parameters — /exchange
| Name | Type | Required | Description |
|---|---|---|---|
api-key | string | Yes | General API key |
Authorization | string | This is a bearer token required to make most XQ API calls. To get one, a user must first utilize the authorize endpoint, upon which they get a pre-auth token. After they confirm their account utilizing the code-validation endpoint, they can then exchange the pre-auth token in return for an access token to a specific team. | |
b | integer | This is the specific Team ID you are using to exchange your pre-auth token for authorization. If no Team ID is specified, you will automatically be authorized into the Team ID with the lowest index that you have access to. |
A successful response will return a 200 with a new access token which can be used to store and access protected keys in that specific team.
Section 2 (Optional) - Supply Entropy to your encryption key
Your encryption key is a random string of bits used by an encryption algorithm for rewriting data so it appears to be random. To help increase the security of your key, you can request quantum entropy using XQ's quantum entropy service. The quantum entropy service provides an alternate seed to your encryption key.
Enter a value for the ks field in the request below and run it to supply entropy.
Parameters — /
| Name | Type | Required | Description |
|---|---|---|---|
ks | string | Yes | The number of entropy bits to fetch. Value will be returned as a base64-encoded hex string. |
The response is a base64-encoded hex string.
While the hex string returned in the response can be used as entropy, to retrieve the actual bits, the string should be decoded from base-64 and each hex character in the sequence converted to its 4-bit binary representation.
Section 3 - Creating and saving the key packet to XQ
This step involves taking the user's secret key that needs protection and a list of authorized identities, then automatically generating and storing an encrypted key packet in the XQ keystore.
After the encrypted key packet is securely stored, the endpoint returns a locator token. This token must be sent alongside the data to enable the recipient to authenticate with the keystore and request access to the decryption key.
Use the following steps with the request below to create the key packet:
- Copy and paste your API key in the required api-key field.
- Copy and paste your stored authToken from the Creating a subscriber response in Section 1 of this tutorial. Prefix the authToken with Bearer.
- Enter a comma-delimited list of email addresses and/or phone numbers as recipients in the Body field.
- Enter a value for the expiration timeframe as expires in the Body field.
- Run the request below (or use Test Request on the matching endpoint in the API reference).
Parameters — /packet/add
| Name | Type | Required | Description |
|---|---|---|---|
api-key | string | Yes | General API key |
authorization | string | Yes | authToken returned in /exchange response. Prefix the auth token with "Bearer" |
recipients | string | Yes | Comma-delimited list of email addresses or phone numbers who are allowed to access the key. |
expires | string | Yes | Number of hours that this key will remain valid for. After this time, it will no longer be accessible. |
key | string | Yes | The secret key that the user wants to protect. |
type | string | A value indicating the type of data that is being encrypted. Values are 1 = “file”, 2 = “msg”, 3 = “chat”, 4 = “form”, 5 = “data” | |
meta | object | Any additional meta information about the key. |
A successful response means your key packet is now stored on the key server. The response contains the locator token used to retrieve your encryption key in the future.
Section 4 - Encrypting a message
Now that your encryption key is stored in the XQ key server, you are ready to encrypt a message! Use an appropriate cryptographic algorithm e.g. AES, DES, Rabbit, RC4 to encrypt your message.
| Name | Type | Required | Description |
|---|---|---|---|
message | string | Yes | The message you want to encrypt |
key | string | Yes | The encryption key that was stored in XQ's keystore in the previous step |
import CryptoJS from 'crypto-js';
// Example values
const message = "This is a secret message";
const key = "xyz123";
// Encrypt the message
const encrypted = CryptoJS.AES.encrypt(message, key).toString();