Don't like ads? PRO users don't see any ads ;-)
Guest

Untitled

By: a guest on Jun 30th, 2012  |  syntax: None  |  size: 5.07 KB  |  hits: 15  |  expires: Never
download  |  raw  |  embed  |  report abuse  |  print
Text below is selected. Please press Ctrl+C to copy to your clipboard. (⌘+C on Mac)
  1. How to make Rijndael CBC mode work in vb.net
  2. Sub Main()
  3.  
  4.         Dim rij As New RijndaelManaged
  5.         Dim iv(15) As Byte
  6.         Dim key(15) As Byte
  7.         Dim secret() As Byte = {59, 60, 61}
  8.  
  9.         Dim cs As ICryptoTransform
  10.         Dim cstream As CryptoStream
  11.  
  12.         Dim out() As Byte
  13.         Dim NewRandom As New RNGCryptoServiceProvider()
  14.  
  15.         NewRandom.GetBytes(iv)
  16.         NewRandom.GetBytes(key)
  17.  
  18.         rij = New RijndaelManaged()
  19.  
  20.         rij.KeySize = 128
  21.         rij.Padding = PaddingMode.PKCS7
  22.  
  23.         rij.Mode = CipherMode.CBC
  24.  
  25.         rij.IV = iv
  26.         rij.Key = key
  27.         cs = rij.CreateEncryptor()
  28.  
  29.         Dim ms_in As New MemoryStream
  30.         cstream = New CryptoStream(ms_in, cs, CryptoStreamMode.Write)
  31.  
  32.  
  33.         Using cstream
  34.             cstream.Write(secret, 0, 3)
  35.         End Using
  36.  
  37.         out = ms_in.ToArray
  38.         Console.WriteLine(ArrayToString(out, out.Length))
  39.         Erase out
  40.  
  41.         ms_in = New MemoryStream
  42.         cstream = New CryptoStream(ms_in, cs, CryptoStreamMode.Write)
  43.  
  44.  
  45.         Using cstream
  46.             cstream.Write(secret, 0, 3)
  47.         End Using
  48.  
  49.         out = ms_in.ToArray
  50.         Console.WriteLine(ArrayToString(out, out.Length))
  51.  
  52.     End Sub
  53.        
  54. Public Function ArrayToString(ByVal bytes() As Byte, ByVal length As Integer) As String
  55.  
  56.         If bytes.Length = 0 Then Return String.Empty
  57.         Dim sb As New System.Text.StringBuilder(length)
  58.  
  59.         Dim k As Integer = length - 1
  60.         Dim i As Integer
  61.  
  62.         For i = 0 To k
  63.             sb.Append(Chr(bytes(i)))
  64.         Next
  65.  
  66.  
  67.         Return sb.ToString()
  68.  
  69.     End Function
  70.        
  71. cs = rij.CreateEncryptor()
  72.     Dim ms_in As New MemoryStream
  73.     cstream = New CryptoStream(ms_in, cs, CryptoStreamMode.Write)
  74.  
  75.     Using cstream
  76.         cstream.Write(secret, 0, 3) 'encrypt
  77.     End Using
  78.  
  79.     out = ms_in.ToArray
  80.     Console.WriteLine(ArrayToString(out, out.Length)) 'see the encrypted message
  81.     Erase out
  82.  
  83.     Using cstream
  84.         cstream.Write(secret, 0, 3) 'encrypt, this will crash here and this is the problem I'm trying to solve
  85.     End Using
  86.  
  87.     out = ms_in.ToArray
  88.     Console.WriteLine(ArrayToString(out, out.Length)) 'see the encrypted message this should not be the same as the first one
  89.        
  90. Public Sub Run()
  91.     Dim key() As Byte = {0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15}
  92.  
  93.     Dim plaintext1 As Byte() = {59, 60, 61}
  94.  
  95.     Dim plaintext2 As Byte() = {0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15, _
  96.                              0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15, _
  97.                              0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15, _
  98.                              0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15 _
  99.                              }
  100.  
  101.     Roundtrip(plaintext1, key)
  102.     System.Console.WriteLine()
  103.     Roundtrip(plaintext2, key)
  104.  
  105. End Sub
  106.  
  107.  
  108. Public Sub Roundtrip(ByRef plaintext As Byte(), ByRef key As Byte())
  109.  
  110.     Dim rij As New RijndaelManaged
  111.     Dim iv(15) As Byte
  112.  
  113.     Dim encryptor As ICryptoTransform
  114.     Dim decryptor As ICryptoTransform
  115.  
  116.     Dim out() As Byte
  117.  
  118.     'Dim NewRandom As New RNGCryptoServiceProvider()
  119.     'NewRandom.GetBytes(iv)
  120.     'NewRandom.GetBytes(key)
  121.  
  122.     Console.WriteLine("Original:")
  123.     Console.WriteLine(ArrayToString(plaintext))
  124.     System.Console.WriteLine()
  125.  
  126.     rij = New RijndaelManaged()
  127.     rij.KeySize = key.Length * 8  ' 16 byte key == 128 bits
  128.     rij.Padding = PaddingMode.PKCS7
  129.     rij.Mode = CipherMode.CBC
  130.     rij.IV = iv
  131.     rij.Key = key
  132.     encryptor = rij.CreateEncryptor()
  133.  
  134.     Using msIn = New MemoryStream
  135.  
  136.         Using cstream = New CryptoStream(msIn, encryptor, CryptoStreamMode.Write)
  137.             cstream.Write(plaintext, 0, plaintext.Length)
  138.         End Using
  139.  
  140.         out = msIn.ToArray
  141.         Console.WriteLine("Encrypted:")
  142.         Console.WriteLine("{0}", ArrayToString(out))
  143.         System.Console.WriteLine()
  144.  
  145.     End Using
  146.  
  147.     decryptor = rij.CreateDecryptor()
  148.     Using msIn = New MemoryStream
  149.  
  150.         Using cstream = New CryptoStream(msIn, decryptor, CryptoStreamMode.Write)
  151.             cstream.Write(out, 0, out.Length)
  152.         End Using
  153.  
  154.         out = msIn.ToArray
  155.         Console.WriteLine("Decrypted:  ")
  156.         Console.WriteLine("{0}", ArrayToString(out))
  157.         System.Console.WriteLine()
  158.  
  159.     End Using
  160.  
  161. End Sub
  162.  
  163.  
  164. Public Shared Function ArrayToString(ByVal bytes As Byte()) As String
  165.  
  166.     Dim sb As New System.Text.StringBuilder()
  167.  
  168.     Dim i As Integer
  169.     For i = 0 To bytes.Length-1
  170.         if (i <> 0 AND i mod 16 = 0) Then
  171.             sb.Append(Environment.NewLine)
  172.         End If
  173.         sb.Append(System.String.Format("{0:X2} ", bytes(i)))
  174.     Next
  175.  
  176.     Return sb.ToString().Trim()
  177.  
  178. End Function
  179.        
  180. For i As Integer = 1 To 2
  181.             Using ms = New MemoryStream
  182.                 Using cstream = New CryptoStream(ms, encryptor, CryptoStreamMode.Write)
  183.                     For j As Integer = 1 To i
  184.                         cstream.Write(plaintext, 0, plaintext.Length)
  185.                     Next j
  186.                 End Using
  187.                 out = ms.ToArray
  188.                 Console.WriteLine("Encrypted (cycle {0}):", i)
  189.                 Console.WriteLine("{0}", ArrayToString(out))
  190.                 System.Console.WriteLine()
  191.  
  192.             End Using
  193.         Next i