Skip to content
Snippets Groups Projects
Commit ae9f55e6 authored by Oulawi's avatar Oulawi
Browse files

Working on encryption

parent 6e978b68
No related branches found
No related tags found
No related merge requests found
use num_bigint::{BigUint};
use rand::prelude::*
type message = Vec<Bool>;
type ciphertext = Vec<BigUint>;
pub fn encrypt(message: &message, n: &BigUint, y: &BigUint) {
let mut res: ciphertext = Vec<BigUint>::with_capacity(message.len());
let mut rng = rand::thread_rng();
for i in 0..message.len() {
let xi = rand_zn(n);
if message[i] {
res[i] = y*xi*xi % n;
} else {
res[i] = xi.modpow(&BigUint::from(2u32), n);
}
}
}
mod prime_gen;
use rand::seq::SliceRandom;
fn main() {
let p = prime_gen::gen_prime(512);
println!("{}", p);
println!("{}", p.bits());
let mut rng = rand::thread_rng();
let p1 = prime_gen::gen_prime(512);
let mut p2 = prime_gen::gen_prime(512);
while p1 == p2 {
p2 = prime_gen::gen_prime(512)
}
//p1 and p2 are unequal primes (private key)
let n = p1*p2; //n is the public key together with some non-residue y to be computed
let mut y = astar(n).choose(rng); //astar (vec<bigUInt>) is the coprimes of n with jacobi symbol 1
while is_residue(n, y) {
//We regenerate y until its not a quadratic residue
y = astar(n).choose(rng);
}
//Now (n, y) is the public key
let message = "Hello world".to_string();
let cipher = encrypt(&n, &y, &message);
}
......@@ -40,7 +40,7 @@ fn divides(b: &BigUint, d: u64) -> bool {
return false;
}
pub fn divide_small_primes(n: &BigUint) -> bool {
fn divide_small_primes(n: &BigUint) -> bool {
let small_primes = erastothenes_sieve(20000);
for m in small_primes {
if divides(n, m) {
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment