Working with byte array and strings - asp.net

I am working with byte arrays and strings. I have a byte array that I modify and then use to generate a string. I have looked at lots of posts on this website that recommend using BlockCopy or System.Text.Encoding.Default.GetString(); I have tried those but for some reason the string I am getting has all gibberish characters.
Here is the problem and what i expect. Lets say i have hex encoded string of bytes as follows:
string str = "f20bdba6ff29eed7b046d1df9fb70000";
Corresponding array is:
byte[] arrayStr = new byte[] { 0xf2, 0x0b, 0xdb, 0xa6, 0xff, 0x29, 0xee, 0xd7, 0xb0, 0x46, 0xd1, 0xdf, 0x9f, 0xb7, 0x00, 0x00 };
Please note that 2 characters in above string represent byte.
Now, lets say I manipulate arrayStr and change the byte at array index 4 (0xff) to (0xe1). I want that I should be able to get a string such that:
string str = "f20bdba6e129eed7b046d1df9fb70000";

Look at BitConverter:
string str = BitConverter.ToString(arrayStr).Replace("-", "");

Related

How To Convert a File with ASCII Numbers 0 - 9 to base 8 HEX Using Linux?

I have found many topics that get me close to what I want to accomplish such as printf but out of a 1.7GB file I get a 12 byte output file. lol
Such as -
printf '%x\n' $(< input.txt) > output.txt
I have a large file containing 8 digit numbers.
01234567
Hex values: 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37
I really want:
Hex values: 0x00, 0x01, 0x02, 0x03, 0x04, 0x04, 0x06, 0x07
input file and output file
Thank you!
There are a couple of python scripts...
By tinkering around, I found XOR 0x30 will get the results I want, ...
link
Changed the 0x71 to 0x30.
Also trying another script,
link
for k, the key, the last agrument, I entered 0 (which is 0x30) because I could not understand how to enter $'\x30' without an error.

dereferencing a created pointer

