Table of content

What is this :grey_question:

The rules of the game are explained in my original post.

22nd Challenge

Challenge

Encrypt plaintext into aes128 encrypted text and back again, note this is currently useless as I am using a static key and IV.

Solution :white_check_mark:

	#[wasm_bindgen]
	pub fn encrypt(plaintext: &str)  -> String {
	    let key = hex!("000102030405060708090a0b0c0d0e0f");
	    let iv = hex!("f0f1f2f3f4f5f6f7f8f9fafbfcfdfeff");
	    type Aes128Cbc = Cbc<Aes128, Pkcs7>;
	    let cipher = Aes128Cbc::new_from_slices(&key, &iv).unwrap();
	    let ciphertext = cipher.encrypt_vec(plaintext.as_bytes());
	    hex::encode(ciphertext)
	}


	#[wasm_bindgen]
	pub fn decrypt(encrypted: &str)  -> String {
	    let key = hex!("000102030405060708090a0b0c0d0e0f");
	    let iv = hex!("f0f1f2f3f4f5f6f7f8f9fafbfcfdfeff");
	    type Aes128Cbc = Cbc<Aes128, Pkcs7>;
	    let cipher = Aes128Cbc::new_from_slices(&key, &iv).unwrap();
	    let decrypted = cipher.decrypt_vec(hex::decode(&encrypted).unwrap().as_ref());
	    match decrypted{
	        Ok(_) => String::from_utf8(decrypted.unwrap()).unwrap(),
	        Err(x) => format!("{}",x)
	    }
	}
	

With a simple html the web assembly is tested.

	<!DOCTYPE html>
	<html lang="en">
	<head>
	    <meta charset="UTF-8">
	    <title>Title</title>
	</head>
	<body>
	<script type="module">
	    import init, {encrypt,decrypt} from "./pkg/wasm_encrypt.js";
		      init()
		        .then(() => {
	                var input = "hello";
	                var output = encrypt(input);
	                console.log(output);
	                var output2 = decrypt(output);
	                console.log(output2);
		        });
	</script>
	</body>
	</html>
	



Encrypted Payload:


Decrypted Payload:



To see the full source see github.