Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- Imports java.math
- Imports System.Diagnostics
- Module Module1
- ' this class shall be the stream cryptogropher
- Private Class StreamCrypto
- ' this class shall be the cryptographically secure pseudorandom number generator
- ' to be used by the stream cryptographer
- Private Class BlumBlumShubChar
- 'Blum Blum Shub (B.B.S.) is a pseudorandom number generator proposed in 1986 by
- 'Lenore Blum, Manuel Blum and Michael Shub (Blum et al., 1986).
- 'Blum Blum Shub takes the form:
- 'x_{n+1} = {x_n}^2 mod M
- '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)
- ' the constructor, takes a seed for the random number egenrator as input
- Public Sub New(ByVal seed As String)
- ' this chararray will hold the seed string after its been converted to a array of ascii chars
- Dim chararray(seed.Length - 1) As SByte
- ' blockmodulo is a biginteger object which will always contain the number 2^128
- ' blockmodulo = 2^128
- blockmodulo = New BigInteger(2)
- blockmodulo = blockmodulo.pow(128)
- ' blockmodulo = BigInteger.Pow(2, 128)
- ' M is the product of 2 primes just below 256
- ' M is the almost-prime modulo that will be used for all the modulos henceforth
- ' this class provides M-bit encryption when used in stream cryptography
- ' M = ((2^256)-189)*((2^256)-357)
- Dim p As New BigInteger(2)
- p = p.pow(256)
- p = p.subtract(New BigInteger(357))
- Dim q As New BigInteger(2)
- q = q.pow(256)
- q = q.subtract(New BigInteger(189))
- 'M = BigInteger.Multiply(BigInteger.Pow(2, 256) - 189, BigInteger.Pow(2, 256) - 357)
- M = q.multiply(p)
- ' copy the seed string to the chararray declared earlier
- For i As UInt16 = 0 To seed.Length - 1
- chararray(i) = Asc(seed.Substring(i, 1)) - 127
- Next
- ' generate the first x from the seed string in the char-array
- x = New BigInteger(chararray)
- ' mod x by M
- x = x.mod(M)
- ' x = BigInteger.ModPow(x, 1, M)
- ' generate the first pseudorandom number, hence randomizing x
- Generate()
- End Sub
- ' the generator returns a block of 16 bytes
- ' this is equal to a number between 0 and (2^128)-1
- ' so the x must be modded by 2^128 to get a 16-byte number
- Public Function Generate() As Byte()
- 'x_{n+1} = {x_n}^2 mod M
- x = x.modPow(New BigInteger(2), M)
- ' op is the output byte array
- ' it is set to x mod 2^128
- Dim top As SByte() = x.mod(blockmodulo).toByteArray()
- Dim op(15) As Byte
- For i As UInt16 = 0 To 15
- op(i) = 0
- Next
- For i As UInt16 = 0 To System.Math.Min(15, top.Length - 1)
- Dim temp As Int16 = top(i)
- temp += 128
- op(i) = temp
- Next
- ' shorten the byte array to 16, we dont need any more
- 'Array.Resize(op, 16)
- ' return op
- Return op
- End Function
- Private blockmodulo As BigInteger
- Private M As BigInteger
- Private x As BigInteger
- End Class
- Public Function Crypt(ByVal input As String, ByVal key As String) As String
- Dim stopWatch As New Stopwatch()
- stopWatch.Start()
- ' create a new random number generator
- Dim c As New BlumBlumShubChar(key)
- ' create a chararray to hold the string *yawn*
- Dim chararray(input.Length - 1) As Char
- ' copy string to chararray
- For i As UInt16 = 0 To input.Length - 1
- chararray(i) = input.Substring(i, 1)
- Next
- ' k is how far we are through the current block of random buytes
- Dim k As UInt16 = 0
- ' block holds the block of random bytes
- Dim block(16) As Byte
- ' generate and discard a block to confuse hackers
- c.Generate()
- ' generate a block and keep
- block = c.Generate()
- ' cycle through the input string
- For i As UInt16 = 0 To input.Length - 1
- ' if weve polished off the current block, get a new one and reset the block counter
- If k >= 16 Then
- c.Generate()
- block = c.Generate()
- k = 0
- End If
- ' exclusive-or the current byte in the block counter with the ascii code of the priginal string
- ' and store back into the string
- 'Console.WriteLine((block(k)))
- chararray(i) = Chr(block(k) Xor Asc(chararray(i)))
- ' increment block counter
- k += 1
- Next
- ' copy the char array back into a new string and return
- Dim output As String = ""
- For i As UInt16 = 0 To input.Length - 1
- output &= chararray(i)
- Next
- stopWatch.Stop()
- Console.WriteLine("This took " & stopWatch.ElapsedMilliseconds & " milliseconds ")
- Return output
- End Function
- End Class
- Sub Main()
- 'Dim lol As New StreamCrypto.BlumBlumShubChar("hammertime")
- 'While True
- ' Console.WriteLine(Chr(lol.Generate()))
- ' 'Console.ReadLine()
- 'End While
- Dim lol As New StreamCrypto
- While True
- Console.WriteLine("i can has key?")
- Dim key As String = Console.ReadLine()
- Console.WriteLine("i can haz plaintext?")
- Dim plaintext As String = Console.ReadLine()
- Console.WriteLine("i gives you ciphertext:")
- Dim ciphertext As String = lol.Crypt(plaintext, key)
- Console.WriteLine(ciphertext)
- Console.WriteLine("i gives you plaintext again:")
- Console.WriteLine(lol.Crypt(ciphertext, key))
- Console.ReadLine()
- End While
- End Sub
- End Module
Add Comment
Please, Sign In to add comment