Advertisement
Guest User

Untitled

a guest
Sep 3rd, 2014
228
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Scala 5.30 KB | None | 0 0
  1. /**
  2.   */
  3. object Krypto {
  4.  
  5.   def teiler(n:BigInt) : List[BigInt] = {
  6.     var t:List[BigInt] = List()
  7.  
  8.     for (i:BigInt <- BigInt(1) to n) {
  9.       if ((n mod i) == BigInt(0)) {
  10.         t = t.:+(i)
  11.       }
  12.     }
  13.     t
  14.   }
  15.  
  16.   def order(n:BigInt, q:BigInt) : BigInt = {
  17.     val t = teiler(q - 1)
  18.  
  19.     for (i <- t) {
  20.       if (n.modPow(i, q) == BigInt(1)) {
  21.         return i
  22.       }
  23.     }
  24.     -1
  25.   }
  26.  
  27.   def orders(q:BigInt) : List[(BigInt, BigInt)] = {
  28.     var t:List[(BigInt, BigInt)] = List()
  29.  
  30.     for (i:BigInt <- BigInt(1) until q) {
  31.       val x = order(i, q)
  32.       t = t.:+((i, x))
  33.     }
  34.     t
  35.   }
  36.  
  37.   def primitives(q:BigInt) : List[(BigInt, BigInt)] = {
  38.     orders(q).filter(_._2 == (q-1))
  39.   }
  40.  
  41.   def units(q:BigInt) :List[BigInt] = {
  42.     var t:List[BigInt] = List()
  43.     for (i:BigInt <- BigInt(2) until q) {
  44.       if (q.gcd(i) == BigInt(1)) {
  45.         t = t.:+(i)
  46.       }
  47.     }
  48.     t
  49.   }
  50.  
  51.   def divides(d : BigInt, n : BigInt) = {
  52.     (n mod d) == BigInt(0)
  53.   }
  54.  
  55.   def ld(n : BigInt) : BigInt = {
  56.     ldf(2, n)
  57.   }
  58.  
  59.   def ldf(k : BigInt, n : BigInt) : BigInt = {
  60.     if (divides(k, n)) k
  61.     else if ((k*k) > n) n
  62.     else ldf(k + 1, n)
  63.   }
  64.  
  65.   def factors(n : BigInt) : List[BigInt] = {
  66.     if (n == BigInt(1)) {
  67.       return List()
  68.     }
  69.     val p = ld(n)
  70.     p :: factors(n / p)
  71.   }
  72.  
  73.   def euler(n : BigInt) : BigInt = {
  74.     val list = factors(n)
  75.     val list2 = list.distinct
  76.  
  77.     var euler:BigDecimal = BigDecimal(n)
  78.  
  79.     for (p <- list2) {
  80.       euler = euler * (1 - 1/BigDecimal(p))
  81.     }
  82.     euler.toBigInt()
  83.   }
  84.  
  85.   def half(n:BigInt) : BigInt = {
  86.     n >> (n.bitLength / 2)
  87.   }
  88.  
  89.   def solve(p:BigInt, q:BigInt) : (BigInt, BigInt) = {
  90.     for (a <- BigInt(1) to BigInt(1000)) {
  91.       for (b <- BigInt(1) to BigInt(1000)) {
  92.         if (a * p - b * q == BigInt(1)) {
  93.           return (a, b * (-1))
  94.         }
  95.         if (b * q - a * p == BigInt(1)) {
  96.           return (a * (-1), b)
  97.         }
  98.       }
  99.     }
  100.     (0, 0)
  101.   }
  102. }
  103.  
  104.  
  105.  
  106. /**
  107.   */
  108.  
  109. class ElGamal(q:BigInt, keyA:BigInt, keyB:BigInt) {
  110.   var a = Krypto.primitives(q).head._1
  111.   val ya = a.modPow(keyA, q)
  112.   val yb = a.modPow(keyB, q)
  113.  
  114.   def getA = a
  115.   def setA(newa : BigInt) = {
  116.     a = newa
  117.   }
  118.  
  119.   def encryptA(m:BigInt, r:BigInt) : (BigInt, BigInt) = {
  120.     val message = m * yb.modPow(r, q) mod q
  121.     val ar = a.modPow(r, q)
  122.     (message, ar)
  123.   }
  124.  
  125.   def decryptA(m:BigInt, ar:BigInt) : BigInt = {
  126.     (m * ar.modPow(q - 1 - keyB, q)) mod q
  127.   }
  128.  
  129.   def encryptB(m:BigInt, r:BigInt) : (BigInt, BigInt) = {
  130.     val message = m * ya.modPow(r, q) mod q
  131.     val ar = a.modPow(r, q)
  132.     (message, ar)
  133.   }
  134.  
  135.   def decryptB(m:BigInt, ar:BigInt) : BigInt = {
  136.     (m * ar.modPow(q - 1 - keyA, q)) mod q
  137.   }
  138.  
  139.   override def toString: String = String.format("q = %s, a = %s, ya = %s, yb = %s", q.toString(), a.toString(), ya.toString(), yb.toString())
  140. }
  141.  
  142.  
  143.  
  144.  
  145. /**
  146.   */
  147. class Rabin (p : BigInt, q : BigInt) {
  148.   val publicKey = p * q
  149.  
  150.   def getPublicKey = publicKey
  151.  
  152.   def encrypt(m : BigInt, key:BigInt) : BigInt = {
  153.     ((m << m.bitLength) + m).modPow(2, key)
  154.   }
  155.  
  156.   def decrypt(m : BigInt) : ((BigInt, BigInt, String), (BigInt, BigInt, String), (BigInt, BigInt, String), (BigInt, BigInt, String)) = {
  157.     val solve = Krypto.solve(p,q)
  158.     val a = solve._1
  159.     val b = solve._2
  160.     val r = m.modPow((p + 1) / 4, p)
  161.     val s = m.modPow((q + 1) / 4, q)
  162.     val x = (a*p*s + b*q*r) mod publicKey
  163.     val y = (a*p*s - b*q*r) mod publicKey
  164.  
  165.     ((x, Krypto.half(x), x.toString(2)), (y, Krypto.half(y), y.toString(2)),
  166.       ((x * -1) mod publicKey, Krypto.half((x * -1) mod publicKey), ((x * -1) mod publicKey).toString(2)),
  167.       ((y * -1) mod publicKey, Krypto.half((y * -1) mod publicKey), ((y * -1) mod publicKey).toString(2)))
  168.   }
  169.  
  170.   override def toString: String = String.format("publicKey = %s", publicKey.toString())
  171. }
  172.  
  173. /**
  174.   */
  175. class RSA (p: BigInt, q: BigInt, e: BigInt){
  176.   val n: BigInt = p * q
  177.   val phi: BigInt = (p - 1) * (q - 1)
  178.   if (e.gcd(phi) != BigInt(1)) {
  179.     throw new IllegalArgumentException("gcd(e, phi) != 1")
  180.   }
  181.   val d: BigInt = e.modInverse(phi)
  182.  
  183.   override def toString: String = String.format("p = %s, q = %s, e = %s, d = %s, phi = %s, n = %s", p.toString(), q.toString(), e.toString(), d.toString(), phi.toString(), (p * q).toString())
  184. }
  185.  
  186. object RSA {
  187.   def decrypt(d: BigInt, m: BigInt, n: BigInt): BigInt = {
  188.     m.modPow(d, n)
  189.   }
  190.  
  191.   def encrypt(e:BigInt, m: BigInt, n: BigInt): BigInt = {
  192.     m.modPow(e, n)
  193.   }
  194.  
  195.   def text2Number(m: String): BigInt = {
  196.     BigInt.apply(m.getBytes)
  197.   }
  198.  
  199.   def number2Text(m: BigInt): String = {
  200.     new String(m.toByteArray)
  201.   }
  202. }
  203. import scala.collection.immutable.Range.BigDecimal
  204.  
  205. /**
  206.   */
  207. class SteamCipher (bit:Int, var key:BigInt, cipher: (BigInt) => BigInt ) {
  208.   def next : BigInt = {
  209.     key = (BigInt(2).pow(bit) - 1) & key
  210.     key = (BigInt(2).pow(bit) - 1) & cipher(key)
  211.     key
  212.   }
  213. }
  214.  
  215. object SteamCipher {
  216.   def test(mem:BigInt) : BigInt = {
  217.     var ret = mem << 1
  218.     val next = mem.testBit(3) ^ mem.testBit(2) ^ mem.testBit(1) ^ mem.testBit(0)
  219.     if (next) {
  220.       ret = ret + BigInt(1)
  221.     }
  222.     ret
  223.   }
  224.   def test2(mem:BigInt) : BigInt = {
  225.     var ret = mem << 1
  226.     val next = mem.testBit(2) ^ mem.testBit(0)
  227.     if (next) {
  228.       ret = ret + BigInt(1)
  229.     }
  230.     ret
  231.   }
  232. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement