Hello,
Just for the sake of it, I tried to convert the Python code (few messages above) into VB .net.
I failed miserably and I don't understand what I am doing wrong.
My code produces diffferent (wrong) keys. I am 100% sure that my Byte arrays for the Key and Message are correct - I checked them against the Python script.
It is the .net Cryptography library that seems to produce a different RC2 result than Crypto.Cipher ARC2 does on Python. But that cannot be true, I would say!
So please help me!
Here is my code (it is not pretty, but at this moment I just want it to produce the same keys as the Python script):
Imports System.Security.Cryptography
Imports System.Text
Imports System.IO
Public Class Form1
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
Dim key() As Byte = str2byte("Revision ")
key(8) = 0
Dim opt() As Integer = {9, 14, 16, 17, 20, 21, 22, 23, 24, 26, 27, 28, 29, 42, 43, 44, 45, 46, 47, 48,
53, 54, 56, 57, 58, 59, 60, 61, 62, 63, 64, 65, 66, 67, 68, 69, 83, 84, 85, 86,
87, 88, 90, 92, 96, 839, 849, 859, 869}
Dim serial1() As Byte = BitConverter.GetBytes(123456) ' <--- Serial Number (first part)
Dim serial2() As Byte = BitConverter.GetBytes(2) ' <--- Serial Number (second part)
Dim message(7) As Byte
message(0) = serial1(0)
message(1) = serial1(1)
message(2) = serial1(2)
message(3) = serial1(3)
message(4) = serial2(0)
message(5) = serial2(1)
ListBox1.Items.Clear()
For Each o In opt
Dim ob() As Byte = BitConverter.GetBytes(o << 20)
message(6) = ob(2)
message(7) = ob(3)
Dim mStream As New MemoryStream
Dim RC2alg As RC2 = RC2.Create
RC2alg.Mode = CipherMode.ECB
RC2alg.Padding = PaddingMode.None
Dim cStream As New CryptoStream(mStream, RC2alg.CreateEncryptor(key, RC2alg.IV), CryptoStreamMode.Write)
cStream.Write(message, 0, message.Length)
cStream.FlushFinalBlock()
Dim ret As Byte() = mStream.ToArray()
cStream.Close()
mStream.Close()
ListBox1.Items.Add(Str(o) + " - " + byte2str(ret))
Next
End Sub
Function str2byte(str As String) As Byte()
Dim b(Len(str) - 1) As Byte
For n = 1 To Len(str)
b(n - 1) = Asc(Mid(str, n, 1))
Next
Return b
End Function
Function byte2str(b() As Byte) As String
Dim t As String = ""
byte2str = ""
For n = 0 To b.Count - 1
t = Hex(b(n))
If Len(t) = 1 Then t = "0" + t
byte2str = byte2str + t
Next
End Function
End Class
To run it, just create a Form, Button1 and Listbox1.
Regards,
Vitor