This repository has been archived on 2026-04-25. You can view files and clone it, but cannot push or open issues or pull requests.
RustyPass/src/secure.rs

41 lines
No EOL
1.2 KiB
Rust

// Libraries
use ring::rand::{SecureRandom, SystemRandom};
// Constants
// Structs
pub struct Secure{
}
// Implementations
impl Secure {
// Takes in plaintext and a key to encrypt it
pub fn encrypt(value: String, key: String) -> String {
let rng = SystemRandom::new();
let value_bytes = value.as_bytes();
let key_bytes = key.as_bytes();
let mut ciphertext = vec![0u8; value_bytes.len()];
rng.fill(&mut ciphertext).expect("Encryption Failed");
for i in 0..value_bytes.len() {
ciphertext[i] = value_bytes[i] ^ key_bytes[i % key_bytes.len()];
}
return base64::encode(ciphertext);
}
// Takes in cyphertext and a key to decrypt it
pub fn decrypt(cyphertext: String, key: String) -> String {
let cyphertext_bytes = base64::decode(cyphertext).expect("(0) Decryption Failed");
let key_bytes = key.as_bytes();
let mut plaintext = vec![0u8; cyphertext_bytes.len()];
for i in 0..cyphertext_bytes.len() {
plaintext[i] = cyphertext_bytes[i] ^ key_bytes[i % key_bytes.len()];
}
return String::from_utf8(plaintext).expect("(1) Decryption Failed");
}
}