Exercise 14: Get Decrypted Storage Value

Problem Statement

Function `getDecrypted(key, password)` banao jo encrypted value ko decrypt kare. Example: getDecrypted("secret", "password123") => "data"

Sample Output:

getDecrypted("secret", "password123") => "data"
// Decrypts and returns value

Solution

const getDecrypted = (key, password) => {
  const encoded = localStorage.getItem(key);
  if (!encoded) return null;
  try {
    const decoded = atob(encoded);
    const value = decoded.slice(0, -password.length);
    return JSON.parse(value);
  } catch { return null; }
};

Explanation

Overall Goal:

  • Encrypted value ko decrypt karna.

Real world:

  • Security: encrypted data retrieval.
  • Privacy: decryption.