init: add code

This commit is contained in:
Xory 2024-06-10 10:57:56 +03:00
commit 2edb74ab27
8 changed files with 233 additions and 0 deletions

1
.gitignore vendored Normal file
View file

@ -0,0 +1 @@
/target

158
Cargo.lock generated Normal file
View file

@ -0,0 +1,158 @@
# This file is automatically @generated by Cargo.
# It is not intended for manual editing.
version = 3
[[package]]
name = "aes"
version = "0.7.5"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "9e8b47f52ea9bae42228d07ec09eb676433d7c4ed1ebdf0f1d1c29ed446f1ab8"
dependencies = [
"cfg-if",
"cipher",
"cpufeatures",
"opaque-debug",
]
[[package]]
name = "block-modes"
version = "0.8.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "2cb03d1bed155d89dce0f845b7899b18a9a163e148fd004e1c28421a783e2d8e"
dependencies = [
"block-padding",
"cipher",
]
[[package]]
name = "block-padding"
version = "0.2.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "8d696c370c750c948ada61c69a0ee2cbbb9c50b1019ddb86d9317157a99c2cae"
[[package]]
name = "cfg-if"
version = "1.0.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "baf1de4339761588bc0619e3cbc0120ee582ebb74b53b4efbf79117bd2da40fd"
[[package]]
name = "cipher"
version = "0.3.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "7ee52072ec15386f770805afd189a01c8841be8696bed250fa2f13c4c0d6dfb7"
dependencies = [
"generic-array",
]
[[package]]
name = "cpufeatures"
version = "0.2.12"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "53fe5e26ff1b7aef8bca9c6080520cfb8d9333c7568e1829cef191a9723e5504"
dependencies = [
"libc",
]
[[package]]
name = "generic-array"
version = "0.14.7"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "85649ca51fd72272d7821adaf274ad91c288277713d9c18820d8499a7ff69e9a"
dependencies = [
"typenum",
"version_check",
]
[[package]]
name = "getrandom"
version = "0.2.15"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "c4567c8db10ae91089c99af84c68c38da3ec2f087c3f82960bcdbf3656b6f4d7"
dependencies = [
"cfg-if",
"libc",
"wasi",
]
[[package]]
name = "hex"
version = "0.4.3"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "7f24254aa9a54b5c858eaee2f5bccdb46aaf0e486a595ed5fd8f86ba55232a70"
[[package]]
name = "libc"
version = "0.2.155"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "97b3888a4aecf77e811145cadf6eef5901f4782c53886191b2f693f24761847c"
[[package]]
name = "opaque-debug"
version = "0.3.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "c08d65885ee38876c4f86fa503fb49d7b507c2b62552df7c70b2fce627e06381"
[[package]]
name = "ppv-lite86"
version = "0.2.17"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "5b40af805b3121feab8a3c29f04d8ad262fa8e0561883e7653e024ae4479e6de"
[[package]]
name = "rand"
version = "0.8.5"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "34af8d1a0e25924bc5b7c43c079c942339d8f0a8b57c39049bef581b46327404"
dependencies = [
"libc",
"rand_chacha",
"rand_core",
]
[[package]]
name = "rand_chacha"
version = "0.3.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "e6c10a63a0fa32252be49d21e7709d4d4baf8d231c2dbce1eaa8141b9b127d88"
dependencies = [
"ppv-lite86",
"rand_core",
]
[[package]]
name = "rand_core"
version = "0.6.4"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "ec0be4795e2f6a28069bec0b5ff3e2ac9bafc99e6a9a7dc3547996c5c816922c"
dependencies = [
"getrandom",
]
[[package]]
name = "solcrypt"
version = "0.1.0"
dependencies = [
"aes",
"block-modes",
"hex",
"rand",
]
[[package]]
name = "typenum"
version = "1.17.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "42ff0bf0c66b8238c6f3b578df37d0b7848e55df8577b3f74f92a69acceeb825"
[[package]]
name = "version_check"
version = "0.9.4"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "49874b5167b65d7193b8aba1567f5c7d93d001cafc34600cee003eda787e483f"
[[package]]
name = "wasi"
version = "0.11.0+wasi-snapshot-preview1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "9c8d87e72b64a3b4db28d11ce29237c246188f4f51057d65a7eab63b7987e423"

10
Cargo.toml Normal file
View file

@ -0,0 +1,10 @@
[package]
name = "solcrypt"
version = "0.1.0"
edition = "2021"
[dependencies]
aes = "0.7.5"
block-modes = "0.8.1"
hex = "0.4.3"
rand = "0.8.5"

1
meow.dec.text Normal file
View file

@ -0,0 +1 @@
cats would rule the world better than humans

1
meow.enc Normal file
View file

@ -0,0 +1 @@
ł¶Îí޶ńNzN˙®<CB99>Ŕ˙=)|ŕچÔ'ÝVéđPŔNŚz…†Rć´˝

1
meow.txt Normal file
View file

@ -0,0 +1 @@
cats would rule the world better than humans

53
src/crypto.rs Normal file
View file

@ -0,0 +1,53 @@
extern crate aes;
extern crate block_modes;
extern crate hex;
extern crate rand;
use aes::Aes256;
use block_modes::{BlockMode, Cbc};
use block_modes::block_padding::Pkcs7;
use std::fs::File;
use std::io::{Read, Write};
use std::error::Error;
use std::str;
type Aes256Cbc = Cbc<Aes256, Pkcs7>;
const KEY: &[u8] = b"keyhereshouldbereplacedbybuilder";
const IV: &[u8] = b"unique_initializ"; // IV should be 16 bytes
pub fn encrypt_file(input_path: &str, output_path: &str) -> Result<(), Box<dyn Error>> {
// Read the input file
let mut input_file = File::open(input_path)?;
let mut buffer = Vec::new();
input_file.read_to_end(&mut buffer)?;
// Encrypt the data
let cipher = Aes256Cbc::new_from_slices(KEY, IV)?;
let ciphertext = cipher.encrypt_vec(&buffer);
// Write the encrypted data to the output file
let mut output_file = File::create(output_path)?;
output_file.write_all(&ciphertext)?;
Ok(())
}
pub fn decrypt_file(input_path: &str, output_path: &str) -> Result<(), Box<dyn Error>> {
// Read the encrypted file
let mut input_file = File::open(input_path)?;
let mut buffer = Vec::new();
input_file.read_to_end(&mut buffer)?;
// Decrypt the data
let cipher = Aes256Cbc::new_from_slices(KEY, IV)?;
let decrypted_data = cipher.decrypt_vec(&buffer)?;
// Write the decrypted data to the output file
let mut output_file = File::create(output_path)?;
output_file.write_all(&decrypted_data)?;
Ok(())
}

8
src/main.rs Normal file
View file

@ -0,0 +1,8 @@
mod crypto;
use crypto::{encrypt_file, decrypt_file};
fn main() {
println!("Hello, world!");
encrypt_file("meow.txt", "meow.enc").unwrap();
decrypt_file("meow.enc", "meow.dec.text").unwrap();
}