Advertisement
ZeekoSec

Making an encryption algorithm

Apr 8th, 2015
516
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
VB.NET 4.66 KB | None | 0 0
  1. ' Start with reversing the string
  2.  
  3. Public Function ReverseString(ByRef strToReverse As String) As String
  4.         Dim result As String = ""
  5.         For i As Integer = 0 To strToReverse.Length - 1
  6.             result += strToReverse(strToReverse.Length - 1 - i)
  7.         Next
  8.         Return result
  9.     End Function
  10.    
  11.     'then get byte value
  12.    
  13.     ' The input String.
  14.     Dim value As String = Textbox1.Text
  15.  
  16.     ' Convert String to Byte array.
  17.     Dim array() As Byte = System.Text.Encoding.ASCII.GetBytes(value)
  18.  
  19.     ' Display Bytes.
  20.     For Each b As Byte In array
  21.         Textbox1.Text = ("{0} = {1}", b, ChrW(b))
  22.    
  23.     'Byte2String
  24.    
  25.     ' Input Bytes.
  26.     Dim array() As Byte = {86, 66, 46, 78, 69, 84}
  27.  
  28.     ' Use GetString.
  29.     Dim value As String = System.Text.ASCIIEncoding.ASCII.GetString(array)
  30.     Console.WriteLine(value)
  31.    
  32.     'Another string 2 byte converter
  33.    
  34.     Private Shared Function GetBytes(str As String) As Byte()
  35.     Dim bytes As Byte() = New Byte(str.Length * 2 - 1) {}
  36.     System.Buffer.BlockCopy(str.ToCharArray(), 0, bytes, 0, bytes.Length)
  37.     Return bytes
  38. End Function
  39.  
  40. Private Shared Function GetString(bytes As Byte()) As String
  41.     Dim chars As Char() = New Char(bytes.Length / 2 - 1) {}
  42.     System.Buffer.BlockCopy(bytes, 0, chars, 0, bytes.Length)
  43.     Return New String(chars)
  44. End Function
  45.          
  46.          
  47. ' Byte Encoder
  48.  
  49. 'encryption key
  50. Public Shared Key As Byte() = New Byte() {&H43, &H72, &H6e, &H6d, &H54, &H4d, _
  51.     &H65, &H94, &H16, &H32, &H44, &H84, _
  52.     &H7e, &H18, &H64, &H76, &H6e, &H63, _
  53.     &H64, &H7a, &H5f, &H84, &H7f, &H9a}
  54.  
  55. 'Decrypt byte[]
  56. Public Shared Function Decrypt(data As Byte()) As Byte()
  57.     Dim ms As New MemoryStream()
  58.     Dim alg As Rijndael = Rijndael.Create()
  59.     alg.Key = Key
  60.     Dim cs As New CryptoStream(ms, alg.CreateDecryptor(), CryptoStreamMode.Write)
  61.     cs.Write(data, 0, data.Length)
  62.     cs.Close()
  63.     Dim decryptedData As Byte() = ms.ToArray()
  64.     Return decryptedData
  65. End Function
  66.  
  67. 'Encrypt byte[]
  68. Public Shared Function Encrypt(data As Byte()) As Byte()
  69.     Dim ms As New MemoryStream()
  70.     Dim alg As Rijndael = Rijndael.Create()
  71.     alg.Key = Key
  72.     Dim cs As New CryptoStream(ms, alg.CreateEncryptor(), CryptoStreamMode.Write)
  73.     cs.Write(data, 0, data.Length)
  74.     cs.Close()
  75.     Dim encryptedData As Byte() = ms.ToArray()
  76.     Return encryptedData
  77. End Function
  78.  
  79. 'serialize object to memory stream
  80. Public Shared Function SerializeToStream(o As Object) As MemoryStream
  81.     Dim stream As New MemoryStream()
  82.     Dim formatter As IFormatter = New BinaryFormatter()
  83.     formatter.Serialize(stream, o)
  84.     Return stream
  85. End Function
  86.  
  87. 'deserialize object from memory stream
  88. Public Shared Function DerializeFromStream(Of T As New)(memoryStream As MemoryStream) As T
  89.     If memoryStream Is Nothing Then
  90.         Return New T()
  91.     End If
  92.     Dim o As T
  93.     Dim binaryFormatter As New BinaryFormatter()
  94.     Using memoryStream
  95.         memoryStream.Seek(0, SeekOrigin.Begin)
  96.         o = DirectCast(binaryFormatter.Deserialize(memoryStream), T)
  97.     End Using
  98.     Return o
  99. End Function
  100.  
  101. 'Another byte modifier
  102.  
  103. Private Shared Sub EncryptThenDecrypt()
  104.     Dim message As Byte()
  105.     ' fill with your bytes
  106.     Dim encMessage As Byte()
  107.     ' the encrypted bytes
  108.     Dim decMessage As Byte()
  109.     ' the decrypted bytes - s/b same as message
  110.     Dim key As Byte()
  111.     Dim iv As Byte()
  112.  
  113.     Using rijndael = New RijndaelManaged()
  114.         rijndael.GenerateKey()
  115.         rijndael.GenerateIV()
  116.         key = rijndael.Key
  117.         iv = rijndael.IV
  118.         encMessage = EncryptBytes(rijndael, message)
  119.     End Using
  120.  
  121.     Using rijndael = New RijndaelManaged()
  122.         rijndael.Key = key
  123.         rijndael.IV = iv
  124.         decMessage = DecryptBytes(rijndael, encMessage)
  125.     End Using
  126. End Sub
  127.  
  128. Private Shared Function EncryptBytes(alg As SymmetricAlgorithm, message As Byte()) As Byte()
  129.     If (message Is Nothing) OrElse (message.Length = 0) Then
  130.         Return message
  131.     End If
  132.  
  133.     If alg Is Nothing Then
  134.         Throw New ArgumentNullException("alg")
  135.     End If
  136.  
  137.     Using stream = New MemoryStream()
  138.         Using encryptor = alg.CreateEncryptor()
  139.             Using encrypt = New CryptoStream(stream, encryptor, CryptoStreamMode.Write)
  140.                 encrypt.Write(message, 0, message.Length)
  141.                 encrypt.FlushFinalBlock()
  142.                 Return stream.ToArray()
  143.             End Using
  144.         End Using
  145.     End Using
  146. End Function
  147.  
  148. Private Shared Function DecryptBytes(alg As SymmetricAlgorithm, message As Byte()) As Byte()
  149.     If (message Is Nothing) OrElse (message.Length = 0) Then
  150.         Return message
  151.     End If
  152.  
  153.     If alg Is Nothing Then
  154.         Throw New ArgumentNullException("alg")
  155.     End If
  156.  
  157.     Using stream = New MemoryStream()
  158.         Using decryptor = alg.CreateDecryptor()
  159.             Using encrypt = New CryptoStream(stream, decryptor, CryptoStreamMode.Write)
  160.                 encrypt.Write(message, 0, message.Length)
  161.                 encrypt.FlushFinalBlock()
  162.                 Return stream.ToArray()
  163.             End Using
  164.         End Using
  165.     End Using
  166. End Function
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement