Upload files to "crypto"
This commit is contained in:
1 parent
d8784bc373
commit
d44611f39b
3 files changed
+116
-1
No files matched your search
@@ -188,7 +188,9 @@ if __name__ == "__main__":
|
|||||||
287390,
|
287390,
|
||||||
480837,
|
480837,
|
||||||
}
|
}
|
||||||
print("#2:", *(f"gcd({m}, {n}) = {gcd(m,n)}" for m, n in cooked(E2)), sep="\n")
|
dih = [f"gcd({m}, {n}) = {gcd(m,n)}" for m, n in cooked(E2)]
|
||||||
|
print("#2:", *dih, sep="\n")
|
||||||
|
print(f"pairs = {len(dih)}")
|
||||||
print("#3:")
|
print("#3:")
|
||||||
fahhh(856, 25, 7)
|
fahhh(856, 25, 7)
|
||||||
print(f"{pow(856, 25, 7) = }")
|
print(f"{pow(856, 25, 7) = }")
|
||||||
|
|||||||
@@ -0,0 +1,40 @@
|
|||||||
|
def e1():
|
||||||
|
s = "flow"
|
||||||
|
|
||||||
|
return int(bytes(s, encoding="ascii").hex(), 16)
|
||||||
|
|
||||||
|
|
||||||
|
def dec_ascii(i: int):
|
||||||
|
b = bytes.fromhex(f"{i:x}")
|
||||||
|
return b.decode("ascii")
|
||||||
|
|
||||||
|
|
||||||
|
def e2():
|
||||||
|
i = 14445
|
||||||
|
return dec_ascii(i)
|
||||||
|
|
||||||
|
|
||||||
|
from proto_rsa import LdaRsahh
|
||||||
|
|
||||||
|
|
||||||
|
def e3():
|
||||||
|
a = LdaRsahh(p=251, q=191, e=65537).enc(26729)
|
||||||
|
|
||||||
|
b = LdaRsahh(p=233, q=151, d=9473).dec(29600)
|
||||||
|
|
||||||
|
return a, b
|
||||||
|
|
||||||
|
|
||||||
|
def e4():
|
||||||
|
C = 412589464
|
||||||
|
M = LdaRsahh(p=81401, q=27109, e=65537).dec(C)
|
||||||
|
return dec_ascii(M)
|
||||||
|
|
||||||
|
|
||||||
|
if __name__ == "__main__":
|
||||||
|
print("#1:", e1())
|
||||||
|
print("#2:", e2())
|
||||||
|
C, M = e3()
|
||||||
|
print("#3:", f"{C = }, {M = }")
|
||||||
|
print("#4:", e4())
|
||||||
|
|
||||||
@@ -0,0 +1,73 @@
|
|||||||
|
def inv(a, n):
|
||||||
|
return pow(a, -1, n)
|
||||||
|
|
||||||
|
|
||||||
|
import math
|
||||||
|
from functools import cached_property
|
||||||
|
from typing import Callable
|
||||||
|
import warnings
|
||||||
|
|
||||||
|
|
||||||
|
class LdaRsahh:
|
||||||
|
def __init__(
|
||||||
|
self,
|
||||||
|
p,
|
||||||
|
q,
|
||||||
|
d: int | None = None,
|
||||||
|
e: int | None = None,
|
||||||
|
ln_ops: Callable[[int, int], int] | None = None,
|
||||||
|
):
|
||||||
|
# operator.mul or math.lcm
|
||||||
|
self._lnops = ln_ops or math.lcm
|
||||||
|
self._p, self._q = p, q
|
||||||
|
if d is None and e is None:
|
||||||
|
raise ValueError("at least d or e needs to be math.gcd(e, phi)fed in")
|
||||||
|
ln = self.ln
|
||||||
|
if e is None:
|
||||||
|
e = inv(d, ln)
|
||||||
|
if not 1 < e < ln:
|
||||||
|
warnings.warn(
|
||||||
|
f"{e = }, {ln = }, 1 < e < ln not satisfied, e rebinds to e % ln",
|
||||||
|
UserWarning,
|
||||||
|
)
|
||||||
|
e %= ln
|
||||||
|
if not 1 < e < ln:
|
||||||
|
raise ValueError(f"{e = }, {ln = }, 1 < e < ln not satisfied")
|
||||||
|
if math.gcd(e, ln) != 1:
|
||||||
|
raise ValueError(
|
||||||
|
f"{e = }, {ln = }, gcd(e, ln) == 1 (coprime) not satisfied"
|
||||||
|
)
|
||||||
|
if d is None:
|
||||||
|
d = inv(e, ln)
|
||||||
|
self._d, self._e = d, e
|
||||||
|
|
||||||
|
@cached_property
|
||||||
|
def n(self):
|
||||||
|
return self.p * self.q
|
||||||
|
|
||||||
|
@cached_property
|
||||||
|
def ln(self):
|
||||||
|
return self._lnops((self.p - 1), (self.q - 1))
|
||||||
|
|
||||||
|
@property
|
||||||
|
def p(self):
|
||||||
|
return self._p
|
||||||
|
|
||||||
|
@property
|
||||||
|
def q(self):
|
||||||
|
return self._q
|
||||||
|
|
||||||
|
@property
|
||||||
|
def d(self):
|
||||||
|
return self._d
|
||||||
|
|
||||||
|
@property
|
||||||
|
def e(self):
|
||||||
|
return self._e
|
||||||
|
|
||||||
|
def enc(self, m: int):
|
||||||
|
return pow(m, self.e, self.n)
|
||||||
|
|
||||||
|
def dec(self, c: int):
|
||||||
|
return pow(c, self.d, self.n)
|
||||||
|
|
||||||
Reference in new issue
Block a user