liveBook · Manning

Symmetric key encryption

As explained in Server-Side WebAssembly

Symmetric encryption is encryption in which the same secret key is used for both encryption and decryption.

Symmetric-key encryption converts readable plaintext into ciphertext and reverses it with the same secret key. In a database, it can protect stored data while allowing authorized retrieval. section 5.3.1

A Wasm UDF implementation follows this sequence:

  • Create an encryption instance from the supplied key. section 5.3.1
  • Encrypt plaintext and return the result as a Base64 string. section 5.3.1
  • Decode ciphertext with the same key and return plaintext, or report access denial when decryption fails. section 5.3.1
Listing 5.4 lib.rs file of our wasm_udf_example project
use libsql_bindgen::*;
use magic_crypt::{new_magic_crypt, MagicCryptTrait};

#[libsql_bindgen::libsql_bindgen]
pub fn encrypt(data: String, key: String) -> String {
    let mc = new_magic_crypt!(key, 256);
    mc.encrypt_str_to_base64(data)
}

#[libsql_bindgen::libsql_bindgen]
pub fn decrypt(data: String, key: String) -> String {
    let mc = new_magic_crypt!(key, 256);
    mc.decrypt_base64_to_string(data)
        .unwrap_or("[ACCESS DENIED]".to_owned())
}

The complete Rust library exposes encrypt and decrypt as libSQL user-defined functions, using Magic Crypt with 256-bit strength. section 5.3.1

How Server-Side WebAssembly covers it

How deep it goes
Explains plaintext, ciphertext, shared keys, Base64 conversion, 256-bit configuration, and graceful failure handling.
Examples
Demonstrates Rust encrypt and decrypt functions backed by Magic Crypt and exposed through libSQL bindings.
What you can do after
Build database encryption and decryption functions with Rust, Magic Crypt, Wasm, and libSQL bindings.
How it connects
Connects symmetric encryption with plaintext, ciphertext, decryption, Base64 encoding, databases, Wasm, and user-defined functions.

Also explained in

Compare how 6 books explain symmetric key encryption →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Server-Side WebAssembly. The section links point to the parts of the book it draws on.