Aller au contenu principal

Chiffrement/Déchiffrement

Sentinel: Chiffrement symétrique (AES)

Pour chiffrer des données en utilisant les sentinelles, suivez ces étapes

  1. Génération d'un Vecteur d'Initialisation (IV) Aléatoire :
    • Un IV est une valeur aléatoire utilisée avec la clé de chiffrement pour garantir que les textes en clair identiques produisent des textes chiffrés différents.
  2. Processus de Chiffrement :
    • Chiffrez le texte en clair en utilisant l'algorithme AES avec le mode CBC et le padding PKCS7.
    • Combinez le texte chiffré, l'IV, l'ID de la sentinelle et un hash HMAC (sum) pour créer la chaîne chiffrée finale.

Exemple de Code

Voici un exemple d'implémentation utilisant CryptoJS pour le chiffrement AES :

import CryptoJS  from "crypto-js";

public static encrypt = (text: string, sentinel: SentinelOutput): string => {
// Generate a random IV
const iv = CryptoJS.lib.WordArray.random(128 / 8);

// Encrypt the text using AES with CBC mode and PKCS7 padding
const ciphertext = CryptoJS.AES.encrypt(text, CryptoJS.enc.Utf8.parse(sentinel.cipher), {
iv: iv,
mode: CryptoJS.mode.CBC,
padding: CryptoJS.pad.Pkcs7
}).toString();

// Compute the HMAC SHA-512 hash for integrity verification
const sum = CryptoJS.SHA512(ciphertext + iv.toString() + sentinel.cipher).toString();

// Concatenate ciphertext, IV, sentinel ID, and hash to form the final encrypted string
return `${ciphertext}.${iv.toString()}.${sentinel.id}.${sum}`;
};

Sentinel: Déchiffrement

Pour déchiffrer les données, vous devez extraire le texte chiffré, l'IV, l'ID de la sentinelle et le hash de la chaîne chiffrée. Vérifiez le hash pour assurer l'intégrité des données, puis déchiffrez le texte chiffré en utilisant le même IV et la clé de la sentinelle.

Exemple de Code

const decrypt = (encryptedText: string, sentinel: SentinelOutput): string => {
// Split the encrypted text into its components
const parts = encryptedText.split('.');
const ciphertext = parts[0];
const iv = CryptoJS.enc.Hex.parse(parts[1]);
const id = parts[2];
const sum = parts[3];

// Verify the integrity of the data using the hash
const computedSum = CryptoJS.SHA512(ciphertext + iv.toString() + sentinel.cipher).toString();
if (computedSum !== sum) {
throw new Error('Data integrity check failed');
}

// Decrypt the ciphertext using AES with CBC mode and PKCS7 padding
const decrypted = CryptoJS.AES.decrypt(ciphertext, CryptoJS.enc.Utf8.parse(sentinel.cipher), {
iv: iv,
mode: CryptoJS.mode.CBC,
padding: CryptoJS.pad.Pkcs7
});

return decrypted.toString(CryptoJS.enc.Utf8);
};

Sentinelle anonyme: chiffrement asymétrique avec Krystal-Kyber

Example banner

Pour chiffrer des données en utilisant une sentinelle Krystal-Kyber, suivez ces étapes :

  1. Génération d'un Vecteur d'Initialisation (IV) Aléatoire :
    • Un IV est une valeur aléatoire utilisée avec la clé de chiffrement pour garantir que les textes en clair identiques produisent des textes chiffrés différents.
  2. Processus de Chiffrement :
    • Générer un kyber_cipher et une clé AES-256.
    • Chiffrer le texte en clair en utilisant l'algorithme AES-256 avec le mode CBC et le padding PKCS7.
    • Combiner le kyber_cipher, le texte chiffré, l'IV, l'ID de la sentinelle anonyme et un hash HMAC (sum) pour créer la chaîne chiffrée finale.

Exemple de code

