Client-Side Encryption
We will be using AES for Symmetric data encryption.
The encryption key used will be user password with sha256 hash and base64 encoded.
Text data are directly encrypted, images are encoded to base64 string first before being encrypted.
Libraries
Sample Code
Pseudocode
import cryptojs from "crypto-js";
export function encrypt(data: string, userPassword: string): string {
const passwordHash = encodeBase64(hashSHA256(userPassword));
return cryptojs.AES.encrypt(data, passwordHash).toString();
}
export function decrypt(encryptedData: string, userPassword: string): string {
const passwordHash = encodeBase64(hashSHA256(userPassword));
return cryptojs.enc.Utf8.stringify(
cryptojs.AES.decrypt(encryptedData, passwordHash)
);
}
const data: string = "content";
const userPassword: string = "xxx";
const encrypted = encrypt(data, userPassword);
const decrypted = decrypt(encrypted, userPassword);