Writing public key and private key to files for Lattice based cryptography in Rust

188 Views Asked by At

The details of my problem is that I am not really sure how I can go about writing what I need to a couple .bin files. I am fairly new to rust and I wanted to explore quantum resistant lattice based cryptography/encryption using public and private key-pairs. This would also allow me to get more familiar with rust.

I am using the "NTRU" crate. (idk of any other better crates at this time. If you do then let me know) The issue is that the keypair and the data in "kp" in the code below that gets generated can't just normally be written to a file the normal way.

Here is the code that I have for context:

extern crate ntru;
extern crate hex;

use ntru::rand::RNG_DEVURANDOM;
use ntru::encparams::DEFAULT_PARAMS_256_BITS;
use hex::{encode, decode};
use std::io;
use std::process::Command;
use std::env::consts::OS;





fn clear() {
    match OS {
        "linux" | "macos" => {
            let _ = Command::new("clear").status();
        }
        "windows" => {
            let _ = Command::new("cmd")
                .args(&["/C", "cls"])
                .status();
        }
        _ => {}
    }
}



fn main() {
    let rand_ctx = ntru::rand::init(&RNG_DEVURANDOM).unwrap();
    let kp = ntru::generate_key_pair(&DEFAULT_PARAMS_256_BITS, &rand_ctx).unwrap();
    // println!("{:?}", kp.get_public());
    // println!("{:?}", kp.get_private());

    
    // Save public key from "kp" to file here.
    // Save keypair "kp" to file here

    // Load pub key from file here


    let mut input = String::new();
    clear();
    println!("Enter a message to encrypt: ");
    io::stdin().read_line(&mut input).unwrap();
    let msg = input.trim().as_bytes();


    // Ask for what public key to use to encrypt data. (Should be able to use other public keys.)
    let encrypted_msg = ntru::encrypt(msg, kp.get_public(), &DEFAULT_PARAMS_256_BITS, &rand_ctx).unwrap();
    let encrypted_msg_hex = encode(&encrypted_msg);




    // Get keypair "kp" or private key from file here


    let encrypted_msg_unhex = decode(&encrypted_msg_hex).unwrap();
    let decrypted_msg = ntru::decrypt(&encrypted_msg_unhex, &kp, &DEFAULT_PARAMS_256_BITS).unwrap();



    // Verify that decrypted message matches original message.
    let msg_as_string = String::from_utf8_lossy(msg);
    let dcr_msg = String::from_utf8_lossy(&decrypted_msg);
    assert_eq!(msg_as_string, dcr_msg, "Error: Decrypted message doesn't match original message");

    println!("Original Message: {}\n", msg_as_string);
    println!("Encrypted Message: {}\n", encrypted_msg_hex);
    println!("Decrypted Message: {}", dcr_msg);
}

And here are my dependencies:

[dependencies]
ntru = "0.5.6"
hex = "0.4.3"

I have tried to mess around with serde and bincode but I just am not fully able to understand/wrap my head around how to use the crate(s) yet and the documentation for them isn't really giving me the kick I need to be able to really understand how I am supposed to use them correctly. (Maybe I can get some examples/help here to give me that extra kick I need.) Also, I am not entirely sure if they are even the correct way to store this kind of data to some files to begin with.

The comments in my code basically show you what I want to do and where to save to files and load from files. I am in the most basic terms possible. Looking for a way to correctly save the public key in "kp" to a file and "kp" itself to another file that would be the private file. So I can also use other public keys made with ntru to encrypt messages.

Any help or advice would be greatly appreciated!

If anyone has any questions or need me to clear up some things, then by all means let me know and I'll try to answer the best I can when I am able to.

0

There are 0 best solutions below