static asym_encrypt(text, sentinel) {
// Generate a random IV
const iv = CryptoJS.lib.WordArray.random(128 / 8);

// Encrypt the text using Kyber
const c_ss = kyber.Encrypt1024(Array.from(Buffer.from(sentinel.public_key, 'hex')));
const cipher = Buffer.from(c_ss[0]);
const ss1 = c_ss[1];
const secret_cipher = cipher.toString('hex');

// Symmetric encryption using shared secret (AES256 key)
const ciphertext = AnonymousSentinelEncryption.text_encryption(text, ss1.toString('hex'), iv);

// Compute the HMAC SHA-512 hash for integrity verification
const sum = CryptoJS.SHA512(ciphertext + iv.toString() + ss1.toString('hex')).toString();

// Concatenate components to form the final encrypted string
return `ano.${secret_cipher}.${ciphertext}.${iv.toString()}.${sentinel.id}.${sum}`;
}

static text_encryption(text, key, iv) {
return CryptoJS.AES.encrypt(text, CryptoJS.enc.Hex.parse(key), {
iv: iv,
mode: CryptoJS.mode.CBC,
padding: CryptoJS.pad.Pkcs7
}).toString();
}

Sentinelle anonyme: Déchiffrement

Pour déchiffrer les données, vous devez :

  • Extraire le kyber_cipher, le texte chiffré, l'IV, l'ID de la sentinelle et le hash de la chaîne chiffrée.
  • Générer la clé AES-256 à partir du kyber_cipher et de la clé secrète.
  • Vérifier le hash pour assurer l'intégrité des données, puis déchiffrer le texte chiffré en utilisant le même IV et la clé de la sentinelle.

Exemple de code

static async asym_decrypt_with_key_chain(content, sentinel) {
// Split the encrypted content into its components
const explode = content.split('.');

// Decrypt the shared secret (AES256 key) using Kyber
const ss2 = kyber.Decrypt1024(
Array.from(Buffer.from(explode[1], 'hex')),
Array.from(Buffer.from(sentinel.secret_key, 'hex'))
);

// Verify the integrity of the data using the hash
const check_sum = CryptoJS.SHA512(
explode[2] + explode[3] + ss2.toString('hex')
).toString();

if (check_sum !== explode[5]) throw new Error('Bad checksum');

// Decrypt the ciphertext using AES-256 with CBC mode and PKCS7 padding
const decrypted = CryptoJS.AES.decrypt(explode[2], CryptoJS.enc.Hex.parse(ss2.toString('hex')), {
iv: CryptoJS.enc.Hex.parse(explode[3]),
mode: CryptoJS.mode.CBC,
padding: CryptoJS.pad.Pkcs7
});

return decrypted.toString(CryptoJS.enc.Utf8);
}

Encryption/Decryption Made Easy with the SDK

The SDK simplifies the process of encrypting and decrypting data, both symmetrically and asymmetrically. Here's how you can easily perform these operations using the provided methods.

Symmetric Encryption

Encrypting Data

To encrypt data symmetrically using AES-256, you only need to call the encrypt method with the plaintext and an object containing the access token and API URL.

const to_encrypt = "Sensitive data to encrypt";
const encrypted = Lagertha.encrypt(to_encrypt, { access_token: "eydzv54vrev...", env: "<lagertha_api_url>" });
console.log('Encrypted:', encrypted);

Decrypting Data

To decrypt the previously encrypted data, simply call the decrypt method with the encrypted string.

const decrypted = Lagertha.decrypt(encrypted, { access_token: "eydzv54vrev...", env: "<lagertha_api_url>" });
console.log('Decrypted:', decrypted);

Asymmetric Encryption

Encrypting Data

For asymmetric encryption using an anonymous sentinel, you can use the anonymous_encrypt method. This method requires the plaintext, the anonymous sentinel object, and the API URL.

const to_encrypt = "Sensitive data to encrypt";
const anonymous_sentinel = {
id: "123e4567-e89b-12d3-a456-426614174000",
public_key: "publicKeyHexString",
sum: "sha256hashofpublickey"
};

const encrypted = Lagertha.anonymous_encrypt(to_encrypt, anonymous_sentinel, "<lagertha_api_url>");
console.log('Encrypted:', encrypted);

Decrypting Data

To decrypt the data encrypted with the anonymous sentinel, use the anonymous_decryption method. This requires the encrypted string and an object containing the access token and API URL.

const decrypted = Lagertha.anonymous_decryption(encrypted, { access_token: "eydzv54vrev...", env: "<lagertha_api_url>" });
console.log('Decrypted:', decrypted);