- Joined
- Apr 24, 2012
- Messages
- 5,113
JASS:
library RSA /* v1.0
************************************************************
*
* RSA Cryptosystem
*
* --------------------
*
* A Cryptosystem that uses the Euler's totient function
* in encrypting/decrypting numbers
*
*
************************************************************
*
* API
*
* static method create takes player temp returns thistype
* - generates RSA keys for player "temp"
*
* method encrypt takes thistype p, integer m returns integer
* - encrypts a number by using "p" key
*
* method decrypt takes integer m returns integer
* - decrypts an encrypted number
*
************************************************************/
globals
private constant integer MIN_KEY = 1024
private constant integer MAX_KEY = 8192
private constant boolean USE_PHI = false
endglobals
struct RSA extends array
private integer pbKey
private integer pvKey
private integer p
private integer q
private integer n
private integer f
private integer e
private integer d
private player owner
private static boolean array primes
/************************************************************
*
* Math libraries for encryption/decryption
*
************************************************************/
private static method mod takes integer a, integer b returns integer
return ModuloInteger(a, b)
endmethod
private static method isPrime takes integer i returns boolean
local integer a
local integer c = 1
if primes[i] == null then
if mod(i, 1) != 0 and i<2 then
return false
endif
set a = R2I(SquareRoot(I2R(i)))
loop
if mod(i, c) == 0 then
return false
endif
exitwhen c == a
set c = c + 1
endloop
set primes[i] = true
return true
endif
return true
endmethod
private static method gcd takes integer a, integer b returns integer
local integer i = 0
if b != 0 then
loop
set i = a
set a = b
set b = mod(i, b)
if b == 0 then
return a
endif
endloop
endif
return a
endmethod
private static method lcm takes integer a, integer b returns integer
return a*b/gcd(a, b)
endmethod
private static method isCoprime takes integer a, integer b returns boolean
return gcd(a, b) == 1
endmethod
private static method totient takes integer n returns integer
local integer count = 0
local integer i = 1
loop
if coprime(i, n) then
set count = count + 1
endif
exitwhen i == n
endloop
return count
endmethod
private static method randomPrime takes nothing returns integer
local integer pr
loop
set pr = RandomInt(MIN_KEY, MAX_KEY)
if isPrime(pr) then
return pr
endif
endloop
endmethod
private static method randomCoprime takes integer max returns integer
local integer c
loop
set c = RandomInt(2, max - 1)
if isCoprime(c, max) then
return c
endif
endloop
endmethod
/************************************************************
*
* Main functions/methods
*
************************************************************/
static method create takes player temp returns thistype
local thistype this = thistype(GetPlayerId(temp))
set owner = temp
set p = randomPrime()
set q = randomPrime()
set n = p*q
if USE_PHI then
set f = totient(n)
else
set f = lcm(p - 1, q - 1)
endif
set e = randomCoprime(f)
set d = R2I(ModuloReal(1/I2R(e), I2R(f))
set pbKey = e
set pvKey = d
return this
endmethod
method encrypt takes thistype p, integer m returns integer
return mod(R2I(Pow(I2R(m), I2R(p.pbKey))), p.n)
endmethod
method decrypt takes integer m returns integer
return mod(R2I(Pow(I2R(m), I2R(pvKey))), n)
endmethod
endstruct
endlibrary
Example:
JASS:
struct Tester extends array
private static method onInit takes nothing returns nothing
local integer i = RSA.create(Player(0))
local integer t = RSA.create(Player(1))
// "Send" 1000 to "t" from "i"
local integer m = i.encrypt(t, 1000)
// Decrypt message from "i"
call BJDebugMsg(I2S(t.decrypt(m)))
endmethod
endstruct
Last edited: