use crate::crypto_systems::rsa::RSA;
use crate::error::RSAError;
use crate::prelude::*;
use crate::Traits::Encrypt;
use std::str::FromStr;
use crate::utils::BaseString;
use rug::Integer;
impl Encrypt<RSAError, BaseString, String> for RSA {
fn encrypt(&self, input: BaseString) -> Result<String, RSAError> {
let input = input.encode_asym()?.flatten();
let text_num = Integer::from_str(&input).unwrap();
let encrypted_num = text_num.secure_pow_mod(&self.e, &self.n);
Ok(encrypted_num.to_string())
}
}
#[cfg(test)]
mod test {
use super::*;
use crate::Traits::Encrypt;
#[test]
fn test_rsa_encrypt() {
let rsa = RSA::from_public(Integer::from(14317u64), Integer::from(7777u64));
let cipher_text = "ko".to_string();
let encrypted_text = rsa.encrypt(cipher_text.into()).unwrap();
assert_eq!(encrypted_text, "10169");
}
}