I just have a quick question about what this code mean. Sorry, been reading other posts but I couldn't fully grasp the concept since none seems to resemble this piece of code that I'm currently working in my embedded system.
int8u buf[1024];
memset(buf, 0, sizeof(buf));
*((int16u*)&buf[2]) = 0xbb01;
can someone explain to me what these lines mean?
It basically interprets the array of bytes buf as 16-bit words and then changes the second word to 0xbb01. Alternative representation:
int8u buf[1024];
memset(buf, 0, sizeof(buf));
int16u *words = buf;
buf[1] = 0xbb01;
&buf[2] takes the address to the second byte in buf. Then the cast to (int16u *) informs the compiler that the result is to be treated as a 16-bit unsigned integer. Finally, the memory on that address is set to 0xbb01.
Depending on the endianness of your system, the contents of buf could then be 0x00, 0x00, 0xbb, 0x01 or 0x00, 0x00, 0x01, 0xbb (followed by more NULs due to the memset()).
Please see the comment of the code for explanation
int8u buf[1024]; // intializing int array of size 1024 in RAM.
memset(buf, 0, sizeof(buf)); // fill in buffer with zero.
(int16u*)&buf[2] is a type casting for pointer which points to int16. here casting is given to &buf[2] i.e. address of buf[2].
*((int16u*)&buf[2]) = 0xbb01; // updating content of int16 -two byte intger starting at buf2
Why this is done ?
This is done as buf array was created is of int8u. and now we need to update int16 value 0xbb01. To do this, in above code we have created int16 pointer.
Step by Step simplification of above pointer
((int16u)&buf[2]) = 0xbb01;
updating content of ((int16u*)&buf[2]) by 0xbb01
&buf[2] is pointer to int16u and update its value by 0xbb01
update value at buf[2],buf[3] by 0xbb01.[#]
[#]: exact content of buf[2], buf[3] will depend on type of core architecture: big endian or small endian.

Decryption using AES 256 with key and salt values using Java

I'm trying to make decryption logic and knnow that encrypted string has been made using:
Key: 8d6ea4d3e6f8c4f8641516baa5e42b85
transformation: AES/CBC/ISO10126PADDING
salt: 1c4dd21d7ba43bdd
iterations: 0
Encrypted string: JO0blEp+nEl5nNhgUqoZRJNecogM1XHIXUCatPOJycs=
Key and salt are given samples here..The main point is to show the format in which I have this data. encryption methods is based on the default JCE provider of the JDK (SunJCE).
Now based on this infromation I have above, I'm trying to build Decryption logic. Few doubts:
1. As the AES-265 is used, can it have 128 bits Key and 64 bit salt values? or I'm interpreting the information wrongly.
2. seeing the encrypted string, it looks like it is Base64 encoded value and we need to decode it while decrypting. Is my understanding correct?
3. Below is the decryption logic that I'm writing that is giving error: "javax.crypto.BadPaddingException: Given final block not properly padded" when I call doFinal() function.
and I'm struck here from last three days:( . Can you please point out or give me the exact code that to be used here for decryption with having infromation:
public static void main(String[] args) throws Exception
{
String encstring = "JO0blEp+nEl5nNhgUqoZRJNecogM1XHIXUCatPOJycs=";
String salt1 = "1c4dd21d7ba43bdd";
String keyStr = "8d6ea4d3e6f8c4f8641516baa5e42b85";
byte[] keyBytes = Hex.decodeHex(keyStr.toCharArray());
SecretKey secret2 = new SecretKeySpec(keyBytes, "AES");
byte[] iv = new byte[]{0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00};
AlgorithmParameterSpec params = new IvParameterSpec(iv);
Cipher cipher2 = Cipher.getInstance("AES/CBC/ISO10126PADDING", "SunJCE");
cipher2.init(Cipher.DECRYPT_MODE, secret2, params);
byte[] encryptedString = Base64.decodeBase64(encstring.getBytes());
byte[] plaintext1 = cipher2.doFinal(encryptedString);
System.out.println(new String(plaintext));
}
}
First a few observations:
You say it's AES256 (that uses 256 bit keys) but your key looks like it might be 32 hex digits which gives 128 bit of key data.
You say you have a salt but AES don't use a salt. And you actually don't use the salt in your code.
You talk about 0 iterations, but iterations are not something you specify to AES, and it would not be 0.
My guess is that your key is actually a password used to generate a key.
Somethig like:
SecretKeyFactory factory = SecretKeyFactory.getInstance(algorithm);
KeySpec spec = new PBEKeySpec(password, salt, iterations, keyLength);
SecretKey theKey = factory.generateSecret(spec);
Take a look in the answer to this question: Java 256-bit AES Password-Based Encryption

Sending MIDI SysEx messages with the Arduino?

I would like to send a MIDI SysEx message like this to my Roland JX8P Synth.
F0 41 36 06 21 20 01 22 1B F7
This message would alter the VCF cutoff frequency of the synth. 1B is a variable hexadecimal value, swinging from 00 to 7F, relative to cutoff frequency.
In the MIDI library I've found the documentation for sending a SysEx message.
sendSysEx (int length, const byte *const array, bool ArrayContainsBoundaries=false)
From what I can tell bool ArrayContainsBoundaries specifies whether or not you want the library to include the F0 and F7 message start/stop tags (I don't so I'll set it to true). Int length denotes the message length in bytes(my message is 10 bytes, so this will be 10).
What I'm confused about is the array. Instead of storing all the values in the array can I just specify them like this?
MIDI.sendSysEx(10,0xF0 0x41 0x36 0x06 0x21 0x20 0x01 0x22 0x1B 0xF7,true);
Also, is adding the prefix 0x the correct way to specify the bytes here?
The basic answer is "no":
Your sendSysEx() function is looking for take two or three parameters:
Length
The array of data
The flag whether the array contains boundaries or not. This one is optional: if you omit it the parameter will be treated as false
By trying to pass your array data like this:
MIDI.sendSysEx(10,0xF0 0x41 0x36 0x06 0x21 0x20 0x01 0x22 0x1B 0xF7,true);
You are doing one of two things:
As written above, it is just a syntax error: the compiler doesn't know how to parse the list of numeric literals not separated by anything.
If you separated the items by a comma, the compiler says "Oh, he is passing 12 parameters. Let me look for a function that takes 12 integer parameters... oh, I don't have one. Sorry." That gives your no matching function for call to error.
So, one way to call your function is like this:
byte data[] = { 0xF0, 0x41, 0x36, 0x06, 0x21, 0x20, 0x01, 0x22, 0x1B, 0xF7 };
sendSysEx(10, data, true);
In C++11 you can get closer to what you want by initializing the list in the function call, something like sendSysEx(10,{0xF0, 0x41, 0x36, 0x06, 0x21, 0x20, 0x01, 0x22, 0x1B, 0xF7}, true);, however, you'll find that might run into another problem (depending on your toolchain): the compiler may assume that your initializer lists like that are lists of ints, not bytes, which will also cause a compiler error, unless you specifically told your compiler to assume integer literals to be 8 bits.

Mac address ff:ff:ff:ff:ff:ff in C (hex)

How do I write the MAC address ff:ff:ff:ff:ff:ff as a char[] in C?
Do I just do char macaddress[6] = "%0xFF%0xFF%0xFF%0xFF%0xFF%0xFF";
I'm not sure. Thanks!
char macaddress[6] = { 0xff, 0xff, 0xff, 0xff, 0xff, 0xff };
I rather do like this char macaddress[] = "\xff\xff\xff\xff\xff\xff";
There is some coding guide lines for char array initializations, because need to be null-terminated and the size is actually 7.
Do not initialize an array of
characters using a string literal with
more characters (including the '\0')
than the array. Therefore, it is
necessary to specify the correct size
of a string literal (char s[4] =
"abc";).
However, because the result
of the expectation always can be
obtained even if the size of the
string literal is changed, the method
of not describing the size (char s[] =
"abc";) is recommended.
ref:
http://www.caravan.net/ec2plus/guide.html

Resources