csquared / fernet.js

Javascript implementation of Fernet symmetric encryption https://github.com/kr/fernet-spec
MIT License
73 stars 29 forks source link

Is this library compatible with Python Fernet library? #25

Open nikolovk opened 1 year ago

nikolovk commented 1 year ago

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

albertpurnama commented 4 months ago

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);