Problems with writing to a file using std::fs::File and std::io::Write

I am trying to make a program to dump the contents of a file (encryptme.txt) using the function write_to_string() from std::io::write, then use the encryption algorithm https://crates.io/crates/aes-gcm with a 256 bit key, then write the encrypted text to another file, encrypted.txt. I also wanted to write the nonce and key to another file, keys.txt, so the user can keep track of them. For some reason, the program does not write the keys to keys.txt. Perhaps someone could help me solve this.

here is a sample run:

   Compiling txtencrypt v0.1.0 (/Users/xxxxxxx/Library/Mobile Documents/com~apple~CloudDocs/Rust_projects/txtencrypt)
    Finished dev [unoptimized + debuginfo] target(s) in 2.80s
     Running `target/debug/txtencrypt`
your key is bQjWiCHrdgm6FUmx3GXMQASlC4AwqcTq
thread 'main' panicked at 'failed to write your key to keys.txt: Os { code: 9, kind: Other, message: "Bad file descriptor" }', src/libcore/result.rs:1165:5
note: run with `RUST_BACKTRACE=1` environment variable to display a backtrace.

Here is my code:

use std::fs::File;
use std::io::Read;
use std::io::Write;

use std::io::{ErrorKind};

use aes_gcm::Aes256Gcm;
use aead::{Aead, NewAead, generic_array::GenericArray};

extern crate rand;
use rand::{thread_rng, Rng};
use rand::distributions::Alphanumeric;

fn main() {

    let mut f = match File::open("encryptme.txt") {
        Ok(file) => {
            file
        }
        Err(e) => {
            match e.kind() {
                ErrorKind::PermissionDenied => { panic!("permision denied to open encryptme.txt, please");}
                ErrorKind::NotFound => { 
                    println!("Creating file encryptme.txt...");
                    match File::create("encryptme.txt") {
                        Ok(file) => {
                            println!("Go ahead stop this program if you want to put text inside of encryptme.txt to encrypt");
                            file
                        }
                        Err(e) => {
                            panic!("failed to make the file encryptme.txt (hint: you could manually make it). Error code: {}", e);
                        }
                    }
                }
                _ => {
                    panic!("failed to open the file encryptme.txt");
                }
            
            }
        }
    };


    let mut encryptme = String::new();
    f.read_to_string(&mut encryptme).expect("Could not read the file encryptme.txt to a string. Are you root?");  //dumps the contents of encryptme.txt into the string encryptme

    encrypt(rand_key(), rand_nonce(), encryptme);

    println!("Remember to write down the key and nonce somewhere safe, or you won't be able to decrypt the file encrypted.txt!!!");
    println!("Your keys and nonces have been written to keys.txt. Don't lose them.");
}


fn encrypt(key: String, nonce: String, plaintext: String) {
    let key = GenericArray::clone_from_slice(key.as_bytes());
    let aead = Aes256Gcm::new(key);

    let snonce = GenericArray::from_slice(nonce.as_bytes());

    let encryptthis: Vec<u8> = aead.encrypt(snonce, plaintext.as_bytes().as_ref()).expect("failed to encrypt text");
    
    let mut f = File::create("encrypted.txt").expect("failed to create the file encrypted.txt");

    f.write_all(&encryptthis[..]).expect("failed to write to the file encrypted.txt");

}


fn rand_nonce() -> String {
    let rand_string: String = thread_rng()
        .sample_iter(&Alphanumeric)
        .take(12)
        .collect();

    println!("your nonce is {}", rand_string);

    //writes the key to keys.txt
    let mut f = match File::open("keys.txt") {
        Ok(file) => {
            file
        }
        Err(e) => {
            match e.kind() {
                ErrorKind::PermissionDenied => { panic!("permision denied to open keys.txt");}
                ErrorKind::NotFound => { 
                    println!("Creating file keys.txt...");
                    match File::create("keys.txt") {
                        Ok(file) => {
                            file
                        }
                        Err(e) => {
                            panic!("failed to make the file encryptme.txt (hint: you could manually make it). Error code: {}", e);
                        }
                    }
                }
                _ => {
                    panic!("failed to open the file encryptme.txt");
                }  
            }
        }
    };

    f.write_all(&format!("Your nonce is {}", rand_string).as_bytes()[..]).expect("could not write nonce to keys.txt");

    
    rand_string
}



fn rand_key() -> String {
    let rand_string: String = thread_rng()
        .sample_iter(&Alphanumeric)
        .take(32)
        .collect();

    println!("your key is {}", rand_string);

    let mut f = match File::open("keys.txt") {
        Ok(file) => {
            file
        }
        Err(e) => {
            match e.kind() {
                ErrorKind::PermissionDenied => { panic!("permision denied to open keys.txt");}
                ErrorKind::NotFound => { 
                    println!("Creating file keys.txt...");
                    match File::create("keys.txt") {
                        Ok(file) => {
                            file
                        }
                        Err(e) => {
                            panic!("failed to make the file encryptme.txt (hint: you could manually make it). Error code: {}", e);
                        }
                    }
                }
                _ => {
                    panic!("failed to open the file encryptme.txt");
                }  
            }
        }
    };

    f.write(&format!("your key is {}", rand_string).as_bytes()[..]).expect("failed to write your key to keys.txt");


    rand_string
}

File::open only opens the file with read permissions, not write permissions. You can use File::create, or OpenOptions instead.