Skip to main content

Encryption Tutorial

This tutorial is not a sandbox!

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:

  1. Create an API key from the XQ Dashboard.
  2. Copy and paste the API key in the required api-key field.
  3. Enter the user's email address as the user the Body field.
  4. Run the request below (or use Test Request on the matching endpoint in the API reference).

Parameters — /authorize

NameTypeRequiredDescription
api-keystringYesGeneral API key
userstringYesThe email address or phone number of the account to validate. The confirmation link or code will be sent to this address or number

POST
Headers
curl
curl -X POST 'https://subscription.xqmsg.net/v2/authorize' \
-H 'Content-Type: application/json' \
-H 'api-key: <API_KEY>' \
-d '{
"user": "test@example.com"
}'

Sent from your browser straight to subscription.xqmsg.net. Credentials stay in this tab and are cleared on reload; missing ones appear as <API_KEY> / <ACCESS_TOKEN> in the curl command.

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

NameTypeRequiredDescription
tokenstringYesThe Pre-Auth token that is returned after an initial request for authorization.
pinintegerYesThe pin that was sent to the users email or phone number.

GET
Headers
Bearer
curl
curl -X GET 'https://subscription.xqmsg.net/v2/codevalidation?pin=123456' \
-H 'Authorization: Bearer <ACCESS_TOKEN>'

Sent from your browser straight to subscription.xqmsg.net. Credentials stay in this tab and are cleared on reload; missing ones appear as <API_KEY> / <ACCESS_TOKEN> in the curl command.

A successful response will return a 204 with "No Content" but this will validate your pre-auth token with your 2fa credentials.

Parameters — /exchange

NameTypeRequiredDescription
api-keystringYesGeneral API key
AuthorizationstringThis 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.
bintegerThis 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.

GET
Headers
Bearer
curl
curl -X GET 'https://subscription.xqmsg.net/v2/exchange?b=123' \
-H 'api-key: <API_KEY>' \
-H 'Authorization: Bearer <ACCESS_TOKEN>'

Sent from your browser straight to subscription.xqmsg.net. Credentials stay in this tab and are cleared on reload; missing ones appear as <API_KEY> / <ACCESS_TOKEN> in the curl command.

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 — /

NameTypeRequiredDescription
ksstringYesThe number of entropy bits to fetch. Value will be returned as a base64-encoded hex string.

GET
curl
curl -X GET 'https://quantum.xqmsg.net/v2?ks=256'

Sent from your browser straight to quantum.xqmsg.net. Credentials stay in this tab and are cleared on reload.

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:

  1. Copy and paste your API key in the required api-key field.
  2. Copy and paste your stored authToken from the Creating a subscriber response in Section 1 of this tutorial. Prefix the authToken with Bearer.
  3. Enter a comma-delimited list of email addresses and/or phone numbers as recipients in the Body field.
  4. Enter a value for the expiration timeframe as expires in the Body field.
  5. Run the request below (or use Test Request on the matching endpoint in the API reference).

Parameters — /packet/add

NameTypeRequiredDescription
api-keystringYesGeneral API key
authorizationstringYesauthToken returned in /exchange response. Prefix the auth token with "Bearer"
recipientsstringYesComma-delimited list of email addresses or phone numbers who are allowed to access the key.
expiresstringYesNumber of hours that this key will remain valid for. After this time, it will no longer be accessible.
keystringYesThe secret key that the user wants to protect.
typestringA value indicating the type of data that is being encrypted. Values are 1 = “file”, 2 = “msg”, 3 = “chat”, 4 = “form”, 5 = “data”
metaobjectAny additional meta information about the key.

POST
Headers
Bearer
curl
curl -X POST 'https://subscription.xqmsg.net/v2/packet/add' \
-H 'Content-Type: application/json' \
-H 'api-key: <API_KEY>' \
-H 'authorization: Bearer <ACCESS_TOKEN>' \
-d '{
"recipients": "test2@example.com",
"expires": "1",
"key": "xyz123",
"type": "msg",
"meta": {
"subject": "demo",
"title": "demo"
}
}'

Sent from your browser straight to subscription.xqmsg.net. Credentials stay in this tab and are cleared on reload; missing ones appear as <API_KEY> / <ACCESS_TOKEN> in the curl command.

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.

NameTypeRequiredDescription
messagestringYesThe message you want to encrypt
keystringYesThe 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();