Skip to content

Commit

Permalink
update (chars): unique second element for alphabets
Browse files Browse the repository at this point in the history
  • Loading branch information
lumbrjx committed Jan 23, 2024
1 parent 03390a6 commit 8fa04c1
Show file tree
Hide file tree
Showing 7 changed files with 80 additions and 36 deletions.
37 changes: 32 additions & 5 deletions src/encpt/decrypt.rs
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
use std::{error::Error, i32};

use crate::{
encpt::math::process::rem_from_vec,
encpt::math::{matrix::vecs_to_mtrx, process::rem_from_vec},
mxas_to_chars,
shared::parse::{
concat_every_three_elements, get_indexes, move_elements, split_string, string_vec2_str,
Mv_Direction,
concat_every_n_elements, get_indexes, move_elements, rem_zeros, split_string,
string_vec2_str, Mv_Direction,
},
};

Expand All @@ -23,7 +24,7 @@ pub fn ceaser_unswap(indxs: Vec<String>, n: usize) -> Vec<i32> {
string_vec2_str(&indxs),
);
let cnct: Vec<String> = swp.iter().map(|c| c.to_string()).collect();
concat_every_three_elements(string_vec2_str(&cnct))
concat_every_n_elements(string_vec2_str(&cnct), 3)
.iter()
.map(|c| c.parse::<i32>().unwrap())
.collect()
Expand All @@ -45,6 +46,32 @@ pub fn df1t_decrypt(buffer: String, salt: String) -> Result<String, Box<dyn Erro
let grn_rm = rem_from_vec(rdlen as i32, grn_un);
let rd_rm = rem_from_vec(rdlen as i32, rd_un);
let ble_rm = rem_from_vec(rdlen as i32, ble_un);
println!("{:?}", rd_rm);

// restore the matrix from the rgb vectors and remove the Null elements
let restored_mtrx: Vec<String> = rem_zeros(vecs_to_mtrx(vec![grn_rm, rd_rm, ble_rm]))
.iter()
.map(|c| c.to_string())
.collect();

let mx_as_to_char: Vec<String>;
match mxas_to_chars(string_vec2_str(&restored_mtrx)) {
Ok(t) => mx_as_to_char = concat_every_n_elements(t, 2),
Err(e) => panic!("{}", e),
};
println!("{:?}", mx_as_to_char);
Ok(salt)
}

// mx version ["652", "165", "314", "671", "113", "422", "103", "923", "314", "194", "113", "389", "314", "422", "652", "923", "113", "194", "103", "422", "652", "389"]
// [652, 165, 314, 671, 113, 422, 103, 923, 314, 194, 113, 389, 314, 422, 652, 923, 113, 194, 103, 422, 652, 389]

// green [165, 314, 671, 113, 923, 314, 194, 422, 652, 422]
// red [652, 103, 314, 103, 0]
// blue [422, 113, 389, 923, 113, 194, 652, 389, 0, 0]

// green dcp : [165, 314, 671, 113, 923, 314, 194, 422, 652, 422]
// red dcp : [652, 103, 314, 103, 0]
// blue dcp : [422, 113, 389, 923, 113, 194, 652, 389, 0]

// the original matrix [[652, 165, 314, 671, 113], [422, 103, 923, 314, 194], [113, 389, 314, 422, 652], [923, 113, 194, 103, 422], [652, 389, 101, 101, 101]]
// [652, 165, 314, 671, 113, 422, 103, 923, 314, 194, 113, 389, 314, 422, 652, 923, 113, 194, 103, 422, 652, 389, 101, 101, 101]
9 changes: 4 additions & 5 deletions src/encpt/encrypt.rs
Original file line number Diff line number Diff line change
Expand Up @@ -103,9 +103,11 @@ pub fn df1t_encrypt(buffer: String, salt: String) -> Result<String, Box<dyn Erro
Ok(t) => mixed = t,
Err(e) => panic!("{}", e),
}
println!("mixed {:?}", mixed);

