namespace VAG_TACHO_LIVE
{
using ProgressODoom;
using SimpleTCP;
using
System.Collections
.Generic
; using
System.Runtime
.CompilerServices
; using
System.Runtime
.InteropServices
; using
System.Text
.RegularExpressions
;
public class Form1 : Form
{
public const uint FT_BAUD_300 = 300;
public const uint FT_BAUD_600 = 600;
public const uint FT_BAUD_1200 = 0x4b0;
public const uint FT_BAUD_2400 = 0x960;
public const uint FT_BAUD_4800 = 0x12c0;
public const uint FT_BAUD_9600 = 0x2580;
public const uint FT_BAUD_14400 = 0x3840;
public const uint FT_BAUD_19200 = 0x4b00;
public const uint FT_BAUD_38400 = 0x9600;
public const uint FT_BAUD_57600 = 0xe100;
public const uint FT_BAUD_115200 = 0x1c200;
public const uint FT_BAUD_230400 = 0x38400;
public const uint FT_BAUD_460800 = 0x70800;
public const uint FT_BAUD_921600 = 0xe1000;
public const uint FT_LIST_NUMBER_ONLY = 0x80000000;
public const uint FT_LIST_BY_INDEX = 0x40000000;
public const uint FT_LIST_ALL = 0x20000000;
public const uint FT_OPEN_BY_SERIAL_NUMBER = 1;
public const uint FT_OPEN_BY_DESCRIPTION = 2;
public const byte FT_BITS_8 = 8;
public const byte FT_BITS_7 = 7;
public const byte FT_BITS_6 = 6;
public const byte FT_BITS_5 = 5;
public const byte FT_STOP_BITS_1 = 0;
public const byte FT_STOP_BITS_1_5 = 1;
public const byte FT_STOP_BITS_2 = 2;
public const byte FT_PARITY_NONE = 0;
public const byte FT_PARITY_ODD = 1;
public const byte FT_PARITY_EVEN = 2;
public const byte FT_PARITY_MARK = 3;
public const byte FT_PARITY_SPACE = 4;
public const ushort FT_FLOW_NONE = 0;
public const ushort FT_FLOW_RTS_CTS = 0x100;
public const ushort FT_FLOW_DTR_DSR = 0x200;
public const ushort FT_FLOW_XON_XOFF = 0x400;
public const byte FT_PURGE_RX = 1;
public const byte FT_PURGE_TX = 2;
public const uint FT_EVENT_RXCHAR = 1;
public const uint FT_EVENT_MODEM_STATUS = 2;
private int cnt = 0;
private bool fContinue = false;
protected internal static uint m_hPort;
protected uint dwListDescFlags;
protected Thread pThreadRead;
private bool available_com_ports = false;
internal static bool is_connected = false;
private string receiveBuffer = "";
internal static byte can_ask_did = 0;
private string rCanId = "";
private string rDlc = "";
internal static byte[] rData;
internal static byte[] rData_FULL;
internal static byte[] rData_DUMP;
private int frame_len = 0;
private int frame_len_recieved = 0;
internal static bool pierwsza_ramka = false;
private byte[] working_frame = new byte[0];
internal static byte[] b_DS_Type;
internal static byte[] b_Data;
internal static bool multi_frame = false;
private byte rDID = 0;
private byte rService = 0;
private byte rSubfunction = 0;
internal static string rImmo = "";
public static byte[] eeprom = new byte[0];
internal static uint cont;
internal static uint
max; private byte[] sData;
internal static int ack_spd = 20;
internal static bool reading_mcu = false;
internal static bool ready = false;
internal static int read_data_cnt = 0x40;
internal static bool eeprom_access = false;
internal static string dump_type = "";
public static byte[] eeprom_mod = new byte[0];
public static string f_sw_tb = "";
public static string f_cn_tb = "";
public static string f_mil_tb = "";
public static string f_skey_tb = "";
internal static string seed_key;
internal static string calc_seed = "";
internal static byte[] SA2_REQUEST;
internal static bool skey_read = false;
private string keyhex;
private int empty_read = 0;
private int prev_cont = 0;
internal static bool read_lcd = false;
private int lcd_move = 0;
internal static bool immo_active = false;
internal static string skey_BKP = "";
private string return_from_cluster;
private bool rFD1103 = false;
internal static bool old_cluster = false;
internal static DateTime save_now;
private DateTime global_time;
internal static string global_path;
private int global_eeprom_read = 0;
internal static RichTextBox INT_LOG;
internal static ProgressBarEx INT_PB;
internal static TextBox INT_SWID;
internal static TextBox INT_SKEY;
internal static byte[] g_working_frame = new byte[0];
internal static SerialPort INT_SP;
internal static TextBox INT_PN;
internal static TextBox INT_SW;
internal static TextBox INT_MIL;
internal static ComboBox INT_CPL;
internal static DateTime LastBufferTime = DateTime.Now;
internal static bool data_received = false;
private readonly Functions Func = new Functions();
private readonly UDS_Functions UDS_Func = new UDS_Functions();
private bool FD04dbg = false;
private FT_STATUS ftStatus = FT_STATUS.FT_OK;
private static byte[] InputBuffer = new byte[500];
private static byte[] InputBuffer2 = new byte[500];
private bool VCDS_RTS;
private byte[] frame = new byte[20];
private uint NumBytesSent = 0;
private static byte Q_Iter = 0;
private static byte[] iter = new byte[4];
internal static string Last_send = "";
private byte[] xor_array = new byte[] { 0xf7, 0xc0, 0xc0, 0x88, 0x51, 0x51, 0x1a, 0xe2 };
public static SimpleTcpClient client;
private byte[] SeedVomTacho;
private byte[] SendSeedRequest;
private byte[] CT1;
private byte[] iv;
private string plaintext;
private Form2 frm2 = new Form2();
private Form3 frm3 = new Form3();
private Form4 frm4 = new Form4();
private IContainer components = null;
private TextBox mileage_tb_hex;
private TextBox mileage_tb;
private TextBox fazid;
private TextBox coding_tb;
public TextBox sw_tb;
private TextBox hw_tb;
private TextBox cluster_number_tb;
public ComboBox com_port_speed;
public ComboBox com_port_list;
private Button connect_btn;
private Button refresh_ports_btn;
private Button log_clear_btn;
private Button clear_all_btn;
private Button get_cluster_info_btn;
private Button reset_cluster_btn;
private Button read_mcu_btn;
private Button save_mcu_btn;
private Button read_eeprom_btn;
private Button save_eeprom_btn;
private Button read_lcd_btn;
private Button save_lcd_btn;
private TextBox error_rate_tb;
private Button exit_bl_btn;
private Button button1;
private Button try_skey_btn;
private Button edit_eeprom_btn;
private Button write_eeprom_btn;
private PlainBackgroundPainter plainBackgroundPainter1;
private PlainBorderPainter plainBorderPainter1;
private PlainProgressPainter plainProgressPainter1;
private Button dump_aes_btn;
private Button pro_btn;
private Button mil_fix_btn;
internal TextBox Skey_TB;
internal TextBox sw_id;
internal SerialPort serialPort;
private ProgressBarEx progressBar1;
private Button read_immo_btn;
private Button write_immo_btn;
private Button immo_form;
private Button SaveImmo_BTN;
private Button ADD_IP_BTN;
private Button exit_god;
private Button TEST_BTN;
public Form1()
{
this.InitializeComponent();
INT_PB = this.progressBar1;
INT_SP = this.serialPort;
INT_PN = this.cluster_number_tb;
INT_SW = this.sw_tb;
INT_SWID = this.sw_id;
INT_MIL = this.mileage_tb;
INT_CPL = this.com_port_list;
}
private void ADD_IP_BTN_Click(object sender, EventArgs e)
{
this.com_port_list.Items.Add("IP:10.0.0.1:10001");
this.GuiEnable();
}
public static byte[] addByteToArray(byte[] bArray, byte newByte)
{
byte
[] array = new byte
[bArray
.Length
+ 1]; array[bArray
.Length
] = newByte
; }
public static string ByteArrayToString(byte[] ba)
{
StringBuilder builder = new StringBuilder(ba.Length * 2);
foreach (byte num2 in ba)
{
builder.AppendFormat("{0:x2}", num2);
}
return builder.ToString();
}
private byte[] calculate_FD04(string s_odpowiedz_z_licznika, bool debug = false)
{
this.SeedVomTacho = StringToByteArray(s_odpowiedz_z_licznika);
this
.key = StringToByteArray
(this
.keyhex
); if (debug)
{
Console
.WriteLine
("SVT : " + BitConverter
.ToString
(this
.SeedVomTacho
).Replace
("-", string
.Empty)); Console.WriteLine("Try to skip AESKEY");
Console
.WriteLine
("AES KEY : " + BitConverter
.ToString
(this
.key).Replace
("-", string
.Empty)); this
.LOG.AppendText
("\nSVT : " + BitConverter
.ToString
(this
.SeedVomTacho
).Replace
("-", string
.Empty)); this
.LOG.AppendText
("\nTry to skip AESKEY"); this
.LOG.AppendText
("\nAES KEY : " + BitConverter
.ToString
(this
.key).Replace
("-", string
.Empty)); }
this.iv = this.SendSeedRequest.Skip<byte>(1).Take<byte>(0x10).ToArray<byte>();
this
.plaintext
= BitConverter
.ToString
(this
.SeedVomTacho
.Take
<byte
>(0x10).ToArray
<byte
>()).Replace
("-", string
.Empty); if (debug)
{
Console
.WriteLine
("IV : " + BitConverter
.ToString
(this
.iv
).Replace
("-", string
.Empty)); Console.WriteLine("Plain : " + this.plaintext);
this
.LOG.AppendText
("\nIV : " + BitConverter
.ToString
(this
.iv
).Replace
("-", string
.Empty)); this
.LOG.AppendText
("\nPlain : " + this
.plaintext
); }
AesGenericEncryptionService service
= new AesGenericEncryptionService
(this
.key, this
.iv
); this.CT1 = service.Encrypt(this.SeedVomTacho.Take<byte>(0x10).ToArray<byte>());
if (debug)
{
Console
.WriteLine
("AESS IV: " + BitConverter
.ToString
(service
.m_IV
).Replace
("-", string
.Empty)); Console
.WriteLine
("AESS KEY: " + BitConverter
.ToString
(service
.m_Key
).Replace
("-", string
.Empty)); Console
.WriteLine
("CT1 : " + BitConverter
.ToString
(this
.CT1
).Replace
("-", string
.Empty)); this
.LOG.AppendText
("\nAESS IV: " + BitConverter
.ToString
(service
.m_IV
).Replace
("-", string
.Empty)); this
.LOG.AppendText
("\nAESS KEY: " + BitConverter
.ToString
(service
.m_Key
).Replace
("-", string
.Empty)); this
.LOG.AppendText
("\nCT1 : " + BitConverter
.ToString
(this
.CT1
).Replace
("-", string
.Empty)); }
this.iv = this.CT1.Take<byte>(0x10).ToArray<byte>();
if (debug)
{
Console
.WriteLine
("IV2 : " + BitConverter
.ToString
(this
.iv
).Replace
("-", string
.Empty)); this
.LOG.AppendText
("\nIV2 : " + BitConverter
.ToString
(this
.iv
).Replace
("-", string
.Empty)); }
AesGenericEncryptionService service2
= new AesGenericEncryptionService
(this
.key, this
.iv
); if (debug)
{
Console
.WriteLine
("AESS2 IV: " + BitConverter
.ToString
(service2
.m_IV
).Replace
("-", string
.Empty)); Console
.WriteLine
("AESS2 KEY: " + BitConverter
.ToString
(service2
.m_Key
).Replace
("-", string
.Empty)); this
.LOG.AppendText
("\nAESS2 IV: " + BitConverter
.ToString
(service2
.m_IV
).Replace
("-", string
.Empty)); this
.LOG.AppendText
("\nAESS2 KEY: " + BitConverter
.ToString
(service2
.m_Key
).Replace
("-", string
.Empty)); }
byte[] buffer = new byte[] { 1 };
byte
[] data
= StringToByteArray
(BitConverter
.ToString
(this
.SeedVomTacho
.Skip
<byte
>(0x10).Take
<byte
>(7).ToArray
<byte
>()).Replace
("-", string
.Empty) + BitConverter
.ToString
(this
.SendSeedRequest
.Skip
<byte
>(0x11).Take
<byte
>(8).ToArray
<byte
>()).Replace
("-", string
.Empty) + BitConverter
.ToString
(buffer
)); byte[] buffer3 = service2.Encrypt(data);
if (debug)
{
Console.WriteLine("Dataparts : ");
Console
.WriteLine
(BitConverter
.ToString
(buffer
).Replace
("-", string
.Empty)); Console
.WriteLine
(BitConverter
.ToString
(data
).Replace
("-", string
.Empty)); Console
.WriteLine
(BitConverter
.ToString
(buffer3
).Replace
("-", string
.Empty)); this
.LOG.AppendText
("\nDataparts : "); this
.LOG.AppendText
("\n" + BitConverter
.ToString
(buffer
).Replace
("-", string
.Empty)); this
.LOG.AppendText
("\n" + BitConverter
.ToString
(data
).Replace
("-", string
.Empty)); this
.LOG.AppendText
("\n" + BitConverter
.ToString
(buffer3
).Replace
("-", string
.Empty)); }
string hex
= BitConverter
.ToString
(this
.CT1
.Take
<byte
>(0x10).ToArray
<byte
>()).Replace
("-", string
.Empty); if (debug)
{
Console.WriteLine("END P1 : " + hex);
this
.LOG.AppendText
("\nEND P1 : " + hex
); }
hex
= hex
+ BitConverter
.ToString
(buffer3
.Take
<byte
>(0x10).ToArray
<byte
>()).Replace
("-", string
.Empty); if (debug)
{
Console
.WriteLine
("END P2 : " + BitConverter
.ToString
(buffer3
.Take
<byte
>(0x10).ToArray
<byte
>()).Replace
("-", string
.Empty)); Console.WriteLine("END FIN : " + hex);
this
.LOG.AppendText
("\nEND P2 : " + BitConverter
.ToString
(buffer3
.Take
<byte
>(0x10).ToArray
<byte
>()).Replace
("-", string
.Empty)); this
.LOG.AppendText
("\nEND FIN : " + hex
); }
return StringToByteArray(hex);
}
public void can_ask_more()
{
pierwsza_ramka = false;
byte[] buffer1 = new byte[3];
buffer1[0] = 0x30;
buffer1[2] = 1;
this.sData = buffer1;
this.wyslij_ramke("714", 8, this.sData, false);
}
private void clear_all_btn_Click(object sender, EventArgs e)
{
this.cluster_number_tb.Text = "";
this.coding_tb.Text = "";
this.hw_tb.Text = "";
this.mileage_tb.Text = "";
this.mileage_tb_hex.Text = "";
this.sw_tb.Text = "";
this.Skey_TB.Text = "";
this.fazid.Text = "";
this.sw_id.Text = "";
this.log_clear_btn_Click(sender, e);
}
private void Client_DataReceived(object sender, SimpleTCP.Message e)
{
string str = e.MessageString.Remove(e.MessageString.Length - 1, 1).Replace(" ", "");
string[] strArray = null;
this.receiveBuffer = str;
char[] separator = new char[] { '\x0013' };
strArray
= this
.receiveBuffer
.Split(separator
); int length = strArray.Length;
for (int i = 0; i < length; i++)
{
if (strArray[i] == "")
{
i++;
}
this.process_can_data(strArray[i]);
Console.WriteLine(strArray[i]);
}
}
public void CloseCANC