2020-05-08 03:28 AM
I am doing AES Encryption of the given data in a processor and transferring it to another processor where the original data is obtained by decryption. From the help from our community member(@Community member i am able to obtain the output.. Thanks for your support. Now whenever i am transferring the 128 bit encrypted data using (HAL_UART_Transmit(&huart4, (uint8_t*)buffer, sprintf(buffer,"%08X", Decryptedtext[0]), 20);) this function data can be transferred.But at the receiver side the array is received a string array and from that array i cannot get the corresponding 32 bit hexadecimal values.SO for decryption i cannot obtain the correct value.. so kindly give suggestions so that i can proceed with my work...
For example: I am representing as 32 bit
sent data=0x12ab3456
received data= 12ab3456( as a string) and also i am receiving the data byte by byte and on applying shift operation for combining the values in array changes and decryption cant be done..
But the required output is 0x12ab3456 (as hexadecimal) to be stored in a 32 bit array...
2020-05-08 03:39 AM
> received data= 12ab3456( as a string) ...
So, why not using the string functions of the standard C library ?
Like strtol() for instance, which can be parametrized to accept only hexadecimal.
Even writing you own decoder isn't really difficult.
2020-05-08 04:16 AM
Received data is received in bytes and it is stored in an array and total number of characters received is 32 digits for example
"2ABCD1349876543212345678AC23DF45" stored in receiver array rxBuff..i.e., rxBuf[1]=2 rxBuf[2]=A etc... where rxBuf is char uint8_t each digit representing 8 bits
Next i have to segregate these digits into 32 bit array each a having 8 digits i.e., Hexarray[0]=0x2ABCD134; ..where Hexarray is uint32_t each digit representing 4 bits
so for the above datatype conversion how can we apply strtol() ....
I am new to STM 32 programming ..so kindly guide me to develop my own decoder...
Thank you
2020-05-08 04:16 AM
Received data is received in bytes and it is stored in an array and total number of characters received is 32 digits for example
"2ABCD1349876543212345678AC23DF45" stored in receiver array rxBuff..i.e., rxBuf[1]=2 rxBuf[2]=A etc... where rxBuf is char uint8_t each digit representing 8 bits
Next i have to segregate these digits into 32 bit array each a having 8 digits i.e., Hexarray[0]=0x2ABCD134; ..where Hexarray is uint32_t each digit representing 4 bits
so for the above datatype conversion how can we apply strtol() ....
I am new to STM 32 programming ..so kindly guide me to develop my own decoder...
Thank you
2020-05-08 04:21 AM
Write a function which decode one character into the four-bit nibble - if it's digit, subtract '0'; if it's a character between 'a' and 'f' subtract 'a' and add 10, if between 'A' to 'F' subtract 'A' and add 10.
Then, for the required number of characters, repeatedly call this function for individual characters, shift an accumulating variable to the left by 4 bits and add the result of character decoding to it.
This has NOTHING to do with STM32, this is basic programming.
JW
2020-05-08 04:47 AM
Figure out how to manage data representation, this has little to do with STM32, or advanced topics like encryption.
2020-05-08 04:55 AM
If you can reconstruct the bytes on the PC side you can send the 32-bit value as 4-bytes
HAL_UART_Transmit(&huart4, (uint8_t*)&Decryptedtext[0], 4, 100);
Or just the whole array as bytes
HAL_UART_Transmit(&huart4, (uint8_t*)Decryptedtext, sizeof(Decryptedtext), 500);
2020-05-08 05:01 AM
I am here to ask for ur help.. i am having individual data received as characters each of length 8 bits... 2ABCD1349876543212345678AC23DF45
I am not in need of separating a single 8 bit char into two 4 bit nibbles .....
as my level of understanding goes and based on the lines used in my program
i)For TRANSMITTING 128 bit DATA from processor I
{
HAL_UART_Transmit(&huart4, (uint8_t*)buffer, sprintf(buffer,"%08X",Encryptedtext[0]), 20);
HAL_UART_Transmit(&huart4, (uint8_t*)buffer, sprintf(buffer,"%08X",Encryptedtext[1]), 20);
HAL_UART_Transmit(&huart4, (uint8_t*)buffer, sprintf(buffer,"%08X",Encryptedtext[2]), 20);
HAL_UART_Transmit(&huart4, (uint8_t*)buffer, sprintf(buffer,"%08X",Encryptedtext[3]), 20);
}
ii) RECEIVING DATA using Interrupt at processor II
{
void UART4_IRQHandler(void)
{
/* USER CODE BEGIN UART4_IRQn 0 */
/* USER CODE END UART4_IRQn 0 */
HAL_UART_IRQHandler(&huart4);
/* USER CODE BEGIN UART4_IRQn 1 */
HAL_UART_Receive_IT(&huart4,&data,1);
//inc++;
if(data=='*'){receive_bit=1;inc=0;}
if(receive_bit==1){rxBuf[inc]=data;inc+=1;} //rxBuf id 8 bit char array
if(data=='#'){receive_bit=2;}
/* USER CODE END UART4_IRQn 1 */
}
}
iii) for decrypting received data at processor II
{
HAL_CRYP_Decrypt(&hcryp,Encryptedtext,16,Decryptedtext,100); // where Encrypted text is 32 bit is unsigned int which must be formed from rx_Buf
which is my problem
}
Is my understanding about the problem ok or is my understanding of the problem is wrong?...
2020-05-08 04:22 PM
Lots of hits for this in Google:
https://stackoverflow.com/questions/10156409/convert-hex-string-char-to-int
Seems silly to convert bytes to a string just to convert that string back to bytes, especially if you can't manage the last part.
2020-05-30 03:01 AM
The only safe way to convert strings to integers with C standard libraries is described there:
But of course using some simple binary protocol is much more effective and simpler.