// map the mixed vec into mx_as vec version
let binding3 = flatten_vec(mixed);
println!("bin3 {:?}", binding3);
let mx_version: Vec<&str>;
match chr_to_mxas(string_vec2_str(&binding3)) {
Ok(t) => mx_version = t,
Expand All @@ -117,20 +119,17 @@ pub fn df1t_encrypt(buffer: String, salt: String) -> Result<String, Box<dyn Erro
let splitted_empty = split_by_n(mtrx_n, str2_string_vec(mx_version));
// structure the matrix by filling the gaps with 0's
let splitted_filled = fill_mtrx_gaps(mtrx_n, char_to_mtrx(splitted_empty));
println!("the original matrix {:?}", &splitted_filled);
// get the green, red, blue vecs from the matrix
let vecs_from_mtrx = mtrx_to_vecs(splitted_filled);
println!("vecs from tmrx {:?}", vecs_from_mtrx);
let grn = &vecs_from_mtrx[0];
let rd = &vecs_from_mtrx[1];
let ble = &vecs_from_mtrx[2];
println!("green {:?} ", &grn);
println!("red {:?} ", &rd);
println!("blue {:?} ", &ble);

// add mtrx_n to green and blue and the red length to red
let grn_a: Vec<String> = flt_subvecs(mtrx_n, grn.to_vec());
let rd_a: Vec<String> = flt_subvecs(mtrx_n, rd.to_vec());
let ble_a: Vec<String> = flt_subvecs(mtrx_n, ble.to_vec());

// ceaser
let grn_swapped = ceaser_swap(grn_a.clone(), grn_a.len() + 2);
let rd_swapped = ceaser_swap(rd_a.clone(), rd_a.len());
Expand Down
19 changes: 18 additions & 1 deletion src/encpt/mapping/mapper.rs
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,23 @@ pub fn chr_to_mxas(vc: Vec<&str>) -> Result<Vec<&str>, &str> {
Ok(result)
}
}

pub fn mxas_to_chars(vc: Vec<&str>) -> Result<Vec<&str>, &str> {
let mut result: Vec<&str> = vec![];

for e in &vc {
for s in CHAR_MAP {
if e == &s[2] {
result.push(s[0]);
}
}
}
if result.len() != vc.len() {
Err("CharError: unrecognized char")
} else {
Ok(result)
}
}
// extend salt based on string length
#[derive(Debug)]
struct EmptyValueError;
Expand Down Expand Up @@ -108,7 +125,7 @@ mod tests {
#[test]
fn try_char() {
let res = chr_to_mp(vec!["A", "B", "C"], MpType::CharMap);
assert_eq!(res, Ok(vec!["Av", "bQ", "TG"]))
assert_eq!(res, Ok(vec!["Aj", "bQ", "TG"]))
}
#[test]
fn salt_extender_longer() {
Expand Down
8 changes: 2 additions & 6 deletions src/encpt/math/matrix.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ pub fn fill_mtrx_gaps(n: usize, orgnl_mtrx: Vec<Vec<i32>>) -> Vec<Vec<i32>> {
let mut row = c.clone(); // Create a new row based on the original row
if row.len() < n {
// If the row is shorter than n, extend it with zeros
row.extend(std::iter::repeat(0).take(n - row.len()));
row.extend(std::iter::repeat(101).take(n - row.len()));
}
row // Return the modified row
})
Expand All @@ -48,19 +48,15 @@ pub fn mtrx_to_vecs(mtrx: Vec<Vec<i32>>) -> Vec<Vec<i32>> {
if i > j {
blue.push(*sub);
} else if j > i {
println!("j : {:?} => {:?}", j, sub);
green.push(*sub);
println!("{:?}", green);
} else {
red.push(*sub);
}
}
}
println!("{:?}", green);

let res: Vec<Vec<i32>> = vec![green, red, blue];
println!("res = {:?}", res);
println!("res = {:?}", res[0]);

res
}

Expand Down
5 changes: 5 additions & 0 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,11 @@ mod encpt {
}

