Skip to content

Commit

Permalink
Merge pull request #11 from Ponjinge/rsa_ecc_sub_branch
Browse files Browse the repository at this point in the history
End of Rsa ecc sub branch
  • Loading branch information
Ponjinge authored Jan 29, 2024
2 parents 7323676 + 8959a41 commit 470d2b2
Show file tree
Hide file tree
Showing 4 changed files with 132 additions and 79 deletions.
172 changes: 101 additions & 71 deletions Python/Classic Encryption Methods .ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -548,7 +548,7 @@
" if math.sqrt(n + b**2) == math.isqrt(n + b**2) : \n",
" possible_vals.append( ( int( b + math.sqrt(n + b**2) ), int(math.sqrt(n + b**2)-b)))\n",
" \n",
" return (possible_vals)"
" return (possible_vals)\n"
]
},
{
Expand Down Expand Up @@ -586,7 +586,7 @@
},
{
"cell_type": "code",
"execution_count": 64,
"execution_count": 65,
"metadata": {},
"outputs": [
{
Expand All @@ -595,7 +595,7 @@
"[(9671881, 2828971)]"
]
},
"execution_count": 64,
"execution_count": 65,
"metadata": {},
"output_type": "execute_result"
}
Expand All @@ -604,6 +604,104 @@
"fermat_method_simple(p*q)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Now we will use this Fermat's factorisation algorithm again, but this time check from the middle, we are guessing that p and q are close together. "
]
},
{
"cell_type": "code",
"execution_count": 90,
"metadata": {},
"outputs": [],
"source": [
"def fermat_method_close(n):\n",
" \n",
" possible_vals = []\n",
" \n",
" for b in range(int(math.sqrt(n)), 0, -1):\n",
" if math.sqrt(n + b**2) == math.isqrt(n + b**2) : \n",
" possible_vals.append( ( int( b + math.sqrt(n + b**2) ), int(math.sqrt(n + b**2)-b)))\n",
" \n",
" return (possible_vals)"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"# Now let's compare \n",
"\n",
"start = time.time()\n",
"\n",
"p, q = 2828971, 9671881\n",
"\n",
"\n",
"if (p,q) in fermat_method_simple(p*q) or (q,p) in fermat_method_simple(p*q) :\n",
" print ('Correct')\n",
"\n",
"end = time.time()\n",
"\n",
"print(\"The time of execution of the simple loop is :\",\n",
" (end-start) * 10**3, \"ms\")\n",
"\n",
"start = time.time()\n",
"\n",
"if (p,q) in fermat_method_simple(p*q) or (q,p) in fermat_method_close(p*q) :\n",
" print ('Correct')\n",
"\n",
"end = time.time()\n",
"\n",
"print(\"The time of execution of the close version is :\",\n",
" (end-start) * 10**3, \"ms\")"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"import sympy\n",
"# Lets get a close to adjacent prime to 350185187058915769990977340231\n",
"sympy.nextprime(sympy.nextprime(sympy.nextprime(sympy.nextprime(350185187058915769990977340231))))"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"# Lets test it on a massive prime factorisation that would take >1h on the first function\n",
"\n",
"p, q = 350185187058915769990977340231, 350185187058915769990977340643\n",
"\n",
"start = time.time()\n",
"\n",
"if (p,q) in fermat_method_close(p*q) or (q,p) in fermat_method_close(p*q) :\n",
" print ('Correct')\n",
"\n",
"end = time.time()\n",
"\n",
"print(\"The time of execution of the close version is :\",\n",
" (end-start) * 10**3, \"ms\")"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"n= p*q\n",
"fermat_method_close(p*q)\n"
]
},
{
"cell_type": "markdown",
"metadata": {},
Expand Down Expand Up @@ -653,74 +751,6 @@
"Examples include SHA-1, SHA-256, SHA-384, and SHA-512."
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": []
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": []
},
{
"cell_type": "code",
"execution_count": 2,
"metadata": {},
"outputs": [],
"source": [
"def say_hello():\n",
" print(\"Hello\")\n",
" print(\"Raphael\")"
]
},
{
"cell_type": "code",
"execution_count": 3,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Hello\n",
"Raphael\n"
]
}
],
"source": [
"say_hello()"
]
},
{
"cell_type": "code",
"execution_count": 8,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"0\n",
"1\n",
"2\n",
"3\n",
"4\n"
]
}
],
"source": [
"a = 5 \n",
"i = 0\n",
"while i<a:\n",
" print(i)\n",
" i = i + 1 "
]
},
{
"cell_type": "code",
"execution_count": null,
Expand Down
14 changes: 7 additions & 7 deletions Rust/encryptions/Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion Rust/encryptions/Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
[package]
name = "Cryptography"
name = "encryptions"
version = "0.1.0"
edition = "2021"

Expand Down
23 changes: 23 additions & 0 deletions Rust/encryptions/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,17 @@ impl UserWeak {
UserWeak { name, prime_p, prime_q, e, n, eulers_totient, d, inbox: Vec::new(), }
}

fn encrypt_message(&self, message: &BigInt) -> BigInt {
message.modpow(&self.e, &self.n)
}

fn decrypt_message(&self, cipher: &BigInt) -> BigInt {
cipher.modpow(&self.d, &self.n)
}

fn send_message(&self, recipient: UserWeak, message: &i32)-> () {

}


}
Expand All @@ -48,6 +59,8 @@ fn mod_inv(a: &BigInt, m: &BigInt) ->Option<BigInt>{





fn main() {
// Example prime numbers and exponent for RSA. In a real application, choose large primes.
//John = User_weak("John", 27647, 60041, 1579)
Expand All @@ -57,7 +70,17 @@ fn main() {

let user = UserWeak::new("John Doe".to_string(), prime_p, prime_q, e);


// Let us check is a user can be created
println!("User created: {:?}", user);

// Encrypt and Decrypt a message (here a BigInt)
let mess = BigInt::from(123u32);
let cipher = user.encrypt_message(&mess);
println!("{}", cipher);
let mess2 = user.decrypt_message(&cipher);
println!("{}", mess2);
}



0 comments on commit 470d2b2

Please sign in to comment.