I've tried hours to figure out how to manipulate a byte array to do the "unpacking" and packing with C#.
Packed Data Format
Data is packed in 8 byte “packets”, with the MS bit stripped from 7
parameter bytes, and packed into an eighth byte, which is sent at the start
of the 8 byte packet.
Example:
Input Data
1 A7 A6 A5 A4 A3 A2 A1 A0
2 B7 B6 B5 B4 B3 B2 B1 B0
3 C7 C6 C5 C4 C3 C2 C1 C0
4 D7 D6 D5 D4 D3 D2 D1 D0
5 E7 E6 E5 E4 E3 E2 E1 E0
6 F7 F6 F5 F4 F3 F2 F1 F0
7 G7 G6 G5 G4 G3 G2 G1 G0
Packed MIDI data
1 00 G7 F7 E7 D7 C7 B7 A7
2 00 A6 A5 A4 A3 A2 A1 A0
3 00 B6 B5 B4 B3 B2 B1 B0
4 00 C6 C5 C4 C3 C2 C1 C0
5 00 D6 D5 D4 D3 D2 D1 D0
6 00 E6 E5 E4 E3 E2 E1 E0
7 00 F6 F5 F4 F3 F2 F1 F0
8 00 G6 G5 G4 G3 G2 G1 G0
Each row indicates one byte, each column one bit (MSB first).
How do I implement this conversion?
If I'm reading the spec right, this should do the trick:
byte[] input = new byte[7] { 0x81, 0x82, 0x03, 0x84, 0x05, 0x06, 0x87 };
byte[] output = new byte[8];
for (int i = 0; i < 7; i++)
{
output[0] |= (byte)((input[i] >> 7) << i);
output[i + 1] = (byte)(input[i] & 0x7F);
}
// output == { 0x4b, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07 }
// output[0] == 0b01001011
As you can see, the most significant bit of each input byte in the example is cleared, and a prefix byte is added with the most significant bit of each input byte.
Related
I generate key pair like this.
ECKeyPairGenerator gen = new ECKeyPairGenerator("ECDSA");
SecureRandom secureRandom = new SecureRandom();
Org.BouncyCastle.Asn1.X9.X9ECParameters ecp = Org.BouncyCastle.Asn1.Nist.NistNamedCurves.GetByName("P-256");
ECDomainParameters ecSpec = new ECDomainParameters(ecp.Curve, ecp.G, ecp.N, ecp.H, ecp.GetSeed());
ECKeyGenerationParameters ecgp = new ECKeyGenerationParameters(ecSpec, secureRandom);
gen.Init(ecgp);
AsymmetricCipherKeyPair eckp = gen.GenerateKeyPair();
and I want to convert AsymmetricCipherKeyPair to byte array.
so I add code.
ECPublicKeyParameters ecPub = (ECPublicKeyParameters)eckp.Public;
ECPrivateKeyParameters ecPri = (ECPrivateKeyParameters)eckp.Private;
But I know there were two ways for convert AsymmetricCipherKeyPair to byte array.
first,
byte[] pubs = ecPub.Q.GetEncoded();
Second,
byte[] pubX = ecPub.Q.XCoord.ToBigInteger().ToByteArray();
byte[] pubY = ecPub.Q.YCoord.ToBigInteger().ToByteArray();
The results of both methods are slightly different.
first way, pubs[0] is always 0x04, and it make array length to 65 bytes.
like this
04 F0 9E 70 EB ED 52 4B 56 E8 64 9C 9A D9 1C 97 6F F1 92 86 BA 87 FC F5 AB E4 CC 72 C6 EA 77 FA 0D 30 4C 39 0F 38 BE E3 C7 3E 8B 4D 2F 05 C3 55 3F 78 DB 8E DD 77 DF 24 D4 3B 56 88 33 D7 CB 0B 9E
seconde way, pubX[0] is sometimes 0x00, and it make array length to 65 bytes.
like this
pubX = 00 F0 9E 70 EB ED 52 4B 56 E8 64 9C 9A D9 1C 97 6F F1 92 86 BA 87 FC F5 AB E4 CC 72 C6 EA 77 FA 0D
pubY = 30 4C 39 0F 38 BE E3 C7 3E 8B 4D 2F 05 C3 55 3F 78 DB 8E DD 77 DF 24 D4 3B 56 88 33 D7 CB 0B 9E
Except for the value of index 0, the rest are the same.
Why does this difference occur?
What value do I actually use?
p.s.
I convert private key like this
byte[] pri = ecPri.D.ToByteArray();
Is this the right way?
And private key also has 0x00 on index 0. Why?
I am trying to write one program for serial port communication, where i need to send data packet with CRC, I am writing this code in C# language.
Below is sample packet which receiver is expecting with CRC.
10 02 B1 F0 3F 32 08 00 00 10 03 B4 5C
Second Data Packet : 10 02 B1 F0 3F 32 07 00 00 10 03 4D EE
10 - DLE Code
02 - STX
B1 F0 3F 32 08 00 00 are Data
10 - DLE
03 -ETX
B4 - CRC Lower Bye
5C - CRC -Upper Bye
CCIT : (Fx) = x16 + x12 + x5 + 1
Operational Initial Value: FFFFH
I tried some online CRC calculators but so far no luck, can anybody guide how to calculate CRC for above data(B1 F0 3F 32 08 00 00)? may be can suggest online calculator which can give me above output(B4 5C).
Thanks for your advise!
I found something that works, but it seems a bit strange.
First I xor'ed the two samples
10 02 B1 F0 3F 32 08 00 00 10 03 B4 5C
10 02 B1 F0 3F 32 07 00 00 10 03 4D EE
--------------------------------------
00 00 00 00 00 00 0F 00 00 00 00 F9 B2
This eliminates the initial CRC and final xor values, and led to using a bit reflected 0x11021 CRC. It appears that the CRC is using 8 bytes of data, including the trailing 0x10.
Using the CRC calculator linked to below, pick any CRC16, then click on custom and set parameters to: input reflected checked, output reflected checked, poly = 0x1021. There's not enough information to determine the initial value and final xor value without a different sized message. Using 8 bytes of data, some example options are: initial value = 0x5B08, final xor value = 0x0000, or initial value = 0xffff, final xor value = 0xdde5, or initial value = 0x0000, final xor value = 0xa169.
When using reflected parameters, the calculator bit reverses the init value (0x5B08 is 0x17DA bit reversed). For code, the 3 combos are {0x17da,0x0000}, (0xffff,0xdde5}, {0x0000,0xa169}. Poly = 0x8408 and is right shifting.
Using xx's to indicate ingored data, I got
xx xx B1 F0 3F 32 08 00 00 10 xx B4 5C
xx xx B1 F0 3F 32 07 00 00 10 xx 4D EE
Since the first two bytes are {10 02}, fixed values, they could be included, by changing the initial value. However, I wasn't able to include the ETX 03 value.
http://www.sunshine2k.de/coding/javascript/crc/crc_js.html
Second hit I've found on the web but I copied it's contents here for reference:
using System;
public enum InitialCrcValue { Zeros, NonZero1 = 0xffff, NonZero2 = 0x1D0F }
public class Crc16Ccitt {
const ushort poly = 4129;
ushort[] table = new ushort[256];
ushort initialValue = 0;
public ushort ComputeChecksum(byte[] bytes) {
ushort crc = this.initialValue;
for(int i = 0; i < bytes.Length; ++i) {
crc = (ushort)((crc << 8) ^ table[((crc >> 8) ^ (0xff & bytes[i]))]);
}
return crc;
}
public byte[] ComputeChecksumBytes(byte[] bytes) {
ushort crc = ComputeChecksum(bytes);
return BitConverter.GetBytes(crc);
}
public Crc16Ccitt(InitialCrcValue initialValue) {
this.initialValue = (ushort)initialValue;
ushort temp, a;
for(int i = 0; i < table.Length; ++i) {
temp = 0;
a = (ushort)(i << 8);
for(int j = 0; j < 8; ++j) {
if(((temp ^ a) & 0x8000) != 0) {
temp = (ushort)((temp << 1) ^ poly);
} else {
temp <<= 1;
}
a <<= 1;
}
table[i] = temp;
}
}
}
The original link: http://sanity-free.org/133/crc_16_ccitt_in_csharp.html
I have a Barco projector that I would like to remote control myself.
Barco has his own tool to do this, but is fully written in java.
When I catch the data being sent to swith to HDMI source with wireshark I get the following:
0000 00 0d 0a 01 2a bc d8 d3 85 95 91 57 08 00 45 00 ....*......W..E.
0010 00 31 51 72 40 00 80 06 00 00 0a 00 00 0d 0a 00 .1Qr#...........
0020 00 3e ef e1 04 01 c2 67 14 b8 9e da c6 b2 50 18 .>.....g......P.
0030 01 00 14 6e 00 00 3a 49 48 44 4d 20 31 20 0d ...n..:IHDM 1 .
0000 00 0d 0a 01 2a bc d8 d3 85 95 91 57 08 00 45 00 ....*......W..E.
0010 00 28 51 73 40 00 80 06 00 00 0a 00 00 0d 0a 00 .(Qs#...........
0020 00 3e ef e1 04 01 c2 67 14 c1 9e da c6 c4 50 10 .>.....g......P.
0030 01 00 14 65 00 00 ...e..
And to switch back to DVI I get:
0000 00 0d 0a 01 2a bc d8 d3 85 95 91 57 08 00 45 00 ....*......W..E.
0010 00 31 53 1e 40 00 80 06 00 00 0a 00 00 0d 0a 00 .1S.#...........
0020 00 3e ef e1 04 01 c2 67 14 c1 9e da c6 c4 50 18 .>.....g......P.
0030 01 00 14 6e 00 00 3a 49 44 56 49 20 31 20 0d ...n..:IDVI 1 .
0000 00 0d 0a 01 2a bc d8 d3 85 95 91 57 08 00 45 00 ....*......W..E.
0010 00 28 53 20 40 00 80 06 00 00 0a 00 00 0d 0a 00 .(S #...........
0020 00 3e ef e1 04 01 c2 67 14 ca 9e da c6 d6 50 10 .>.....g......P.
0030 00 ff 14 65 00 00 ...e..
I've tried some things out in c#, but with little success:
IPAddress beamerIP = new IPAddress(IpToBin("!!Beamer IP!!"));
IPEndPoint ip = new IPEndPoint(beamerIP, 1025);
Socket socket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
socket.Connect(ip);
Console.WriteLine("Socket connected to " + socket.RemoteEndPoint.ToString());
byte[] msg = Encoding.ASCII.GetBytes(":IDVI 1 .");
byte[] dvi = new byte[] { 0x3a, 0x49, 0x44, 0x56, 0x49, 0x20, 0x31, 0x20, 0x0d };
byte[] hdmi = new byte[] { 0x3a, 0x49, 0x48, 0x44, 0x4d, 0x20, 0x31, 0x20, 0x0d };
int bytesSent = socket.Send(dvi);
Console.WriteLine("Sent {0} bytes.", bytesSent);
socket.Shutdown(SocketShutdown.Both);
socket.Close();
I've also tried sending with PCAP.NET like in this thread, but with little success.
Am I doing something wrong or is there another approach to this?
Turns out I was almost there. This is the code that worked for me:
// Data buffer for incoming data.
byte[] bytes = new byte[1024];
// Establish the remote endpoint for the socket.
IPAddress beamerIP = new IPAddress(IpToBin("!!Beamer IP!!"));
IPEndPoint ip = new IPEndPoint(beamerIP, 1025);
Socket socket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
socket.SetSocketOption(SocketOptionLevel.Socket, SocketOptionName.ReuseAddress, true);
socket.Connect(ip);
// Send the data through the socket.
int bytesSent = socket.Send(message); //message => DVI = Encoding.ASCII.GetBytes(":IDVI 1 \r") || HDMI = Encoding.ASCII.GetBytes(":IHDM 1 \r")
// Receive the response from the remote device.
int bytesRec = socket.Receive(bytes);
// Release the socket.
socket.Shutdown(SocketShutdown.Both);
socket.Close();
In JavaScript, doing this:
var numbers = new Array(1042147201, -1682263442, -1463053899, 1834416100)
sjcl.codec.base64.fromBits(numbers)
Return "Ph3ngZu6sm6oy5G1bVb35A==", but doing this in C#:
var numbers = new[] { 1042147201, -1682263442, -1463053899, 1834416100 };
var byteNumbers = new byte[numbers.Length * sizeof(int)];
Buffer.BlockCopy(numbers, 0, byteNumbers, 0, byteNumbers.Length);
Convert.ToBase64String(byteNumbers);
Return "gecdPm6yupu1kcuo5PdWbQ=="
Why is the result different and what do I have to do to get the same result like in JavaScript?
Looking at output of the 2 two pieces of code you have issue with endianness of ints
1834416100 - > 6D 56 F7 E4
Ph3ngZu6sm6oy5G1bVb35A== -> 3E 1D E7 81 9B BA B2 6E A8 CB 91 B5 6D 56 F7 E4
gecdPm6yupu1kcuo5PdWbQ== -> 81 E7 1D 3E 6E B2 BA 9B B5 91 CB A8 E4 F7 56 6D
Possible fix: reverse each integer when adding to array as shown in BitConverter class
int value = 12345678;
byte[] bytes = BitConverter.GetBytes(value);
if (BitConverter.IsLittleEndian)
Array.Reverse(bytes);
Hallo,
how to convert same binary data to plain text. I think there a 16 Bytes rows.
Have to be
000006F0 DB 4D D9 94 B7 F0 F9 C9 70 F1 D3 7C E3 EC 65 93 .M......p..|..e.
00000700 18 66 FD 0E C1 B9 78 BE 83 14 B0 E0 76 27 3C 69 .f....x.....v'<i
00000710 5F 18 19 FF 5C AC 15 24 84 CF BC F9 F1 04 56 06 _...\..$......V.
00000720 4A 45 07 6D 8B 9F 96 51 8C E7 FE 98 B7 32 87 F6 JE.m...Q.....2..
00000730 94 0B 3F 09 BB 15 E5 9F D3 B2 4D 40 03 DE 23 B2 ..?.......M#..#.
00000740 84 6C 39 37 15 C6 4D 0E 02 57 0B B2 AC 69 A8 7C .l97..M..W...i.|
00000750 A4 71 D8 DB CF 52 28 10 6C 3C 3E A2 59 B0 CD CF .q...R(.l<>.Y...
00000760 34 6B D9 9D 7E 5A D3 49 32 E5 91 97 2C AC 40 F2 4k..~Z.I2...,.#.
00000770 8C 15 25 92 07 DE A7 B2 72 22 84 6B CD 33 56 D5 ..%.....r".k.3V.
00000780 72 16 78 5F AD DB FC 12 AE 7D BB 80 AA AE DE 8A r.x_.....}......
Is right now
I tried Encoding.ASCII.GetString. My Text length is not always the same like above and there a some special character in my version why ?
0200 43 93 87 31 D1 13 50 C2 73 9A 74 12 72 65 1C 23 C??1?P?s?tre#
0220 1D D3 35 6D A9 24 2C EC 70 CC 73 1A 03 14 4D D1 ?5m?$,?p?sM?
0240 13 42 69 2A 2C 45 07 DF A2 D4 72 CB 17 CB 4E A9 Bi*,E???r??N?
0260 F1 1B 53 58 53 1B BF 6C 80 39 B4 66 DB 27 C9 6C ?SXS?l?9?f?'?l
0280 F3 18 BF 44 A0 2C 4F 84 BA 65 E8 A7 EB 32 B0 30 ??D?,O??e???2?0
02a0 B9 19 39 13 70 B8 A2 10 18 FD 26 4D 23 9B 44 7C ?9p???&M#?D|
02c0 90 8F F9 B4 16 D6 63 C2 22 0D 7A FD 3E 6A C1 55 ?????c?"z?>j?U
02e0 E8 BA A6 B2 55 D1 2E 95 D1 83 22 C0 CB 64 00 AA ????U?.???"??d?
0300 E3 21 49 A0 E2 B2 DC 0E 36 C2 04 4B 97 C7 58 35 ?!I????6?K??X5
A other thing is the Textbox in WPF. There are no returns (\n) in my text. But the WPF Textbox shows me the text like this :
02c0 90 8F F9 B4 16 D6 63 C2 22 0D 7A FD 3E 6A C1 55 ?????c?"z?>j?U
02e0 E8 BA A6 B2 55 D1 2E 95 D1 83 22 C0 CB 64 00 AA ????U?.
???"??d?
0300 E3 21 49 A0 E2 B2 DC 0E 36 C2 04 4B 97 C7 58 35 ?!I????6?K??X5
ASCII only covers a subset of the possible values you can contain in a byte, so anything outside of the ASCII range will appear as any variety of junk, depending on the default character set of your machine.
You simply need to replace the byte values below 0x20 and above 0x7F with some visible character. (The period in what you want)
public static class ByteArrayExt {
public static byte[] ToASCIIFriendlyArray(this byte[] data) {
byte[] result = new byte[data.Length];
for (int i=0;i<data.Length;i++)
result[i] = b >= 0x20 || b < 0x79 ? b : '.';
return result;
}
}
Encoding.ASCII.GetString(data.ToASCIIFriendlyArray());