def randKey(self):
     p = CryptoMath.randPrime()
     q = CryptoMath.randPrime()
     self._N = p * q
     PhiN = (p - 1) * (q - 1)
     self._e = CryptoMath.randPrime()
     self._d = Euclidean.extendedEuclideanAlgorythm(PhiN, self._e)["y"]
     self._d = self._d % PhiN
     while self._d <= 0:
         self._d += PhiN
 def getError(self):
     if not self._N < 4:
         return "N smaller 4: " + str(self._N)
     if not CryptoMath.isPrime(self._e):
         return "e not Prime: " + str(self._e)
     if self._d <= 0:
         return "d smaller 1: " + str(self._d)
     return str()
Example #3
0
import sys
from CryptoMath import CryptoMath

if len(sys.argv) > 0:
    m = int(sys.argv[1])
    if m < 1:
        print("Error: Input value must be greater than 0")
        sys.exit(1)
    CryptoMath.getEulerPhi(m)
else:
    print("python3 ph.py m")
    sys.exit(1)
Example #4
0
import sys
from CryptoMath import CryptoMath

if len(sys.argv) == 2:
    n = int(sys.argv[1])
    elements = CryptoMath.getElements(n)
    cardinality = CryptoMath.getCardinality(n)
    print("Elements:    {0}".format(elements))
    print("Cardinality: {0}".format(cardinality))
else:
    print("Usage: python cardinality.py n")
    sys.exit(1)
Example #5
0
import sys
from CryptoMath import CryptoMath

if len(sys.argv) < 3:
    print("Usage: getGCD.py a b")
    sys.exit(1)

a = int(sys.argv[1])
b = int(sys.argv[2])

print(CryptoMath.getGCD(a, b))
Example #6
0
import sys
from CryptoMath import CryptoMath

if len(sys.argv) < 3:
    print("Usage: modular-inverse-naive.py A q")
    sys.exit(1)

A = int(sys.argv[1])
m = int(sys.argv[2])

r = CryptoMath.getModularInverseNaive(A, m)

if r is None:
    print(
        "None since it's likely that {0} and {1} are not coprime/relatively prime"
        .format(A, m))
else:
    print(r)
Example #7
0
import math
import sys
from CryptoMath import CryptoMath

# In math, a number is said to be divisible by another number if the remainder is 0.
# a^x = a (mod x)
# Divide both sides by a
# a^(x -1) = 1 (mod x)
# x^a / x^b = x^(a-b)
# a^x /a^1 = a^(x-1)

if len(sys.argv) > 1:
    congruent = "\u2261"
    a = int(sys.argv[1])
    p = int(sys.argv[2])

    if CryptoMath.isPrime(p):
        print("Using Fermat since p is prime")
        print("a^-1 \u2261 a^(p-2) (mod p) if p is prime")
        print("{0}^-1 \u2261 {0}^({1}-2) (mod {1})".format(a, p))
        print("{0}^({1}) (mod {2})".format(a, p - 2, p))
        print("{0} (mod {1})".format(a**(p - 2), p))
        print("{0}".format(a**(p - 2) % p))
    else:
        print("Using Euler since p is NOT prime")
Example #8
0
import sys
from CryptoMath import CryptoMath

if len(sys.argv) > 2:
    print("Usage: divisors.py num")
    sys.exit(1)

d = CryptoMath.getDivisors(int(sys.argv[1]))

if len(d) == 2:
    print("{0} [PRIME]".format(d))
else:
    print("{0}".format(d))
Example #9
0
import sys
from CryptoMath import CryptoMath

if len(sys.argv) > 1:
    r0 = int(sys.argv[1])
    r1 = int(sys.argv[2])
    if r0 < 1 or r1 < 1:
        print("Error: Input values must be greater than 0")
        sys.exit(1)
    #gcd = CryptoMath.getEuclideanGcd(r0, r1)
    #print(gcd)
    #CryptoMath.getGcd(r0, r1)
    CryptoMath.getExtendedEuclideanGcd(r0, r1)
    #print(CryptoMath.xgcd(r0, r1))
else:
    print("python3 gcd.py r1 r2")
    sys.exit(1)
 def isEncryptValid(self):
     if self._N < 4:
         return False
     if not CryptoMath.isPrime(self._e):
         return False
     return True