Files

grsa.src
  • //Imported <Bltings> Functions
  • factors = function(value)
  • Temp = []
  • for i in range(1, floor(sqrt(value)) + 1)
  • if value % i == 0 then Temp.push([i, floor(value / i)])
  • end for
  • Factors = []
  • for pair in Temp
  • for item in pair
  • Factors.push(item)
  • end for
  • end for
  • return Factors.set.sort
  • end function
  • factorsPrime = function(number)
  • Factors = [1]
  • while not isPrime(number)
  • number = floor(number / Factors[-1])
  • for i in factors(number)
  • if isPrime(i) then
  • Factors.push(i)
  • break
  • end if
  • end for
  • end while
  • return Factors[1:]
  • end function
  • isPrime = function(number)
  • if number == 1 then return false
  • return factors(number).len == 2
  • end function
  • list.set = function
  • Values = []
  • for i in self
  • if Values.indexOf(i) == null then Values.push(i)
  • end for
  • return Values
  • end function
  • //Extra Functions
  • nmod = function(a, b)
  • if a >= 0 then return a % b
  • while a < 0
  • a = a + b
  • end while
  • return a
  • end function
  • expmod = function(a, b, m)
  • r = 1
  • while b > 0
  • if b % 2 == 1 then r = (r * a) % m
  • b = floor(b / 2)
  • a = a ^ 2 % m
  • end while
  • return r
  • end function
  • modinv = function(a, b)
  • x = 0; y = 1; u = 1; v = 0; c = b
  • while a != 0
  • q = floor(c / a)
  • r = nmod(c, a)
  • m = x - u * q
  • n = y - v * q
  • c = a; a = r; x = u; y = v; u = m; v = n
  • end while
  • if c != 1 then return null else return nmod(x, b)
  • end function
  • //Key Pair Generation
  • genKeyPair = function(primeLength)
  • minimum = "1" * primeLength
  • maximum = "9" * primeLength
  • Primes = []
  • for i in range(minimum.to_int, maximum.to_int)
  • if isPrime(i) then Primes.push(i)
  • end for
  • pair = []
  • for i in range(2)
  • x = rnd * (Primes.len - 1)
  • pair.push(Primes[x])
  • Primes.remove(x)
  • end for
  • n = pair[0] * pair[1]; c = (pair[0] - 1) * (pair[1] - 1)
  • excludes = factorsPrime(n) + factorsPrime(c)
  • excludes = excludes.set
  • includes = []
  • for i in range(2, c - 1)
  • if excludes.indexOf(i) == null and isPrime(i) then includes.push(i)
  • end for
  • e = includes[rnd * (includes.len - 1)]
  • return [[e, n], [modinv(e, c), n]]
  • end function
  • //Encrypt & Decrypt Functions
  • encrypt = function(text, publickey)
  • nums = []
  • for i in range(0, text.len - 1)
  • nums.push(expmod(text[i].code, publickey[0], publickey[1]))
  • end for
  • return nums
  • end function
  • decrypt = function(nums, privatekey)
  • chrs = []
  • for i in range(0, nums.len - 1)
  • chrs.push(char(expmod(nums[i], privatekey[0], privatekey[1])))
  • end for
  • return chrs.join("")
  • end function
  • numstochars = function(nums)
  • chars = []
  • for i in nums
  • chars.push(char(i))
  • end for
  • return chars.join("")
  • end function
  • charstonums = function(chars)
  • nums = []
  • for i in chars
  • nums.push(i.code)
  • end for
  • return nums
  • end function
  • //Full Encyrpt & Decrypt Functions
  • fullencrypt = function(text, publickey)
  • return numstochars(encrypt(text, publickey))
  • end function
  • fulldecrypt = function(text, privatekey)
  • return decrypt(charstonums(text), privatekey)
  • end function