public byte[] GetBytes() { byte[] buffer = new byte[1 + Data.Length + 2]; buffer[0] = (byte)Command; ByteWriter.WriteBytes(buffer, 1, Data); ushort crc16 = Crc16Ccitt.ComputeChecksum(buffer, 1 + Data.Length); LittleEndianWriter.WriteUInt16(buffer, 1 + Data.Length, crc16); return(buffer); }
public void ComputeChecksumTest3() { Crc16Ccitt target = new Crc16Ccitt(InitialCrcValue.Zeros); byte[] bytes = new byte[] { 0x00, 0x00, 0x00, 0x42 }; ushort expected = 0x6886; ushort actual; actual = target.ComputeChecksum(bytes); Assert.AreEqual(expected, actual); }
public void ComputeChecksumTest6() { Crc16Ccitt target = new Crc16Ccitt(InitialCrcValue.Zeros); byte[] bytes = new byte[] { 0xCA, 0xFE, 0xBA, 0xBE, 0x94, 0x8F }; ushort expected = 0; ushort actual; actual = target.ComputeChecksum(bytes); Assert.AreEqual(expected, actual); }
public void ComputeChecksumTest7() { Crc16Ccitt target = new Crc16Ccitt(InitialCrcValue.Zeros); byte[] bytes = new byte[10000]; for (int i = 0; i < 10000; i++) { bytes[i] = (byte)i; } ushort expected = 0x5885; ushort actual; actual = target.ComputeChecksum(bytes); Assert.AreEqual(expected, actual); }
public static ArduinoI2CResponse VerifyIncomingData(byte[] bData) { try { Int16 sizeData = bData[0]; int size = sizeData + 4; if (bData[1] != 2) { throw new Exception("To nie jest typ Response"); } byte lo = bData[size - 2]; byte hi = bData[size - 1]; byte[] crcData = new byte[size - 2]; Buffer.BlockCopy(bData, 0, crcData, 0, size - 2); Crc16Ccitt crc = new Crc16Ccitt(InitialCrcValue.NonZero1); Debug.WriteLine("CRC: " + crc.ComputeChecksum(crcData)); var retByte = crc.ComputeChecksumBytes(crcData); if (!((retByte[0] == lo) && retByte[1] == hi)) { throw new Exception("B³ad przesy³u danych! B³ad CRC"); } byte[] Data = new byte[size - 4]; Buffer.BlockCopy(bData, 2, Data, 0, sizeData); return((ArduinoI2CResponse)GetObjectFromBytes(Data, typeof(ArduinoI2CResponse))); } catch (Exception ex) { string val = String.Join(",", bData); LoggerFactory.LogException(ex, "VerifyIncomingData", new { val }); return(new ArduinoI2CResponse { Status = (int)I2CResponseStatus.Error }); } }
private void encryptBinFile_Click(object sender, EventArgs e) { var fileArray = File.ReadAllBytes(fileNameTextBox.Text); Crc16Ccitt c = new Crc16Ccitt(InitialCrcValue.NonZero1); ushort crcValue = c.ComputeChecksum(fileArray); outputTextBox.AppendText(String.Format("Calculated CRC: 0x{0:X}\r\n", crcValue)); outputTextBox.AppendText(String.Format("Calculated CRC: 0d{0}\r\n", crcValue)); int totalNumberOfPackets = (fileArray.Length / 32); if (fileArray.Length % 32 != 0) { totalNumberOfPackets++; } byte[] encryptedArray = new byte[fileArray.Length]; byte[] aplitEncryptedArray = new byte[fileArray.Length]; byte[] key = { 0x30, 0xAB, 0xC2, 0x12, 0x09, 0xCE, 0x56, 0x9A, 0xF8, 0x81, 0xD4, 0xA9 , 0x05, 0x67, 0x5E, 0x6F, 0x79, 0x3F, 0x96, 0xC9, 0x11, 0x2F, 0x37, 0x1C, 0x60, 0xE3, 0x04, 0x7E , 0xB6, 0x93, 0x70, 0xEF, 0x4A, 0x45, 0x02, 0x6A, 0x97, 0x12, 0x27, 0x42, 0xEC, 0x51, 0x55, 0x70 , 0x00, 0xCB, 0xA8, 0x61, 0x22, 0xE1, 0xF1, 0x78, 0x53, 0x18, 0xF8, 0xDA, 0xC9, 0x40, 0x08, 0x5D , 0x3F, 0x96, 0x09, 0xC5, 0x9B, 0x08, 0x34, 0xBD, 0x9C, 0xFA, 0x9F, 0x1D, 0x5F, 0x61, 0x77, 0xA0 , 0x16, 0xC5, 0xB1, 0x76, 0x00, 0xF7, 0x97, 0x9A, 0x04, 0x09, 0x95, 0x8C, 0x44, 0x5A, 0xF3, 0x1E , 0xCE, 0x96, 0x24, 0xF7, 0xE1, 0x2B, 0x20, 0x5A, 0x67, 0x89, 0xEa, 0x6C, 0xBA, 0x73, 0x0B, 0x19 , 0xE6, 0xA3, 0xCD, 0xFC, 0xA1, 0xAD, 0x7E, 0x65, 0x67, 0x0C, 0x9B, 0x48, 0x5E, 0x6A, 0x53, 0xBD , 0xBB, 0x22, 0xBC, 0x85, 0x17, 0x85, 0x2A, 0x07, 0x9A, 0xC0, 0x18, 0xB6, 0xEF, 0x90, 0x3C, 0x0E , 0x8E, 0x19, 0x5E, 0xA1, 0xE0, 0x06, 0x18, 0xD0, 0x56, 0x36, 0x30, 0xA0, 0xA8, 0x60, 0x77, 0x31 , 0x4C, 0xDA, 0x3B, 0x5E, 0x75, 0x9C, 0xD5, 0xB6, 0x97, 0x59, 0x56, 0x47, 0xF8, 0xEB, 0x40, 0xF9 , 0x99, 0x2F, 0xFB, 0xFA, 0xFE, 0x88, 0xF3, 0x49, 0xD2, 0xC7, 0xD6, 0x54, 0x6A, 0xBB, 0x02, 0x52 , 0xCB, 0xC1, 0xCA, 0xA1, 0x23, 0x6D, 0x7F, 0xC9, 0x4F, 0x6B, 0xDE, 0x2E, 0xF3, 0xB5, 0x4A, 0x30 , 0x46, 0xE5, 0x08, 0xB9, 0xCD, 0x1F, 0x07, 0x12, 0x89, 0x04, 0xD3, 0x03, 0x72, 0x96, 0x50, 0x0D , 0xF3, 0xB9, 0xB1, 0xD6, 0xAE, 0xF9, 0xB4, 0x3A, 0x95, 0x78, 0xCE, 0x7D, 0xA4, 0xA0, 0x55, 0x4C , 0xDF, 0xC2, 0x5C, 0x5A, 0x79, 0x91, 0xD8, 0x4D, 0xC8, 0x62, 0x8E, 0x8D, 0xA0, 0xED, 0xF0, 0xE6 , 0xED, 0x59, 0x63, 0xE2 }; uint Counter = 0; int Rotator = fileArray.Length % 8; if (Rotator.Equals(0)) { Rotator = (fileArray.Length + 1) % 5; } if (Rotator.Equals(0)) { Rotator = 4; } outputTextBox.AppendText(String.Format("File size in bytes: {0}\r\n", fileArray.Length)); outputTextBox.AppendText(String.Format("Calculated Rotator: {0}\r\n", Rotator)); for (int i = 0; i < fileArray.Length; i++) { byte firstLeftRotation = lrotate(fileArray[i], Rotator); byte afterKey = (byte)(firstLeftRotation ^ key[Counter]); byte secondLeftRotate = lrotate(afterKey, 8 - Rotator); byte firstLeftRotationAplit = lrotate(fileArray[i], 3); byte afterKeyAplit = (byte)(firstLeftRotationAplit ^ key[Counter]); byte secondLeftRotateAplit = lrotate(afterKeyAplit, 5); Counter++; encryptedArray[i] = secondLeftRotate; aplitEncryptedArray[i] = secondLeftRotateAplit; if (Counter > 255) { Counter = 0; } } File.WriteAllBytes(fileNameTextBox.Text.Replace(shortFileName, "Encrypted" + shortFileName), encryptedArray); File.WriteAllBytes(fileNameTextBox.Text.Replace(shortFileName, "AplitEncrypted" + shortFileName), aplitEncryptedArray); }
private void Form1_Load(object sender, System.EventArgs e) { //Test if version of DLL is compatible : 3rd argument is "version of DLL tested;Your application name;Your application version" String ToDisplay; MediaInfo MI = new MediaInfo(); ToDisplay = MI.Option("Info_Version", "0.7.0.0;MediaInfoDLL_Example_CS;0.7.0.0"); if (ToDisplay.Length == 0) { richTextBox1.Text = "MediaInfo.Dll: this version of the DLL is not compatible"; return; } //Information about MediaInfo ToDisplay += "\r\n\r\nInfo_Parameters\r\n"; ToDisplay += MI.Option("Info_Parameters"); ToDisplay += "\r\n\r\nInfo_Capacities\r\n"; ToDisplay += MI.Option("Info_Capacities"); ToDisplay += "\r\n\r\nInfo_Codecs\r\n"; ToDisplay += MI.Option("Info_Codecs"); //An example of how to use the library ToDisplay += "\r\n\r\nOpen\r\n"; MI.Open("Example.ogg"); ToDisplay += "\r\n\r\nInform with Complete=false\r\n"; MI.Option("Complete"); ToDisplay += MI.Inform(); ToDisplay += "\r\n\r\nInform with Complete=true\r\n"; MI.Option("Complete", "1"); ToDisplay += MI.Inform(); ToDisplay += "\r\n\r\nCustom Inform\r\n"; MI.Option("Inform", "General;File size is %FileSize% bytes"); ToDisplay += MI.Inform(); ToDisplay += "\r\n\r\nGet with Stream=General and Parameter='FileSize'\r\n"; ToDisplay += MI.Get(0, 0, "FileSize"); ToDisplay += "\r\n\r\nGet with Stream=General and Parameter=46\r\n"; ToDisplay += MI.Get(0, 0, 46); ToDisplay += "\r\n\r\nCount_Get with StreamKind=Stream_Audio\r\n"; ToDisplay += MI.Count_Get(StreamKind.Audio); ToDisplay += "\r\n\r\nGet with Stream=General and Parameter='AudioCount'\r\n"; ToDisplay += MI.Get(StreamKind.General, 0, "AudioCount"); ToDisplay += "\r\n\r\nGet with Stream=Audio and Parameter='StreamCount'\r\n"; ToDisplay += MI.Get(StreamKind.Audio, 0, "StreamCount"); ToDisplay += "\r\n\r\nClose\r\n"; MI.Close(); //Example with a stream //ToDisplay+="\r\n"+ExampleWithStream()+"\r\n"; Crc16Ccitt A=new Crc16Ccitt(InitialCrcValue.Zeros); byte[] B = { 0x84, 0x00, 0x00, 0x00, 0x6B, 0x00, 0x00, 0x00, 0x67, 0x1F, 0x20, 0x00, 0x00, 0x62, 0x0C, 0x9B, 0x35, 0x35, 0x38, 0x3B, 0x34, 0x31, 0x36, 0x20, 0x56, 0x9B, 0x38, 0x31, 0x3B, 0x33, 0x32, 0x20, 0x5F, 0x90, 0x6F, 0x90, 0x20, 0x41, 0x90, 0x7E, 0x9B, 0x31, 0x3B, 0x30, 0x30, 0x30, 0x30, 0x20, 0x63, 0x9B, 0x30, 0x20, 0x58, 0x1C, 0x40, 0x40, 0x1C, 0x46, 0x40, 0x90, 0x20, 0x40, 0x90, 0x50, 0xC9, 0xB3, 0xC7, 0x89, 0x20, 0x1D, 0x61, 0x1B, 0x7E, 0xC0, 0xA4, 0xE9, 0xF3, 0xC1, 0xF3, 0x8A, 0x24, 0x72, 0x21, 0x29, 0x88, 0x1C, 0x4F, 0x41, 0x89, 0xE1, 0xAD, 0xB7, 0xB3, 0x8A, 0x21, 0x29, 0x89, 0x20, 0x8A, 0x21, 0x44, 0x89, 0xA4, 0xF3, 0xBF, 0xF9, 0xCD, 0xC3, 0xC8, 0x8A, 0x21, 0x29 }; ushort C=A.ComputeChecksum(B); //Displaying the text richTextBox1.Text = ToDisplay; }