fn main() {
let f = "Dr0]DQSrSDD2QG2]DQDQQsGrS3rsGr";
let d = "NLhdd09DNDL0dd0dDD*Rh9DNZZ";
println!("grn : {}, ble : {}", f.len(), d.len());
//Dr0]DQSrSDD2QG2]DQDQQsGrS3rsGr
//NLhdd09DNDL0dd0dDD*Rh9DNZZ
// let _ = chr_to_mp(vec!["A", "B", "C"], MpType::CharMap);
// match salt_extender(String::from("abc"), String::from("dsdfsqdfsqdff")) {
// Ok(result) => println!("Result: {}", result),
Expand Down
27 changes: 14 additions & 13 deletions src/maps/chars.rs
Original file line number Diff line number Diff line change
@@ -1,39 +1,39 @@
pub const CHAR_MAP: [[&'static str; 3]; 85] = [
["a", "vA", "176"],
["b", "Qb", "421"],
["c", "GT", "342"],
["c", "Gm", "342"],
["d", "fd", "422"],
["e", "He", "512"],
["f", "pJ", "617"],
["g", "kF", "784"],
["h", "rs", "847"],
["h", "rc", "847"],
["i", "Mg", "967"],
["j", "OO", "993"],
["j", "OB", "993"],
["k", "Za", "832"],
["l", "rL", "735"],
["m", "sM", "672"],
["m", "sD", "672"],
["n", "qK", "561"],
["o", "wQ", "491"],
["p", "nH", "349"],
["o", "wE", "491"],
["p", "nI", "349"],
["q", "vP", "256"],
["r", "tN", "198"],
["s", "uO", "314"],
["t", "zY", "197"],
["t", "zX", "197"],
["u", "uT", "113"],
["v", "wV", "258"],
["w", "Wx", "301"],
["x", "vU", "485"],
["y", "YY", "574"],
["z", "SS", "669"],
//
["A", "Av", "671"],
["A", "Aj", "671"],
["B", "bQ", "142"],
["C", "TG", "243"],
["D", "df", "224"],
["E", "eH", "215"],
["F", "Jp", "716"],
["G", "Fk", "487"],
["H", "sr", "748"],
["H", "sR", "748"],
["I", "gM", "769"],
["J", "Io", "389"],
["K", "aZ", "238"],
Expand All @@ -42,16 +42,17 @@ pub const CHAR_MAP: [[&'static str; 3]; 85] = [
["N", "Kq", "165"],
["O", "Qw", "194"],
["P", "Hn", "923"],
["Q", "Pv", "652"],
["R", "Nt", "891"],
["S", "Ou", "430"],
["Q", "Pl", "652"],
["R", "NC", "891"],
["S", "Oh", "430"],
["T", "Yz", "790"],
["U", "Tu", "311"],
["V", "VW", "852"],
["V", "Vi", "852"],
["W", "xW", "103"],
["X", "Uv", "584"],
["Y", "yy", "475"],
["Z", "St", "966"],
//
["0", "gv", "667"],
["1", "jk", "418"],
["2", "Gt", "779"],
Expand Down
11 changes: 5 additions & 6 deletions src/shared/parse.rs
Original file line number Diff line number Diff line change
Expand Up @@ -33,17 +33,16 @@ pub fn split_by_n(n: usize, chunk: Vec<String>) -> Vec<Vec<String>> {
split_vectors
}

pub fn rem_zeros(chunk: Vec<i32>) -> (Vec<i32>, usize) {
let count_zeros = chunk.iter().filter(|&&c| c == 0).count();
let filtered_chunk: Vec<i32> = chunk.into_iter().filter(|&c| c != 0).collect();
(filtered_chunk, count_zeros)
pub fn rem_zeros(chunk: Vec<i32>) -> Vec<i32> {
let filtered_chunk: Vec<i32> = chunk.into_iter().filter(|&c| c != 101).collect();
filtered_chunk
}

pub fn concat_every_three_elements(input_vec: Vec<&str>) -> Vec<String> {
pub fn concat_every_n_elements(input_vec: Vec<&str>, n: usize) -> Vec<String> {
let mut result_vec = Vec::new();

// Iterate over the input vector in chunks of three
for chunk in input_vec.chunks(3) {
for chunk in input_vec.chunks(n) {
// Concatenate the elements in each chunk
let concatenated = chunk.join("");
result_vec.push(concatenated);
Expand Down

0 comments on commit 8fa04c1

Please sign in to comment.