fernet.js icon indicating copy to clipboard operation
fernet.js copied to clipboard

Is this library compatible with Python Fernet library?

Open nikolovk opened this issue 2 years ago • 1 comments

Could we decrypt messages that are encrypted with https://cryptography.io/en/latest/fernet/?

nikolovk avatar Jan 21 '23 09:01 nikolovk

Yes this is possible.

// Get the encryption key from the environment variable or use a default value
const encryptionKey = process.env.DOCUMENT_ENCRYPTION_KEY || "";

// Create a Fernet cipher using the encryption key
const fernetSecret = new Secret(encryptionKey);

// Decryption function
function decrypt(encryptedText: string): string {
  const token = new Token({
    secret: fernetSecret,
    token: encryptedText,
    ttl: 0,
  });
  return token.decode();
}

Usage:

const encryptedText =
    "gAAAAABmIAROED2UWje1er9CmTjN8frlusIXk-_tLftujD2J8DexIeXWdfph3S0WOlmbE2HddT-hWN7FwInWuw3aqzj6GvxKY4U0FzMdBVwTh3P4kmTZ1rguPAtnvvlmPOkzUcKsygdL4Ur6oia_ibsbBqN1wKfKarbBmckNrtA_a-GbSoBlDE3M3XbvY1cXZj3F2Ii_kTMve_n8FEWxt0pQtAOZelp77I6v44jmQ5iRuStdYhwIWEAtl_cIePCw8j9oMIGSs9JR0bGU31h3ZaeYDSUhQpKtWvpGw_L8SGwlbbYA_FM7GZZ7X5jWpt98GTbQTBM5wNAyuG5ZZ_MCVZhJXUCaZTpw463NzR60C-1As0vKKuiKiqOj8tN1N15gOH1TNrdNmDY5X6PeaJ3eh1iqP2pC_Voer1k2Fe-yAlSNXDaaXHkWctY=";

const decryptedText = decrypt(encryptedText);
console.log(decryptedText);

albertpurnama avatar Apr 30 '24 01:04 albertpurnama