feat: initial sea128 encrypt is working
This commit is contained in:
parent
c416547067
commit
c21c11aed0
5 changed files with 89 additions and 7 deletions
|
|
@ -6,5 +6,6 @@ edition = "2021"
|
||||||
[dependencies]
|
[dependencies]
|
||||||
anyhow = "1.0.90"
|
anyhow = "1.0.90"
|
||||||
base64 = "0.22.1"
|
base64 = "0.22.1"
|
||||||
|
openssl = "0.10.68"
|
||||||
serde = { version = "1.0.210", features = ["derive"] }
|
serde = { version = "1.0.210", features = ["derive"] }
|
||||||
serde_json = "1.0"
|
serde_json = "1.0"
|
||||||
|
|
|
||||||
|
|
@ -1,6 +1,6 @@
|
||||||
use std::{str::Bytes, string};
|
use std::{str::Bytes, string};
|
||||||
|
|
||||||
use crate::utils::poly::{self, get_coefficients};
|
use crate::utils::poly::{self, b64_2_num, get_coefficients};
|
||||||
use anyhow::Result;
|
use anyhow::Result;
|
||||||
use base64::prelude::*;
|
use base64::prelude::*;
|
||||||
use serde_json::Value;
|
use serde_json::Value;
|
||||||
|
|
@ -9,11 +9,7 @@ pub fn block2poly(val: &Value) -> Result<Vec<u8>> {
|
||||||
// Convert JSON data in to a u128
|
// Convert JSON data in to a u128
|
||||||
// TODO: Transfer decoding into own function?
|
// TODO: Transfer decoding into own function?
|
||||||
let string: String = serde_json::from_value(val["block"].clone())?;
|
let string: String = serde_json::from_value(val["block"].clone())?;
|
||||||
let decoded: Vec<u8> = BASE64_STANDARD.decode(string)?;
|
let number = b64_2_num(&string)?;
|
||||||
|
|
||||||
let mut bytes: [u8; 16] = [0u8; 16];
|
|
||||||
bytes.copy_from_slice(&decoded);
|
|
||||||
let number: u128 = <u128>::from_ne_bytes(bytes);
|
|
||||||
|
|
||||||
let coefficients: Vec<u8> = get_coefficients(number);
|
let coefficients: Vec<u8> = get_coefficients(number);
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -1,3 +1,3 @@
|
||||||
pub mod block2poly;
|
pub mod block2poly;
|
||||||
pub mod poly2block;
|
pub mod poly2block;
|
||||||
|
pub mod sea128;
|
||||||
|
|
|
||||||
59
src/tasks/tasks01/sea128.rs
Normal file
59
src/tasks/tasks01/sea128.rs
Normal file
|
|
@ -0,0 +1,59 @@
|
||||||
|
use anyhow::Result;
|
||||||
|
use base64::prelude::*;
|
||||||
|
use openssl::symm::{Cipher, Crypter, Mode};
|
||||||
|
use serde_json::Value;
|
||||||
|
|
||||||
|
use crate::utils::poly::b64_2_num;
|
||||||
|
|
||||||
|
pub fn sea128(args: &Value) -> Result<String> {
|
||||||
|
let key_string: String = serde_json::from_value(args["key"].clone())?;
|
||||||
|
//let key: &[u8] = b64_2_num(key_string)?.to_ne_bytes();
|
||||||
|
let key = BASE64_STANDARD.decode(key_string)?;
|
||||||
|
eprintln!("{:?}", key);
|
||||||
|
|
||||||
|
let plaintexts_string: String = serde_json::from_value(args["input"].clone())?;
|
||||||
|
//let plaintexts: &[u8] = &b64_2_num(plaintexts_string)?.to_ne_bytes();
|
||||||
|
let plaintexts = BASE64_STANDARD.decode(plaintexts_string)?;
|
||||||
|
eprintln!("{:?}", plaintexts);
|
||||||
|
|
||||||
|
let xor_val: u128 = 0xc0ffeec0ffeec0ffeec0ffeec0ffee11;
|
||||||
|
|
||||||
|
let mut encrypter = Crypter::new(Cipher::aes_128_ecb(), Mode::Encrypt, &key, None)?;
|
||||||
|
encrypter.pad(false);
|
||||||
|
|
||||||
|
let mut ciphertext = [0; 32].to_vec();
|
||||||
|
|
||||||
|
let mut count = encrypter.update(&plaintexts, &mut ciphertext)?;
|
||||||
|
count += encrypter.finalize(&mut ciphertext)?;
|
||||||
|
ciphertext.truncate(count);
|
||||||
|
|
||||||
|
eprintln!("{:?}", &ciphertext[..]);
|
||||||
|
|
||||||
|
let mut bytes: [u8; 16] = [0u8; 16];
|
||||||
|
bytes.copy_from_slice(&ciphertext);
|
||||||
|
let number: u128 = <u128>::from_be_bytes(bytes);
|
||||||
|
|
||||||
|
let output = BASE64_STANDARD.encode((number ^ xor_val).to_be_bytes());
|
||||||
|
|
||||||
|
Ok(output)
|
||||||
|
}
|
||||||
|
|
||||||
|
#[cfg(test)]
|
||||||
|
mod tests {
|
||||||
|
use std::fs;
|
||||||
|
|
||||||
|
use anyhow::Result;
|
||||||
|
use serde_json::json;
|
||||||
|
// Note this useful idiom: importing names from outer (for mod tests) scope.
|
||||||
|
use super::*;
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn test_sea128() -> Result<()> {
|
||||||
|
let args =
|
||||||
|
json!({"key" : "istDASeincoolerKEYrofg==", "input" : "yv66vvrO263eyviIiDNEVQ=="});
|
||||||
|
|
||||||
|
assert_eq!(sea128(&args)?, "D5FDo3iVBoBN9gVi9/MSKQ==");
|
||||||
|
|
||||||
|
Ok(())
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
@ -1,3 +1,4 @@
|
||||||
|
use anyhow::Result;
|
||||||
use base64::prelude::*;
|
use base64::prelude::*;
|
||||||
use std::{fmt::format, str::FromStr, u128, u8};
|
use std::{fmt::format, str::FromStr, u128, u8};
|
||||||
|
|
||||||
|
|
@ -19,6 +20,16 @@ pub fn get_alpha_rep(num: u128) -> String {
|
||||||
alpha_rep
|
alpha_rep
|
||||||
}
|
}
|
||||||
|
|
||||||
|
pub fn b64_2_num(string: &String) -> Result<u128> {
|
||||||
|
let decoded: Vec<u8> = BASE64_STANDARD.decode(string)?;
|
||||||
|
|
||||||
|
let mut bytes: [u8; 16] = [0u8; 16];
|
||||||
|
bytes.copy_from_slice(&decoded);
|
||||||
|
let number: u128 = <u128>::from_ne_bytes(bytes);
|
||||||
|
|
||||||
|
Ok(number)
|
||||||
|
}
|
||||||
|
|
||||||
pub fn get_coefficients(num: u128) -> Vec<u8> {
|
pub fn get_coefficients(num: u128) -> Vec<u8> {
|
||||||
let mut powers: Vec<u8> = vec![];
|
let mut powers: Vec<u8> = vec![];
|
||||||
for shift in 0..128 {
|
for shift in 0..128 {
|
||||||
|
|
@ -54,6 +65,8 @@ pub fn coefficient_to_binary(coefficients: Vec<u8>) -> u128 {
|
||||||
|
|
||||||
#[cfg(test)]
|
#[cfg(test)]
|
||||||
mod tests {
|
mod tests {
|
||||||
|
use crate::utils::poly::b64_2_num;
|
||||||
|
use anyhow::Result;
|
||||||
// Note this useful idiom: importing names from outer (for mod tests) scope.
|
// Note this useful idiom: importing names from outer (for mod tests) scope.
|
||||||
use super::*;
|
use super::*;
|
||||||
|
|
||||||
|
|
@ -88,4 +101,17 @@ mod tests {
|
||||||
"ARIAAAAAAAAAAAAAAAAAgA=="
|
"ARIAAAAAAAAAAAAAAAAAgA=="
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn test_b64_2_num() -> Result<()> {
|
||||||
|
let b64_payload: String = String::from_str("juMqbhnlBwAAAAAAAAAAAA==")?;
|
||||||
|
assert_eq!(
|
||||||
|
b64_2_num(&b64_payload)?,
|
||||||
|
2222222222222222,
|
||||||
|
"Error: Value was: {}",
|
||||||
|
b64_2_num(&b64_payload)?
|
||||||
|
);
|
||||||
|
|
||||||
|
Ok(())
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue