Guest User

Untitled

a guest
May 26th, 2018
108
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. Imports java.math
  2. Imports System.Diagnostics
  3.  
  4.  
  5.  
  6. Module Module1
  7.     ' this class shall be the stream cryptogropher
  8.    Private Class StreamCrypto
  9.         ' this class shall be the cryptographically secure pseudorandom number generator
  10.        ' to be used by the stream cryptographer
  11.        Private Class BlumBlumShubChar
  12.             'Blum Blum Shub (B.B.S.) is a pseudorandom number generator proposed in 1986 by
  13.            'Lenore Blum, Manuel Blum and Michael Shub (Blum et al., 1986).
  14.            'Blum Blum Shub takes the form:
  15.            'x_{n+1} = {x_n}^2 mod M
  16.            'where M=pq is the product of two large primes p and q (which will be primes just below 2^256 in our case, making M close to 2^512)
  17.            ' the constructor, takes a seed for the random number egenrator as input
  18.            Public Sub New(ByVal seed As String)
  19.                 ' this chararray will hold the seed string after its been converted to a array of ascii chars
  20.                Dim chararray(seed.Length - 1) As SByte
  21.                 ' blockmodulo is a biginteger object which will always contain the number 2^128
  22.                ' blockmodulo = 2^128
  23.                blockmodulo = New BigInteger(2)
  24.                 blockmodulo = blockmodulo.pow(128)
  25.                 ' blockmodulo = BigInteger.Pow(2, 128)
  26.                ' M is the product of 2 primes just below 256
  27.                ' M is the almost-prime modulo that will be used for all the modulos henceforth
  28.                ' this class provides M-bit encryption when used in stream cryptography
  29.                ' M = ((2^256)-189)*((2^256)-357)
  30.  
  31.                 Dim p As New BigInteger(2)
  32.                 p = p.pow(256)
  33.                 p = p.subtract(New BigInteger(357))
  34.  
  35.                 Dim q As New BigInteger(2)
  36.                 q = q.pow(256)
  37.                 q = q.subtract(New BigInteger(189))
  38.  
  39.                 'M = BigInteger.Multiply(BigInteger.Pow(2, 256) - 189, BigInteger.Pow(2, 256) - 357)
  40.                M = q.multiply(p)
  41.                 ' copy the seed string to the chararray declared earlier
  42.                For i As UInt16 = 0 To seed.Length - 1
  43.                     chararray(i) = Asc(seed.Substring(i, 1)) - 127
  44.                 Next
  45.                 ' generate the first x from the seed string in the char-array
  46.                x = New BigInteger(chararray)
  47.                 ' mod x by M
  48.                x = x.mod(M)
  49.                 ' x = BigInteger.ModPow(x, 1, M)
  50.                ' generate the first pseudorandom number, hence randomizing x
  51.                Generate()
  52.             End Sub
  53.             ' the generator returns a block of 16 bytes
  54.            ' this is equal to a number between 0 and (2^128)-1
  55.            ' so the x must be modded by 2^128 to get a 16-byte number
  56.            Public Function Generate() As Byte()
  57.                 'x_{n+1} = {x_n}^2 mod M
  58.                x = x.modPow(New BigInteger(2), M)
  59.                 ' op is the output byte array
  60.                ' it is set to x mod 2^128
  61.                Dim top As SByte() = x.mod(blockmodulo).toByteArray()
  62.                 Dim op(15) As Byte
  63.                 For i As UInt16 = 0 To 15
  64.                     op(i) = 0
  65.                 Next
  66.                 For i As UInt16 = 0 To System.Math.Min(15, top.Length - 1)
  67.                     Dim temp As Int16 = top(i)
  68.                     temp += 128
  69.                     op(i) = temp
  70.                 Next
  71.                 ' shorten the byte array to 16, we dont need any more
  72.                'Array.Resize(op, 16)
  73.                ' return op
  74.                Return op
  75.             End Function
  76.             Private blockmodulo As BigInteger
  77.             Private M As BigInteger
  78.             Private x As BigInteger
  79.         End Class
  80.  
  81.         Public Function Crypt(ByVal input As String, ByVal key As String) As String
  82.             Dim stopWatch As New Stopwatch()
  83.             stopWatch.Start()
  84.             ' create a new random number generator
  85.            Dim c As New BlumBlumShubChar(key)
  86.             ' create a chararray to hold the string *yawn*
  87.            Dim chararray(input.Length - 1) As Char
  88.             ' copy string to chararray
  89.            For i As UInt16 = 0 To input.Length - 1
  90.                 chararray(i) = input.Substring(i, 1)
  91.             Next
  92.             ' k is how far we are through the current block of random buytes
  93.            Dim k As UInt16 = 0
  94.             ' block holds the block of random bytes
  95.            Dim block(16) As Byte
  96.             ' generate and discard a block to confuse hackers
  97.            c.Generate()
  98.             ' generate a block and keep
  99.            block = c.Generate()
  100.             ' cycle through the input string
  101.            For i As UInt16 = 0 To input.Length - 1
  102.                 ' if weve polished off the current block, get a new one and reset the block counter
  103.                If k >= 16 Then
  104.                     c.Generate()
  105.                     block = c.Generate()
  106.                     k = 0
  107.                 End If
  108.                 ' exclusive-or the current byte in the block counter with the ascii code of the priginal string
  109.                ' and store back into the string
  110.                'Console.WriteLine((block(k)))
  111.                chararray(i) = Chr(block(k) Xor Asc(chararray(i)))
  112.                 ' increment block counter
  113.                k += 1
  114.             Next
  115.             ' copy the char array back into a new string and return
  116.            Dim output As String = ""
  117.             For i As UInt16 = 0 To input.Length - 1
  118.                 output &= chararray(i)
  119.             Next
  120.             stopWatch.Stop()
  121.             Console.WriteLine("This took " & stopWatch.ElapsedMilliseconds & " milliseconds ")
  122.             Return output
  123.         End Function
  124.     End Class
  125.  
  126.     Sub Main()
  127.         'Dim lol As New StreamCrypto.BlumBlumShubChar("hammertime")
  128.        'While True
  129.        '    Console.WriteLine(Chr(lol.Generate()))
  130.        '    'Console.ReadLine()
  131.        'End While
  132.        Dim lol As New StreamCrypto
  133.         While True
  134.             Console.WriteLine("i can has key?")
  135.             Dim key As String = Console.ReadLine()
  136.             Console.WriteLine("i can haz plaintext?")
  137.             Dim plaintext As String = Console.ReadLine()
  138.             Console.WriteLine("i gives you ciphertext:")
  139.             Dim ciphertext As String = lol.Crypt(plaintext, key)
  140.             Console.WriteLine(ciphertext)
  141.             Console.WriteLine("i gives you plaintext again:")
  142.             Console.WriteLine(lol.Crypt(ciphertext, key))
  143.             Console.ReadLine()
  144.         End While
  145.     End Sub
  146.  
  147. End Module
Add Comment
Please, Sign In to add comment